1、数据结构栈1 大学数据结构课程实验报告课程名称:数据结构班级:软件1011实验成绩:指导教师: 姓名: 实验项目名称:数据结构-栈学号:上机实践日期:一、目的理解栈数据结构及其应用;理解递归及其应用二、实验内容1 栈的类定义2 顺序栈的类定义、实现与测试;链栈的类定义、实现与测试3 栈的应用1(可使用顺序栈或链栈完成):实现括号匹配测试如下表达式:”()()” 及”()”三、实验使用环境VC+ 6.0控制台程序四、实验关键代码与结果展示(部分截图)1、栈的类定义:const int maxSize=50;templateclass Stack public: Stack(); virtual
2、Stack(); virtual void Push(const T& x)=0; virtual bool Pop(T& x)=0; virtual bool getTop(T& x)const=0; virtual bool IsEmpty() const =0; virtual bool IsFull() const = 0; virtual int getSize() const = 0;2、顺序栈的类定义、实现与测试:#include#include#includestdlib.h templateclass SeqStack public: SeqStack(int sz=50);
3、 SeqStack()delete elements; void Push(const T& x); bool Pop(T& x); bool getTop(T& x)const; bool IsEmpty()constreturn (top=-1)?true:false; bool IsFull()constreturn (top=maxSize-1)?true:false; int getSize()constreturn top+1; void MakeEmpty()top=-1; bool InPut(); bool OutPut()const;private: T *elements
4、; int top; int maxSize; void overflowProcess();templateSeqStack:SeqStack(int sz):top(-1),maxSize(sz) elements=new TmaxSize; assert(elements!=NULL); template void SeqStack:overflowProcess() int stackIncreament=maxSize; T *newArray = new TmaxSize + stackIncreament; if(newArray = NULL)cerr存储分配失败!endl;e
5、xit(1); for(int i = 0;i=top;i+)newArrayi=elementsi; maxSize=maxSize+stackIncreament; delete elements; elements=newArray; templatebool SeqStack:Pop(T &x) if(IsEmpty()=true)return false; x=elementstop-; return true; templatevoid SeqStack:Push(const T &x) if(IsFull()=true)overflowProcess(); elements+to
6、p=x; templatebool SeqStack:getTop(T &x)const if(IsEmpty()=true)return false; x=elementstop; return true; templatebool SeqStack:InPut() T val; cinval; Pop(val); return true; templatebool SeqStack:OutPut()const coutelementstopendl; return true;测试:#include #includeSeqStack.h void main() SeqStack seq(10
7、);int m;for(int i=0;i5;i+) coutm; seq.Push(m); /coutseq.getTop(m)endl; int t=seq.getSize(); cout当前栈内共有元素:tendl; seq.Pop(m); t=seq.getSize(); cout执行Pop()后栈内元素个数为:tendlendl; /seq.Push(x); cout请输入InPut()的val:; seq.InPut(); t=seq.getSize(); cout执行InPut()后栈内元素个数为:tendlendl; cout执行OutPut()后栈顶元素为:; seq.Out
8、Put();3、链栈的类定义、实现与测试:#includeSeqStack.htemplatestruct LinkNode T data; LinkNode *link; LinkNode(LinkNode *ptr = NULL)link = ptr; LinkNode(T item,LinkNode *ptr)data = item;link=ptr;templateclass LinkedStack public: LinkedStack():top(NULL) virtual LinkedStack()makeEmpty(); void Push(const T& x); bool
9、Pop(T& x); bool getTop(T& x)const; bool IsEmpty()constreturn(top=NULL)?true:false; int getSize(); void makeEmpty(); friend ostream& operator(ostream& os,SeqStack& s);private: LinkNode *top;templatevoid LinkedStack:makeEmpty() LinkNode *p; while(top!=NULL) p=top;top=top-link;delete p;templatevoid Lin
10、kedStack:Push(const T& x) top = new LinkNode(x,top); /assert(top!=NULL);templatebool LinkedStack:Pop(T& x) if(IsEmpty()=true) return false; struct LinkNode *p=top; top = top-link; x=p-data; delete p; return true;templatebool LinkedStack:getTop(T& x)const if(IsEmpty()=true) return false; x=top-data;
11、return true;templateint LinkedStack:getSize() LinkNode *p = top;int k = 0; while(top!=NULL) top = top-link;k+; return k;templateostream& operator(ostream& os,SeqStack& s) os栈中元素个数=s.getSize()endl; LinkNode *p = S.top; int i = 0; while(p!=NULL) os+i:dataendl;p=p-link; return os; 测试:#include #includeL
12、inkedStack.hvoid main() LinkedStack lsk; int x=2; lsk.Push(x); x=3; lsk.Push(x); int t = lsk.getSize();cout把2和3压栈后链栈内的元素个数为:tendlendl; lsk.makeEmpty(); t = lsk.getSize(); cout清空栈内元素后,栈内元素个数为:tendlendl; cout把1,3,5压栈进入栈内endl; x=1;lsk.Push(x);x=3;lsk.Push(x);x=5;lsk.Push(x); cout依次弹出栈内元素:endl; while(ls
13、k.IsEmpty()=false) lsk.Pop(x);coutx ; coutendlendl;4、实现括号匹配:#includeSeqStack.h#includeclass kuohao public: void PrintMatchedPairs(char *expression) SeqStack s(50); char j; int length=strlen(expression); for(int i=1;i=length;i+) if(expressioni-1=(|expressioni-1=|expressioni-1=) s.Push(expressioni-1);
14、 else if(expressioni-1=)|expressioni-1=|expressioni-1=) if(s.Pop(j)=true) if(expressioni-1=)&j=() coutj与expressioni-1匹配endl;j= ; else if(expressioni-1=&j=) coutj与expressioni-1匹配endl;j= ; else if(expressioni-1=&j=) coutj与expressioni-1匹配endl;j= ; else cout没有与第i-1个右括号匹配的左括号!endl; while(s.IsEmpty()=fals
15、e) s.Pop(j); cout没有与j左括号相匹配的右括号!endl; private: /const int maxLength=100;测试:#include #includeSeqStack.h#include kuohao.hvoid main() kuohao k; cout表达式1测试“()()”:endlendl; char *e1=()(); k.PrintMatchedPairs(e1); coutendlendl表达式2测试“()”:endlendl; char *e2=(); k.PrintMatchedPairs(e2);六、附录参考数据结构(用面向对象方法与C+语
16、言描述)附上SeqStack.h代码:#include#include#includestdlib.h templateclass SeqStack public: SeqStack(int sz=50); SeqStack()delete elements; void Push(const T& x); bool Pop(T& x); bool getTop(T& x)const; bool IsEmpty()constreturn (top=-1)?true:false; bool IsFull()constreturn (top=maxSize-1)?true:false; int ge
17、tSize()constreturn top+1; void MakeEmpty()top=-1; /void PrintMatchedPairs(char *expression); bool InPut(); bool OutPut()const;private: T *elements; int top; int maxSize; void overflowProcess();templateSeqStack:SeqStack(int sz):top(-1),maxSize(sz) elements=new TmaxSize; assert(elements!=NULL); templa
18、te void SeqStack:overflowProcess() int stackIncreament=maxSize; T *newArray = new TmaxSize + stackIncreament; if(newArray = NULL)cerr存储分配失败!endl;exit(1); for(int i = 0;i=top;i+)newArrayi=elementsi; maxSize=maxSize+stackIncreament; delete elements; elements=newArray; templatebool SeqStack:Pop(T &x) i
19、f(IsEmpty()=true)return false; x=elementstop-; return true; templatevoid SeqStack:Push(const T &x) if(IsFull()=true)overflowProcess(); elements+top=x; templatebool SeqStack:getTop(T &x)const if(IsEmpty()=true)return false; x=elementstop; return true; templatebool SeqStack:InPut() T val; cinval; Pop(val); return true; templatebool SeqStack:OutPut()const coutelementstopendl; return true;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1