栈栈的应用队列优先队列.ppt
《栈栈的应用队列优先队列.ppt》由会员分享,可在线阅读,更多相关《栈栈的应用队列优先队列.ppt(101页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、n n栈n n栈的应用n n队列 n n优先队列a1a2a3a4a5a6插入 xi删除 xj插入删除栈栈(Stack)栈和队列 都是特殊的线性表 限定性的线性表 操作受限的线性表一、栈限定只在表的一端访问的线性表元素只能从栈顶插入和删除先进后出 后进先出 例 羊肉串 子弹夹 货 栈 top top A B C D top A B C D顺序栈的模型 top B A C D top C B A D top D C B A top C B A D top B A C D top A B C D top A B C D top top A B C D top A B C D top B A C Dt
2、op B A C Dtop A B C D top A B C D top A B D C top D A B C top C D A B 顺序栈顺序栈栈类的顺序表示栈类的顺序表示#ifndef STACK_CLASS#define STACK_CLASS#include#include const int MaxStackSize=50;template class Stack T stacklistMaxStackSize;int top;public:Stack(void);void Push(const T&item);T Pop(void);void ClearStack(void)
3、;T Peek(void)const;/gettop int StackEmpty(void)const;int StackFull(void)const;/initialize stack top.templateStack:Stack(void):top(-1)/push item on the the stacktemplatevoid Stack:Push(const T&item)/if stacklist is full,terminate the program if(top=MaxStackSize-1)cerr Stack overflow!endl;exit(1);/inc
4、rement top and copy item to stacklist top+;stacklisttop=item;/pop the stack and return the top elementtemplateT Stack:Pop(void)T temp;/if stack is empty,terminate the program if(top=-1)cerr Attempt to pop an empty stack!endl;exit(1);temp=stacklisttop;/record the top element top-;return temp;/return
5、the value at the top of the stacktemplateT Stack:Peek(void)const /if the stack is empty,terminate the program if(top=-1)cerr Attempt to peek at an empty stack!endl;exit(1);return stacklisttop;/test for an empty stacktemplateint Stack:StackEmpty(void)const /return the logical value top=-1 return top=
6、-1;/test for a full stacktemplateint Stack:StackFull(void)const /test the position of top return top=MaxStackSize-1;/clear all items from the stacktemplatevoid Stack:ClearStack(void)top=-1;#endif /STACK_CLASS链栈栈的链式表示线性链表类的定义#include node.htemplate class LinkedList Node*front,*rear,*prevPtr,*currPtr;
7、int size;int position;Node*GetNode(const T&item,Node*ptrNext=NULL);void FreeNode(Node*p);void CopyList(const LinkedList&L);public:LinkedList(void);LinkedList(const LinkedList&L);LinkedList(void);LinkedList&operator=(const LinkedList&L);int ListSize(void)const;int ListEmpty(void)const;void Reset(int
8、pos=0);void Next(void);int EndOfList(void)const;int CurrentPosition(void)const;void InsertFront(const T&item);void InsertRear(const T&item);void InsertAt(const T&item);void InsertAfter(const T&item);T DeleteFront(void);void DeleteAt(void);T&Data(void);void ClearList(void);链栈类的定义#ifndef STACK_CLASS#d
9、efine STACK_CLASS#include#include#include link.htemplate class Stack LinkedList stackList;public:Stack(void);/constructor void Push(const T&item);T Pop(void);T Peek(void);int StackEmpty(void)const;void ClearStack(void);/constructortemplate Stack:Stack(void)/uses the LinkedList method/ClearList to cl
10、ear the stacktemplate void Stack:ClearStack(void)stackList.ClearList();/use the LinkedList method/InsertFront to push itemtemplate void Stack:Push(const T&item)stackList.InsertFront(item);/use the LinkedList method DeleteFront to pop stacktemplate T Stack:Pop(void)/check for an empty linked list if(
11、stackList.ListEmpty()cerr Popping an empty stack endl;exit(1);/delete first node and return its data value return stackList.DeleteFront();/returns the data value of the first first item on the stacktemplate T Stack:Peek(void)/check for an empty linked list if(stackList.ListEmpty()cerr Calling Peek f
12、or an empty stack endl;exit(1);/reset to the front of linked list and return value stackList.Reset();return stackList.Data();/use the LinkedList method/ListEmpty to test for empty stacktemplate int Stack:StackEmpty(void)const return stackList.ListEmpty();#endif /STACK_CLASS二、二、栈的应用回文验证数制转换表达式求值括号匹配检
13、验行编辑程序递归实现回文验证palindrome回文的例子 dad madam sees madam im adam a man a plan a canal panama#include#pragma hdrstop#include astack.h/creates a new string with all blank characters removedvoid Deblank(char*s,char*t)/loop through expression until NULL character is found while(*s!=NULL)/if character is a non
14、-blank,copy to new string if(*s!=)*t+=*s;s+;/move to next character *t=NULL;/append NULL to new stringvoid main(void)const int True=1,False=0;Stack S;/stack elements are characters char palstring80,deblankstring80,c;int i=0;int ispalindrome=True;/assume string is a palindrome cin.getline(palstring,8
15、0,n);/read in the full-line string /remove blanks from string and put result in deblankstringDeblank(palstring,deblankstring);/push the chars of deblanked expression on the stack i=0;while(deblankstringi!=0)S.Push(deblankstringi);i+;i=0;while(!S.StackEmpty()c=S.Pop();/get next character from stack i
16、f(c!=deblankstringi)ispalindrome=False;break;i+;if(ispalindrome)cout palstring is a palindrome endl;else cout palstring is not a palindrome endl;/*madam im adammadam im adam is a palindromea man a plan a canal panamaa man a plan a canal panama is a palindromepalindromepalindrome is not a palindrome*
17、/数制转换输入十进制数,以其他进制输出#include#pragma hdrstop#include astack.h/print integer num in base Bvoid MultibaseOutput(long num,int B)/stack holds base B digits left to right Stack S;/extract base B digits right to left and push on stack S do S.Push(int(num%B);/convert to int and push on stack num/=B;/remove r
18、ight most digit while(num!=0);/continue until all digits computed while(!S.StackEmpty()/flush the stack cout S.Pop();void main(void)long num;/decimal number int B;/base /read 3 positive numbers and the desired base 2=B=9 for(int i=0;i 3;i+)cout Enter non-negative decimal number and base (2=B num B;c
19、out num base B is;MultibaseOutput(num,B);cout endl;/*Enter non-negative decimal number and base(2=B=9):72 472 base 4 is 1020Enter non-negative decimal number and base(2=B=9):53 253 base 2 is 110101Enter non-negative decimal number and base(2=B=9):3553 83553 base 8 is 6741*/表达式求值表达式求值中缀表达式a+b*c a+b*(
20、c-d)+e/f(b*b-4*a*c)/(2*a)后缀表达式abc*+abcd-*+ef/+bb*4a*c*-2a*/后缀表达式求值/calc.h#include#include#pragma hdrstopenum Boolean False,True;#include astack.hclass CalculatorStack S;/holds operands void Enter(double num);Boolean GetTwoOperands(double&opnd1,double&opnd2);void Compute(char op);public:Calculator(vo
21、id);/evaluate an expression and clear calculator void Run(void);void Clear(void);/store data value on the stackvoid Calculator:Enter(double num)S.Push(num);Boolean Calculator:GetTwoOperands(double&opnd1,double&opnd2)if(S.StackEmpty()/check for presence of operand cerr Missing operand!endl;return Fal
22、se;opnd1=S.Pop();/fetch right-hand operand if(S.StackEmpty()cerr Missing operand!endl;return False;opnd2=S.Pop();/fetch left-hand operand return True;void Calculator:Compute(char op)Boolean result;double operand1,operand2;result=GetTwoOperands(operand1,operand2);if(result=True)switch(op)case+:S.Push
23、(operand2+operand1);break;case-:S.Push(operand2-operand1);break;case*:S.Push(operand2*operand1);break;case/:if(operand1=0.0)cerr Divide by 0!c,c!=)/read until=(Quit)switch(c)case+:case-:case*:case/:case:Compute(c);break;default:/not operator,must be operand;put char back cin.putback(c);/read the ope
24、rand and store it on the stack cin newoperand;Enter(newoperand);break;if(!S.StackEmpty()cout S.Peek()endl;void Calculator:Clear(void)S.ClearStack();#include calc.hvoid main(void)Calculator CALC;CALC.Run();/*8 8*6 6*+.5 =103 4+*Missing operand!3 4+8*=561 0/=Divide by 0!*/中缀表达式求值定义表达式等级为表达式中每个元素赋一个等级表
25、达式的累计等级必须为0或1整个表达式的等级为1 项等级数1正负+-0运算+-*/-1左右括号()0 5+3*-6读到负号时等级累计-1,出错5+3-最后等级0,出错 中缀表达式求值法用两个栈:一个存放操作数 一个存放运算符2+3-4*5=32+操作数读入“-”弹出两个-优先级与+相同操作数运算得到结运算符弹出+果6入栈6-操作数运算符“-”入栈54*6-操作数4入栈5入栈“*”优先级高于“-”运算符“*”入栈206-操作数弹出“*”20入栈弹出5 4运算运算符206-操作数弹出“-”-14入栈弹出20 6运算运算符-14操作数弹出结果-14运算符定义运算符优先级icp 栈外优先级 in com
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 应用 队列 优先
限制150内