来一个C语言版本链表完整操作
#include<stdio.h>
#define MAXLEN 100
typedef int DataType;
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))
{ printf("栈满,不能进栈!");
return 0;
}
else
{ S->top++;
S->data[S->top]=x;
return 1;
}
}
int Pop(SeqStack *S,DataType *x)
{
if(EmptyStack(S))
{ printf("栈空,不能出栈!");
return 0;
}
else
{ *x=S->data[S->top];
S->top--;
return 1;
}
}
int GetTop(SeqStack *S,DataType *x)
{
if(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;
}
相关阅读:
数据结构
顺序表
链表
顺序栈