栈定义及其应用Word格式.docx
《栈定义及其应用Word格式.docx》由会员分享,可在线阅读,更多相关《栈定义及其应用Word格式.docx(22页珍藏版)》请在冰豆网上搜索。
使用递归的回溯法实现小型迷宫程序(提示:
程序分为两大模块,根据数据生成地图,漫游地图)
三、实验使用环境
VC++6.0控制台程序
四、实验关键代码与结果展示(部分截图)
1.顺序栈:
SeqStack.h
#include<
cassert>
#include<
iostream>
usingnamespacestd;
constintstackIncreament=20;
template<
typenameT>
classSeqStack{
public:
SeqStack(intsz=50);
~SeqStack(){
delete[]elements;
}
voidPush(constT&
x);
boolPop(T&
boolgetTop(T&
x)const;
boolIsEmpty()const{
returntop==-1;
boolIsFull()const{
returntop==maxSize-1;
intgetSize()const{
returntop+1;
voidMakeEmpty(){
top=-1;
friendostream&
operator<
<
(ostream&
out,SeqStack<
T>
&
s){
out<
"
栈顶指针位于:
<
s.top<
endl;
for(inti=0;
i<
=s.top;
i++)
out<
:
s.elements[i]<
returnout;
private:
T*elements;
inttop;
intmaxSize;
voidoverflowProcess();
};
SeqStack<
SeqStack(intsz){
top=-1;
maxSize=sz;
elements=newT[maxSize];
assert(elements);
}
voidSeqStack<
overflowProcess(){
T*newArray=newT[maxSize+stackIncreament];
assert(newArray);
for(inti=0;
=top;
i++)
newArray[i]=elements[i];
maxSize=maxSize+stackIncreament;
delete[]elements;
elements=newArray;
Push(constT&
x){
if(IsFull())overflowProcess();
elements[++top]=x;
boolSeqStack<
Pop(T&
if(IsEmpty()){
returnfalse;
x=elements[top--];
returntrue;
getTop(T&
x)const{
x=elements[top];
Main.cpp
#include"
SeqStack.h"
fstream>
string.h>
stdio.h>
intmain(){
SeqStack<
int>
sta;
ifstreamfin("
data.txt"
);
assert(fin);
intdata;
while(!
fin.eof()){
assert(fin>
>
data);
sta.Push(data);
inttishi();
intnum;
chary;
num=tishi();
if(num==1){
cout<
未操作前的顺序栈:
\n"
当前顺序栈的元素个数:
sta.getSize()<
sta.getTop(data);
当前的栈顶元素值为:
data<
main();
if(num==2){
while(true){
sta.Pop(data);
\n进行一次出栈操作:
sta<
出栈的元素值为:
cout<
"
\n继续出栈:
Y退出:
N:
;
cin>
y;
if(y=='
N'
||y=='
n'
)break;
if(num==3){
intx;
\n输入您要入栈的元素值:
x;
sta.Push(x);
\n当前顺序栈的元素个数:
入栈后的顺序栈:
sta;
\n继续入栈:
if(num==4){
if(sta.IsEmpty())cout<
栈空!
elseif(sta.IsFull())cout<
栈满!
elsecout<
栈目前处于正常状态!
执行清空栈操作:
sta.MakeEmpty();
main();
if(num==5){
charexpression[]="
{[()]}"
voidPrintMatchedPairs(char*expression);
PrintMatchedPairs(expression);
return0;
inttishi(){
===============================================================\n"
输入相应的操作序号:
1、打印顺序栈2、出栈3、入栈4、清空栈5、括号匹配:
num;
returnnum;
constintmaxLength=100;
voidPrintMatchedPairs(char*expression){
s(maxLength);
intj,length=strlen(expression);
for(inti=1;
i<
length;
i++){
if(expression[i-1]=='
('
)
s.Push(i);
elseif(expression[i-1]=='
)'
){
if(s.Pop(j)==true)cout<
j<
与"
匹配"
endl;
elsecout<
没有与第"
个右括号匹配的左括号!
}
while(s.IsEmpty()==false){
s.Pop(j);
cout<
个左括号匹配的右括号!
}
截图
2.链栈
LinkedStack.h
#ifndefLINKEDSTACK_H
#defineLINKEDSTACK_H
structStackNode{
Tdata;
StackNode<
*link;
StackNode(Td=0,StackNode<
*next=NULL):
link(next),data(d){}
classLinkedStack{
*top;
LinkedStack():
top(NULL){}
~LinkedStack(){
makeEmpty();
intgetSize()const;
returntop==NULL;
voidmakeEmpty();
os,LinkedStack<
os<
栈长度:
s.getSize()<
StackNode<
*p=s.top;
inti=0;
while(p){
os<
++i<
p->
data<
p=p->
link;
returnos;
voidLinkedStack<
makeEmpty(){
*p;
while(top){
p=top;
top=top->
deletep;
top=newStackNode<
(x,top);
assert(top);
boolLinkedStack<
*p=top;
top=top->
x=p->
data;
deletep;
x)const{
if(IsEmpty())returnfalse;
x=top->
intLinkedStack<
getSize()const{
intk=0;
while(p){
p=p->
k++;
returnk;
#endif
#include"
LinkedStack.h"
LinkedStack<
初始化链栈:
当前链栈的大小为:
当前栈顶元素为:
while(true){
\n出栈操作后的链栈:
出栈的元素为:
链栈已空!
链栈已满!
链栈目前处于正常状态!
sta.makeEmpty();
置空链栈:
1、打印顺序栈2、出栈3、入栈4、清空栈:
3.队列
SeqQueue.h
#ifndefSEQQUEUE_H
#defineSEQQUEUE_H
classSeqQueue{
SeqQueue(intsz=100);
~SeqQueue(){
delete[]elements;
boolEnQueue(constT&
//进队
boolDeQueue(T&
//出队
boolgetFront(T&
voidmakeEmpty(){
front=rear=0;
returnfront==rear;
return(rear+1)%maxSize==front;
return(rear-front+maxSize)%maxSize;
operator<
(ostream&
os,SeqQueue<
Q){
对头指针位置="
Q.front<
队尾指针位置="
Q.rear<
for(inti=Q.front;
i!
=Q.rear;
i=(i+1)%Q.maxSize){
【"
】:
Q.elements[i]<
}
队列长度:
Q.getSize()<
protected:
intrear,front;
SeqQueue<
SeqQueue(intsz){
front=0;
rear=0;
assert(elements!
=NULL);
boolSeqQueue<
EnQueue(constT&
if(IsFull())returnfalse;
elements[rear]=x;
rear=(rear+1)%maxSize;
DeQueue(T&
x=elements[front];
front=(front+1)%maxSize;
getFront(T&
SeqQueue.h"
SeqQueue<
que;
intdata,x,num;
que.EnQueue(data);
初始化队列:
que<
que.getFront(data);
输入你所要入队的元素值:
cin>
que.EnQueue(x);
cout<
新队列:
\n继续入队:
if(y=='
}main();
que.DeQueue(data);
删除对头元素:
\n继续出队:
===============================================