C语言实现数据结构线性表之顺序栈

来一个C语言版本链表完整操作

#include<stdio.h>

#define  MAXLEN  100          /*顺序栈存储空间的总分配量*/
typedef  int  DataType;          /*定义DataType为int类型*/ 
typedef  struct                  /*顺序栈存储类型*/
{   DataType   data[MAXLEN];    /*存放顺序栈的数组*/
	int  top;                      /*记录栈顶元素位置的变量*/
}SeqStack; 
SeqStack s;

void  InitStack( SeqStack  *S )
{   /*初始化栈函数*/
    S->top=-1;               /*初始化的顺序栈为空*/
	printf("初始化成功\n");
} 

int EmptyStack(SeqStack *S)
{   
	/*判断栈空函数*/
    if(S->top==-1)               /*栈为空*/
        return 1;
    else
        return 0;
} 

int FullStack(SeqStack *S)
{   /*判断栈满函数*/
    if(S->top==MAXLEN-1)               /*栈为满*/
        return 1;
    else
        return 0;
} 

int Push(SeqStack *S,DataType x)
{   /*进栈操作函数*/
    if(FullStack(S))      /*调用判满函数FullStack(S),判断栈是否为满*/
    {   printf("栈满,不能进栈!");
        return 0;                /*栈满不能进栈*/
    }
    else                          /*栈不为满*/
    {   S->top++;
        S->data[S->top]=x;
        return 1;
    }
}

int Pop(SeqStack *S,DataType *x)
{   /*出栈操作函数*/
    if(EmptyStack(S))     /*调用判空函数EmptyStack(S),判断栈是否为空*/
    {   printf("栈空,不能出栈!");
        return 0;              /*栈空不能出栈*/
    }
    else                        /*栈不为空*/
    {   *x=S->data[S->top];
        S->top--;
        return 1;
    }
}

int GetTop(SeqStack *S,DataType *x)
{   /*取栈顶元素函数*/
    if(EmptyStack(S))    /*调用判空函数EmptyStack(S),判断栈是否为空*/
    {   printf("栈空,取栈顶元素失败!");
        return 0;
    }
    else                   /*栈不为空*/
    {   *x=S->data[S->top];
        return 1;
    }
} 


int main(){
	int r,value;
	printf("%d\n",s.top);
	InitStack(&s);
	printf("%d\n",s.top);
	printf("第一个=%d 第二个=%d  第三个=%d\n",s.data[0],s.data[1],s.data[2]);
	r=EmptyStack(&s);
	printf("%d\n",r);
	r=FullStack(&s);
	printf("%d\n",r);
	r=Push(&s,10);
	printf("%d\n",r);
	r=Push(&s,20);
	printf("%d\n",r);
	r=Push(&s,30);
	printf("%d\n",r);
	printf("第一个=%d 第二个=%d  第三个=%d\n",s.data[0],s.data[1],s.data[2]);
	          
	r=Pop(&s,&value);
	printf("r=%d  value=%d\n",r,value);
	r=Pop(&s,&value);
	printf("r=%d  value=%d\n",r,value);

	printf("第一个=%d 第二个=%d  第三个=%d\n",s.data[0],s.data[1],s.data[2]);
	r=GetTop(&s,&value);
	printf("r=%d  value=%d\n",r,value);
	r=GetTop(&s,&value);
	printf("r=%d  value=%d\n",r,value);

	return 0;
}

相关阅读:

数据结构
顺序表
链表
顺序栈

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农NoError

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值