您好,欢迎访问三七文档
#includestdio.h#includeStack.h#includeStackCommonFunction.hvoidmain(){Stacks;Tx;CreateStack(&s,10);Push(&s,10);Push(&s,15);PrintStack(s);x=*InputElement();Push(&s,x);PrintStack(s);Pop(&s);Pop(&s);if(IsEmpty(s))printf(IsEmpty!\n);elseprintf(IsnotEmpty!\n);PrintStack(s);}#includestdio.h#includeStack.h#includeStackCommonFunction.hT*InputElement(){staticTa;scanf(%d,&a);return&a;}voidPrintElement(Tx){printf(%d,x);}voidPrintStack(Stacks){if(IsEmpty(s))printf(TheStackisempty!\n);elsefor(;s.Top=0;s.Top--)PrintElement(s.Element[s.Top]);}voidCreateStack(Stack*s,intmaxsize){s-Top=-1;s-MaxSize=maxsize;}BOOLIsEmpty(Stacks){returns.Top0;}BOOLIsFull(Stacks){returns.Top=s.MaxSize-1;}voidPush(Stack*s,Tx){if(IsFull(*s))printf(Overflow!\n);else{s-Top++;s-Element[s-Top]=x;}}voidPop(Stack*s){if(IsEmpty(*s))printf(Underflow!\n);elses-Top--;}voidStackTop(Stacks,T*x){if(IsEmpty(s))printf(Underflow);else*x=s.Element[s.Top];}#ifndefSTACK_H#defineSTACK_H#defineMAXSIZE50#defineFALSE0#defineTRUE1typedefintBOOL;typedefintT;typedefstructstack{intTop,MaxSize;TElement[MAXSIZE];}Stack;#endif#ifndefSTACKCOMMONFUNCTION_H#defineSTACKCOMMONFUNCTION_H#includeStack.hvoidCreateStack(Stack*s,intmaxsize);//已经创建一个堆栈后,构造一个空堆栈,maxsize为实际长度BOOLIsEmpty(Stacks);//判断堆栈是否为空,返回BOOL值。1为空,0为不空BOOLIsFull(Stacks);//判断堆栈是否为满,返回BOOL值,1为满,0为不满voidPush(Stack*s,Tx);//入栈操作voidPop(Stack*s);//出栈操作voidStackTop(Stacks,T*x);//在x中返回栈顶元素T*InputElement();//输入单个元素T,返回输入值的地址voidPrintElement(Tx);//输出显示单个元素的值voidPrintStack(Stacks);//输出显示堆栈中的每一个元素#endif
本文标题:C语言堆栈
链接地址:https://www.777doc.com/doc-7027367 .html