数据结构与算法(4)顺序栈

栈的定义

栈是一种比较特殊的线性表,它的操作受限,其限制是仅允许在表的一端进行插入和删除运算, 这种限制造成栈最先进入的元素最晚出栈,也就是FILO(First In, Last Out)。 可以操作的一端被称为栈顶,另一端称之为栈底。向一个栈插入新元素称为压栈push(将新元素放在栈顶元素的上面,成为新的栈); 从一个栈删除元素称为出栈pop (将栈顶元素删除,使栈顶元素下面的元素成为新的栈顶元素)

栈的结构

  • 使用静态数组来存放栈中元素,其中top元素记录栈顶的位置属性
typedef char Elemtype;

typedef struct{
    Elemtype data[STACK_MAX_SIZE];
    int top;
}SqStack;

栈的基本运算实现

  • 栈的初始化
/*
初始化栈
*/
Status InitStack(SqStack *s)
{
    s->top = -1;
    return OK;
}
  • 栈空判断
/*
栈空判断
*/
Status StackEmpty(SqStack s)
{
    if (s.top == -1)
        return TRUE;
    else
        return FALSE;
}
  • 压栈
/*
压栈
*/
Status Push(SqStack *s, Elemtype e)
{
    if (s->top >= STACK_MAX_SIZE - 1)
        return OVERFLOW;    //已达到最大的容量

    s->top++;
    s->data[s->top] = e;

    return OK;
}
  • 出栈
/*
出栈
*/
Status Pop(SqStack *s, Elemtype *e)
{
    if (StackEmpty(*s))
        return ERROR;
    else{
        *e = s->data[s->top];
        s->top--;
        return OK;
    }
}
  • 显示栈顶元素
/*
显示栈顶元素
*/
Status GetTop(SqStack s, Elemtype *e)
{
    if (StackEmpty(s))
        return ERROR;
    else{
        *e = s.data[s.top];
        return OK;
    }

}
  • 得到栈中数据长度
/*
得到栈的长度
*/
Status GetLength(SqStack s)
{
    return s.top + 1;
}
  • 显示栈中数据内容
/*
显示栈中存放的数据
*/
Status DispStack(SqStack s)
{
    int i;
    if (StackEmpty(s)){
        printf("空栈\n");
        return ERROR;
    }
    else{
        for (i = 0; i < GetLength(s); ++i){
            printf("%c ", s.data[i]);
        }
        printf("\n");
        return OK;
    }
}

测试完整代码

#if 0
实现了顺序栈(采用的是静态数组的形式)
#endif

#include <stdio.h>
#include <stdlib.h>

#define STACK_MAX_SIZE 20

#define OK 1
#define ERROR 0
#define OVERFLOW -1

#define TRUE 1
#define FALSE 0

typedef int Status;
typedef char Elemtype;

typedef struct{
    Elemtype data[STACK_MAX_SIZE];
    int top;
}SqStack;


/*
初始化栈
*/
Status InitStack(SqStack *s)
{
    s->top = -1;
    return OK;
}
/*
栈空判断
*/
Status StackEmpty(SqStack s)
{
    if (s.top == -1)
        return TRUE;
    else
        return FALSE;
}
/*
压栈
*/
Status Push(SqStack *s, Elemtype e)
{
    if (s->top >= STACK_MAX_SIZE - 1)
        return OVERFLOW;    //已达到最大的容量

    s->top++;
    s->data[s->top] = e;

    return OK;
}
/*
出栈
*/
Status Pop(SqStack *s, Elemtype *e)
{
    if (StackEmpty(*s))
        return ERROR;
    else{
        *e = s->data[s->top];
        s->top--;
        return OK;
    }
}
/*
显示栈顶元素
*/
Status GetTop(SqStack s, Elemtype *e)
{
    if (StackEmpty(s))
        return ERROR;
    else{
        *e = s.data[s.top];
        return OK;
    }

}
/*
得到栈的长度
*/
Status GetLength(SqStack s)
{
    return s.top + 1;
}
/*
显示栈中存放的数据
*/
Status DispStack(SqStack s)
{
    int i;
    if (StackEmpty(s)){
        printf("空栈\n");
        return ERROR;
    }
    else{
        for (i = 0; i < GetLength(s); ++i){
            printf("%c ", s.data[i]);
        }
        printf("\n");
        return OK;
    }
}

int main()
{
    SqStack s;
    Elemtype e;
    printf("初始化栈\n");
    InitStack(&s);
    printf("a, b, c, d, e, f进行进栈操作\n");
    Push(&s, 'a');
    Push(&s, 'b');
    Push(&s, 'c');
    Push(&s, 'd');
    Push(&s, 'e');
    Push(&s, 'f');
    Push(&s, 'g');
    printf("显示栈中的内容");
    DispStack(s);
    Pop(&s, &e);
    printf("弹栈操作 %c\n", e);
    Pop(&s, &e);
    printf("弹栈操作 %c\n", e);
    printf("显示栈中内容\n");
    DispStack(s);
    GetTop(s, &e);
    printf("显示栈顶元素%c \n",e);

    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值