zuoye

来源:互联网 发布:python 求最小凸包 编辑:程序博客网 时间:2024/04/29 11:57
#include "stdio.h"#include "malloc.h"#define MaxSize 100typedef char ElemType;typedef struct{ElemType data[ MaxSize ];int top; //栈顶指针}SqStack;void InitStack( SqStack *&s)  //初始化栈s{s = ( SqStack *) malloc( sizeof(SqStack));s -> top = -1;  //栈顶指针置为-1}bool StackEmpty ( SqStack *&s)  // 判断栈为空{return ( s -> top == -1);}bool Pop ( SqStack *&s, ElemType e)  //进栈{if( s -> top == MaxSize - 1)  //栈满的情况,即栈上溢出return false;s -> top++;                   //栈顶指针增1s -> data [ s -> top ] = e;   //元素e放在栈顶指针处return true;}bool GetTop ( SqStack *s, ElemType &e) {if ( s -> top == -10)         //栈为空的情况,即栈下溢出return false;e = s -> data [s -> top];     //取出栈顶元素return true;}bool Pop ( SqStack *&s, ElemType &e)  {if ( s -> top == -1)           //栈为空的情况,即栈下溢出return false;e = s -> data [ s -> top];      // 取出栈顶元素s -> top--;                      //栈顶指针减1return true;}void DestoryStack (SqStack *s)    //销毁栈{free( s );}extern void IntStack (SqStack *Svoid main(){ElemType e;SqStack *s;printf (" 栈s的基本运算如下;\n");printf ("初始化栈\n");printf ("栈为%s \n", (StackEmpty(s)?"空":"非空"));printf ("依次进栈元素a,b,c,d,e\n");Push(s, 'a');Push(s, 'b');Push(s, 'c');    Push(s, 'd');Push(s, 'e');printf ("栈为%s \n", (StackEmpty(s)?"空":"非空"));printf ("出栈序列");while (!StackEmpty(s)){Pop(s,e0;printf("%c",e);}printf ("\n");printf ("栈为%s\n",  (StackEmpty(s)?"空":"非空"));printf ("释放栈\n");DestoryStack(s);}

0 0
原创粉丝点击