栈和队列的基本操作实现及其应用.docx
- 文档编号:28069189
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:21
- 大小:18.29KB
栈和队列的基本操作实现及其应用.docx
《栈和队列的基本操作实现及其应用.docx》由会员分享,可在线阅读,更多相关《栈和队列的基本操作实现及其应用.docx(21页珍藏版)》请在冰豆网上搜索。
栈和队列的基本操作实现及其应用
实验二栈和队列的基本操作实现及其应用
一_一、实验目的
1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。
一_二、实验内容
题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。
所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。
相关常量及结构定义:
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstructSqStack
{SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
设计相关函数声明:
判断函数:
intIsReverse()
栈:
intInitStack(SqStack&S)
intPush(SqStack&S,SElemTypee)
intPop(SqStack&S,SElemType&e)
intStackEmpty(s)
一_三、数据结构与核心算法的设计描述
1、初始化栈
/*函数功能:
对栈进行初始化。
参数:
栈(SqStackS)。
成功初始化返回0,否则返回-1*/
intInitStack(SqStack&S)
{
S.base=(SElemType*)malloc(10*sizeof(SElemType));
if(!
S.base)//判断有无申请到空间
return-1;//没有申请到内存,参数失败返回-1
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
S.base=newSElemType;
return0;
}
2、判断栈是否是空
/*函数功能:
判断栈是否为空。
参数;栈(SqStackS)。
栈为空时返回-1,不为空返回0*/
intStackEmpty(SqStackS)
{
if(S.top==S.base)return-1;
elsereturn0;
}
3、入栈
/*函数功能:
向栈中插入元素。
参数;栈(SqStackS),元素(SElemtypee)。
成功插入返回0,否则返回-1*/
intPush(SqStack&S,SElemTypee)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+1)*sizeof(SElemType));
//重新分配空间
if(!
S.base)return-1;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;//插入操作
return0;
}
4、出栈
/*函数功能:
在栈中删除元素。
参数;栈(SqStackS),元素(SElemtypee)。
成功删除返回0,否则返回-1*/
intPop(SqStack&S,SElemType&e)
{
if(S.top==S.base)return-1;
e=*--S.top;//删除操作
return0;
}
5、判断是否为回文
/*函数功能:
判断栈中的字符串是否为回文。
参数;栈(SqStackS)。
是回文时返回1,否则返回0*/
intIsReverse(SqStack&S)
{
inti;
chara;
for(i=0;i { Pop(S,a); if(a! =b[i])return0; } return1; } 一_四、函数的调用 主函数主要设计: intlpp; charch; SqStackp; InitStack(p); cout<<"请输入字符: "; while((ch=cin.get())&&ch! ='@') { Push(p,ch); b[j]=ch; j++; } if(StackEmpty(p)==-1) { cout<<"此为空栈"< return0; } lpp=IsReverse(p); if(lpp==0)cout<<"此字符串不是回文。 "< elsecout<<"此字符串是回文。 "< 一_五、实验总结 通过这次试验我熟悉了对栈的基本操作,对基本的栈操作有了很好的掌握,知道自己容易在什么地方出错,。 一_六、程序清单 #include usingnamespacestd; #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 charb[STACK_INIT_SIZE+STACKINCREMENT]; intj=0; typedefcharSElemType; typedefstructSqStack {SElemType*base; SElemType*top; intstacksize; }SqStack; intInitStack(SqStack&S) { S.base=(SElemType*)malloc(10*sizeof(SElemType)); if(! S.base)return-1; S.top=S.base; S.stacksize=STACK_INIT_SIZE; S.base=newSElemType; return0; } intStackEmpty(SqStackS) { if(S.top==S.base)return-1; elsereturn0; } intPush(SqStack&S,SElemTypee) { if(S.top-S.base>=S.stacksize) { S.base=(SElemType*)realloc(S.base,(S.stacksize+1)*sizeof(SElemType)); if(! S.base)return-1; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return0; } intPop(SqStack&S,SElemType&e) { if(S.top==S.base)return-1; e=*--S.top; return0; } intIsReverse(SqStack&S) { inti; chara; for(i=0;i { Pop(S,a); if(a! =b[i])return0; } return1; } intmain() { intlpp; charch; SqStackp; InitStack(p); cout<<"请输入字符: "; while((ch=cin.get())&&ch! ='@') { Push(p,ch); b[j]=ch; j++; } if(StackEmpty(p)==-1) { cout<<"此为空栈"< return0; } lpp=IsReverse(p); if(lpp==0)cout<<"此字符串不是回文。 "< elsecout<<"此字符串是回文。 "< return0; } 二_一、实验目的 2、会用栈和队列解决简单的实际问题。 二_二、实验内容 题目二、编程模拟队列的管理,主要包括: 出队列、入队、统计队列的长度、查找队列某个元素e、及输出队列中元素。 相关常量及结构定义: typedefintQElemType; typedefstructQNode { QElemTypedata; structQNode*next; }QNode,*QueuePtr; typedefstruct { QueuePtrfront; QueuePtrrear; intcount; }LinkQueue; 设计相关函数声明: InitQueue(LinkQueue&Q) EnQueue(LinkQueue&Q,QElemTypee) DeQueue(LinkQueue&Q,QElemType&e) QueueLength(LinkQueueQ) QueueTraverse(LinkQueueQ) QueueFind(LinkQueueQ,QElemTypee) 二_三、数据结构与核心算法的设计描述 1、初始化队列 intInitQueue(LinkQueue&Q) { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(! Q.front)return-1; Q.front->next=NULL; return0; } 2、入队列 intEnQueue(LinkQueue&Q,QElemTypee) { QueuePtrlpp; lpp=(QueuePtr)malloc(sizeof(QNode)); if(! lpp)return-1; lpp->data=e;lpp->next=NULL; if(Q.front==NULL) { Q.front->next=lpp; Q.rear=lpp; } else { Q.rear->next=lpp; Q.rear=lpp; } return0; } 3、出队列 intDeQueue(LinkQueue&Q,QElemType&e) { QueuePtrlpp; if(Q.front==Q.rear)return-1; lpp=Q.front->next; e=lpp->data; Q.front->next=lpp->next; if(Q.rear==lpp)Q.rear=Q.front; deletelpp; return0; } 4、统计队列的长度 intQueueLength(LinkQueueQ) { QueuePtrlpp=Q.front; inti=0; while(lpp! =Q.rear) { i++; lpp=lpp->next; } returni; } 5、查找队列的某个元素 intQueueFind(LinkQueueQ,QElemTypee) { QueuePtrp; p=Q.front->next; while(p) { if(p->data==e) return1; p=p->next; } return0; } 6、遍历队列 intQueueTraverse(LinkQueueQ) { QueuePtrp; p=Q.front->next; while(p) { cout< p=p->next; } cout< return0; } 7、主界面函数 voidzhujiemian() { cout< cout<<"【\t\t数据结构实验二】"< cout<<"【\t\t---------------------------------------------------------------------】"< cout<<"【\t\t1队列初始化】"< cout<<"【\t\t2出队列】"< cout<<"【\t\t3入队列】"< cout<<"【\t\t4队列长度】"< cout<<"【\t\t5在队列中查找元素】"< cout<<"【\t\t6遍历队列】"< cout<<"【\t\t其他键退出】"< cout<<"【\t\t---------------------------------------------------------------------】"< cout<<"【\t\t请选择要进行操作的序号(1--6)】: "; } 二_四、函数调用及主函数设计 主函数主要涉及: LinkQueueQ; inta,b,c; zhujiemian(); cin>>a; while(a! =1) { cout<<"输入错误,必须先初始化,请重新输入: "; cin>>a; } cout< do { switch(a) { case1: if(InitQueue(Q)==0) cout<<"初始化成功! "< else cout<<"初始化失败! "< break; case2: if(QueueLength(Q)==0) { cout<<"队列为空无法出队! "< break; } if(DeQueue(Q,c)==0) cout<<"删除成功! "< else cout<<"删除失败! "< break; case3: cout<<"输入你要入队元素"< cin>>c; if(EnQueue(Q,c)==0) cout<<"入队成功! "< else cout<<"入队失败! "< break; case4: b=QueueLength(Q); cout<<"队列的长度为: "< break; case5: cout<<"您要查找的元素: "; cin>>b; if(QueueFind(Q,b)==1) cout<<"恭喜您,队列中有您要找的元素"< else cout<<"不好意思,队列中没有您要找的元素"< break; case6: QueueTraverse(Q); break; default: break; } zhujiemian(); cin>>a; cout< }while(a>0&&a<=6); 说明: 通过调用序列号不同的函数进行各种操作。 函数根据每次输入的数进行判断不在1—6内的函数将结束,否则将继续进行。 二_五、程序调试及运行结果分析 程序第一步必须执行初始化,否则程序不能运行。 在程序第一步必须执行初始化后,程序完美运行,在进行任何函数操作程序都是正常运行,而且本程序对插入和删除时进行错误检测如有的地方不可以插入,有点地方不能删除,如果队列为空时则程序会输出队列为空,并继续进行其他操作,大大减少了程序的bug。 二_六、程序清单 #include usingnamespacestd; typedefintQElemType; typedefstructQNode { QElemTypedata; structQNode*next; }QNode,*QueuePtr; typedefstruct { QueuePtrfront; QueuePtrrear; intcount; }LinkQueue; intInitQueue(LinkQueue&Q) { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(! Q.front)return-1; Q.front->next=NULL; return0; } intEnQueue(LinkQueue&Q,QElemTypee) { QueuePtrlpp; lpp=(QueuePtr)malloc(sizeof(QNode)); if(! lpp)return-1; lpp->data=e;lpp->next=NULL; if(Q.front==NULL) { Q.front->next=lpp; Q.rear=lpp; } else { Q.rear->next=lpp; Q.rear=lpp; } return0; } intDeQueue(LinkQueue&Q,QElemType&e) { QueuePtrlpp; if(Q.front==Q.rear)return-1; lpp=Q.front->next; e=lpp->data; Q.front->next=lpp->next; if(Q.rear==lpp)Q.rear=Q.front; deletelpp; return0; } intQueueLength(LinkQueueQ) { QueuePtrlpp=Q.front; inti=0; while(lpp! =Q.rear) { i++; lpp=lpp->next; } returni; } intQueueTraverse(LinkQueueQ) { QueuePtrp; p=Q.front->next; while(p) { cout< p=p->next; } cout< return0; } intQueueFind(LinkQueueQ,QElemTypee) { QueuePtrp; p=Q.front->next; while(p) { if(p->data==e) return1; p=p->next; } return0; } voidzhujiemian() { cout< cout<<"【\t\t数据结构实验二】"< cout<<"【\t\t---------------------------------------------------------------------】"< cout<<"【\t\t1队列初始化】"< cout<<"【\t\t2出队列】"< cout<<"【\t\t3入队列】"< cout<<"【\t\t4队列长度】"< cout<<"【\t\t5在队列中查找元素】"< cout<<"【\t\t6遍历队列】"< cout<<"【\t\t其他键退出】"< cout<<"【\t\t---------------------------------------------------------------------】"< cout<<"【\t\t请选择要进行操作的序号(1--6)】: "; } intmain() { LinkQueueQ; inta,b,c; zhujiemian(); cin>>a; while(a! =1) { cout<<"输入错误,必须先初始化,请重新输入: "; cin>>a; } cout< do { switch(a) { case1: if(InitQueue(Q)==0) cout<<"初始化成功! "< else cout<<"初始化失败! "< break; case2: if(QueueLength(Q)==0) { cout<<"队列为空无法出队! "< break; } if(DeQueue(Q,c)==0) cout<<"删除成功! "< else cout<<"删除失败! "< break; case3: cout<<"输入你要入队元素"< cin>>c; if(EnQueue(Q,c)==0) cout<<"入队成功! "< else cout<<"入队失败! "< break; case4: b=QueueLength(Q); cout<<"队列的长度为: "< break; case5: cout<<"您要查找的元素: "; cin>>b; if(QueueFind(Q,b)==1) cout<<"恭喜您,队列中有您要找的元素"< else cout<<"不好意思,队列中没有您要找的元素"< break; case6: QueueTraverse(Q); break; default: break; } zhujiemian(); cin>>a; cout< }while(a>0&&a<=6); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 队列 基本 操作 实现 及其 应用
![提示](https://static.bdocx.com/images/bang_tan.gif)