数据结构中的栈(c)
栈中的初始化和进栈操作#define_CRT_SECURE_NO_WARNINGS#includestdio.h#defineMaxSize10//栈中元素的最大个数typedefstruct{intdata[MaxSize];//静态数组存放栈中元素inttop;//栈顶指针}SqStack;//Sequence - 顺序//初始化栈voidInitStack(SqStackS){S.top-1;//初始化栈顶指针}//判断栈空boolStackEmpty(SqStack S){if(S.top-1){printf(Now the Stack is empty!\n);returntrue;}else{returnfalse;}}//进栈操作boolPush(SqStackS,intx){if(S.topMaxSize-1)returnfalse;S.top1;//等价于S.data[S.top]x;//S.data[S.top] x;returntrue;}//出栈操作boolPop(SqStackS,intx){if(S.top-1)returnfalse;xS.data[S.top--];returntrue;}voidtestStack(){SqStack S;//声明一个顺序栈分配空间InitStack(S);StackEmpty(S);Push(S,3);printf(SqStack: %d\n,S.data[0]);Push(S,5);printf(SqStack: %d %d\n,S.data[0],S.data[1]);Push(S,7);printf(SqStack: );for(inti0;iS.top;i){printf(%d ,S.data[i]);}printf(\n----------------);}intmain(){testStack();return0;}