定义顺序栈存储结构,实现其基本运算,并完成测试。
要求:
1、头文件sqstack.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> InitStack(SqStack *&s); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化栈</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> DestroyStack(SqStack *&s); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//销毁栈</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> StackEmpty(SqStack *s); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//栈是否为空</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> StackLength(SqStack *s); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回栈中元素个数——栈长度</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> Push(SqStack *&s,ElemType e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//入栈</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> Pop(SqStack *&s,ElemType &e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//出栈</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> GetTop(SqStack *s,ElemType &e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//取栈顶数据元素</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> DispStack(SqStack *s); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//输出栈</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
2、在sqstack.cpp中实现这些函数
3、在main函数中完成测试,包括如下内容:
(1)初始化栈s
(2)判断s栈是否为空
(3)依次进栈元素a,b,c,d,e
(4)判断s栈是否为空
(5)输出栈长度
(6)输出从栈顶到栈底元素
(7)出栈,并输出出栈序列
(8)判断s栈是否为空
(9)释放栈
代码:
#ifndef SQSTACK_H_INCLUDED#define SQSTACK_H_INCLUDED#include<iostream>#include "malloc.h"#define MaxSize 50typedef int ElemType;typedef struct{ ElemType data[MaxSize]; int top;}SqStack;void InitStack(SqStack *&s); //初始化栈void DestroyStack(SqStack *&s); //销毁栈bool StackEmpty(SqStack *s); //栈是否为空int StackLength(SqStack *s); //返回栈中元素个数——栈长度bool Push(SqStack *&s,ElemType e); //入栈bool Pop(SqStack *&s,ElemType &e); //出栈bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素void DispStack(SqStack *s); //输出栈#endif // SQSTACK_H_INCLUDED
#include<iostream>#include<stdio.h>#include"sqstack.h"void InitStack(SqStack *&s)//初始化栈{ s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1;}void DestroyStack(SqStack *&s) //销毁栈{ free(s);}bool StackEmpty(SqStack *s) //栈是否为空{ return (s->top==-1);}int StackLength(SqStack *s) //返回栈中元素个数——栈长度{ return(s->top+1);}bool Push(SqStack *&s,ElemType e) //入栈{ if(s->top==MaxSize-1) return false; s->top++; s->data[s->top]=e; return true;}bool Pop(SqStack *&s,ElemType &e) //出栈{ if(s->top==-1) return false; e=s->data[s->top]; s->top--; return true;}bool GetTop(SqStack *s,ElemType &e)//取栈顶数据元素{ if(s->top==-1) return false; e=s->data[s->top]; return true;}void DispStack(SqStack *s) //输出栈{ if(s->top==-1) printf("你的栈为空"); else { int i; for(i=s->top;i>-1;i--) { printf("%d\n",s->data[i]); } }}
#include <iostream>#include <stdio.h>#include "sqstack.h"using namespace std;int main(){ SqStack *L; int i,length; ElemType a=1,b=2,c=3,d=4,e=5,x; InitStack(L); StackEmpty(L); Push(L,a); Push(L,b); Push(L,c); Push(L,d); Push(L,e); if(StackEmpty(L)) return false; length=StackLength(L); printf("栈长为:%d\n",length); DispStack(L); printf("出栈序列:"); for(i=0;i<length;i++) { Pop(L,x); printf("%d ",x); } if(StackEmpty(L)) DestroyStack(L); else return false; return 0;}
运行结果:
0 0