来一个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;elsereturn 0;
} int FullStack(SeqStack *S)
{   /*判断栈满函数*/if(S->top==MAXLEN-1)               /*栈为满*/return 1;elsereturn 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;
}
相关阅读:
数据结构
 顺序表
 链表
 顺序栈