栈和队列的应用算法与数据结构课程设计.docx
- 文档编号:10818021
- 上传时间:2023-02-23
- 格式:DOCX
- 页数:25
- 大小:68.26KB
栈和队列的应用算法与数据结构课程设计.docx
《栈和队列的应用算法与数据结构课程设计.docx》由会员分享,可在线阅读,更多相关《栈和队列的应用算法与数据结构课程设计.docx(25页珍藏版)》请在冰豆网上搜索。
栈和队列的应用算法与数据结构课程设计
栈和队列的应用
一、问题描述
栈和队列是一种常见的数据结构,是两种非常重要的线性结构,也都是线性表,它们是操作受限的的线性表,有顺序栈、链式栈、链式队列和循环队列等形式。
它们广泛应用在各种软件系统中。
本题就是要用这些线性结构先完成基本的应用,如回文,逆置。
再编写一个简易的停车场管理系统,完成对车辆出入库的管理、停车时间的记录和管理费用的结算。
二、基本要求
1、选择顺序栈和链队列,完成回文判断、字符串的逆置;
2、选择链栈和循环队列,完成回文判断、字符串的逆置;
3、运用栈和队列,完成简易停车场管理系统,要求:
(1)车辆入库管理及时间记录;
(2)车辆出库管理、时间的记录及管理费用的结算;
(3)若停车场已满则车辆进入便车道等候。
三、测试数据
1、回文判断的测试数据:
abcbc@;
2、字符串逆置的测试数据:
abcdef;
3、停车场管理系统测试数据:
(1)输入A1、A2、A3实现车辆的入库及对便车道进行测试;
(2)输入D1对车辆出库及管理费用结算进行测试。
四、算法思想
1、
(1)定义顺序栈和链队列及关于它们的基本操作,如定义栈和队列、求栈和队列的长度、入栈出栈、入队列出队列等。
方便后面函数的调用,是实现程序的基石。
(链栈和循环队列也是如此)
2、
(1)编写函数Palindrome_Test来实现字符串的回文判断,回文是利用了栈的反序输出原则而队列则是顺序输出这个思想来实现的。
往栈和队列输入同一组字符,再将它们一起输出,这样栈输出的字符就与原来的顺序相反了,而队列输出的字符与原来的顺序仍然是一样的,这样比较它们输出的字符是否相等就可以判断是否是回文了。
是则输出1,不是则输出。
(2)编写函数nzhi来实现一段字符串的逆置。
逆置是通过栈的反序输出来实现的。
通过它将队列的一组字符串进行逆置。
将队列的字符串顺序入栈然后出栈。
这样得到的字符串与原来的字符串就逆置过来了。
3、
(1)定义车节点的类型及栈和队列的相关操作。
(2)用栈的操作编写一个停车场,队列的操作编写一个便车道。
(3)编写函数Carrival实现车辆入库管理,若车库满则进入便车道,这个函数是利用栈的入栈等操作来实现的,并利用“t=localtime(&car.t);”语句记录入库时间。
(4)编写函数Carleave实现车辆出库管理并利用“t=localtime(&car.t);”语句记录出库时间,计算车辆的管理费用。
若便车道内有车等候,则进入停车场,并记录此时时间。
主要用到了栈的入栈出栈及队列中的入队列等操作。
用“(car.t-car2.t)*charge”语句来计算车辆的管理费用。
五、模块划分。
1、定义栈和队列及其基本操作。
(1)voidInitStack(SqStack*S)
初始化栈
(2)intStackLength(SqStackS)
求栈的长度
(3)voidPush(SqStack*S,ElemTypee)及voidPop(SqStack*S,ElemType*e)
入栈及出栈
(4)voidInitQueue(LinkQueue*Q)
初始化队列
(5)intQueueLength(SqQueueQ)
求队列的长度
(6)intEnQueue(SqQueue*Q,ElemTypee)及voidDeQueue(LinkQueue*Q,ElemType*e)
入队列及出队列
2、intPalindrome_Test(SqStack*S,LinkQueueQ)
判断是否为回文,是则输入1,否则输出0。
3、voidnzhi(LinkQueueQ)
实现字符串的逆置。
4、typedefstructParkCar
定义车节点类型。
5、voidCarrival(LinkQueue*q,ParkCarcar)
实现车辆入库管理及记录时间的功能。
6、voidCarleave(LinkQueue*q,ParkCarcar)
实现车辆出库管理及时间记录,管理费用的计算的功能。
7、intmain()
主函数。
调用定义的函数进行操作及结果的输出。
六、数据结构//(ADT)
1、链栈typedefintElemType;
typedefstructSNode
{ElemTypeData;
structSNode*next;
}SNode*LinkStack;
循环队列typedefstruct
{ElemType*base;
intfront;
intrear;}SqQueue;
2、顺序栈typedefcharElemType;
typedefstruct
{ElemType*base;
ElemType*top;}SqStack;
链式队列typedefstructQNode
{ElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{QueuePtrfront;
QueuePtrrear;
}LinkQueue;
3、车节点
typedefstructParkCar
{charADmark;
intPlate;
time_tt;
}ElemType,ParkCar;
七、源程序
1.用顺序栈和链式队列实现逆置和回文
#include
#include
#defineN20
typedefcharElemType;
typedefstruct
{ElemType*base;
ElemType*top;}SqStack;
typedefstructQNode
{ElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{QueuePtrfront;
QueuePtrrear;
}LinkQueue;
voidInitStack(SqStack*S)
{S->base=(ElemType*)malloc(N*sizeof(ElemType));
if(!
S->base)exit(0);
S->top=S->base;}
voidDestroyStack(SqStack*S)
{free(S->base);}
voidClearStack(SqStack*S)
{S->top=S->base;}
intStackEmpty(SqStackS)
{if(S.top==S.base)
return1;
else
return0;}
intStackLength(SqStackS)
{returnS.top-S.base;}
voidGetTop(SqStackS,ElemType*e)
{if(S.top>S.base)*e=*(S.top-1);}
voidPush(SqStack*S,ElemTypee)
{if(S->top-S->base {*(S->top)=e;S->top++;} else printf("\n栈满");} voidPop(SqStack*S,ElemType*e) {if(S->top>S->base) S->top--; *e=*(S->top); } voidInitQueue(LinkQueue*Q) {Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); if(! (Q->front))exit(0); Q->front->next=NULL;} voidDestroyQueue(LinkQueue*Q) {while(Q->front) {Q->rear=Q->front->next; free(Q->front); Q->front=Q->rear;} } intQueueEmpty(LinkQueueQ) {if(Q.front==Q.rear) return1; else return0;} intQueueLength(LinkQueueQ) {QueuePtrp;intn=0; p=Q.front; while(p! =Q.rear) {n++;p=p->next;} returnn; } voidEnQueue(LinkQueue*Q,ElemTypee) {QueuePtrp; p=(QueuePtr)malloc(sizeof(QNode)); if(! p)exit(0); p->data=e;p->next=NULL; Q->rear->next=p; Q->rear=p;} voidDeQueue(LinkQueue*Q,ElemType*e) {QueuePtrp; if(Q->front! =Q->rear) {p=Q->front->next; *e=p->data; Q->front->next=p->next; if(Q->rear==p)Q->rear=Q->front; free(p);} } intPalindrome_Test(SqStack*S,LinkQueueQ) { charc,a,b; intflag=0; while((c=getchar()! ='@')) { Push(S,c);EnQueue(&Q,c);//同时使用栈和队列两种结构 } while(! StackEmpty(*S)) { Pop(S,&a);DeQueue(&Q,&b); if(a! =b) flag=1; } if(flag==0) return1; else return0; } /*****/ voidnzhi(LinkQueueQ) { charch;SqStackS; InitStack(&S); while(! QueueEmpty(Q)) { DeQueue(&Q,&ch); Push(&S,ch); } while(! StackEmpty(S)) { Pop(&S,&ch); EnQueue(&Q,ch); } while(! QueueEmpty(Q)) { DeQueue(&Q,&ch); printf("%c",ch); } } intmain() { SqStacks;LinkQueueq;charch[N];inti; InitStack(&s);InitQueue(&q); if(Palindrome_Test(&s,q)) printf("这是回文\n"); else printf("这不是回文\n"); fflush(stdin); for(i=0;i<6;i++) { scanf("%c",&ch[i]); EnQueue(&q,ch[i]);} nzhi(q); system("pause"); } 2.用链栈和循环队列实现逆置和回文 #include"stdlib.h" #include"stdio.h" #defineN20 /*定义链栈类型*/ typedefcharElemType; typedefstructSNode {ElemTypedata; structSNode*next; }SNode,*LinkStack; /*初始化链栈(没有头结点)*/ voidInitStack(LinkStack*top) {*top=NULL;} /*销毁链栈*/ voidDestroyStack(LinkStack*top) {LinkStackp; while(*top! =NULL) {p=*top; *top=(*top)->next; free(p);} } /*清空链栈*/ voidClearStack(LinkStack*top) {LinkStackp; while(*top! =NULL) {p=*top; *top=(*top)->next; free(p);} } /*判断顺栈是否为空*/ intStackEmpty(LinkStacktop) {if(top==NULL) return1; else return0;} /*求链栈长度*/ intStackLength(LinkStacktop) {LinkStackp;intn=0; p=top; while(p! =NULL) {n++;p=p->next;} returnn; } /*取链栈栈顶元素*/ voidGetTop(LinkStacktop,ElemType*e) {if(top! =NULL) *e=top->data;} /*入链栈*/ voidPush(LinkStack*top,ElemTypee) {LinkStacknew1; new1=(LinkStack)malloc(sizeof(SNode)); new1->data=e; new1->next=*top;*top=new1;} /*出链栈*/ voidPop(LinkStack*top,ElemType*e) {LinkStackp; if(*top! =NULL) {*e=(*top)->data; p=*top;(*top)=p->next; free(p);} } /*遍历链栈并输出: 栈顶向栈底方向输出*/ voidStackTraverse(LinkStacktop) {LinkStackp; printf("\nStack: "); p=top; while(p) {printf("\t%d",p->data); p=p->next;} } typedefstruct {ElemType*base; charfront; charrear;}SqQueue; voidInitQueue(SqQueue*Q) {Q->base=(ElemType*)malloc(N*sizeof(ElemType)); Q->front=Q->rear=0;} voidDestroyQueue(SqQueue*Q) {free(Q->base);} voidClearQueue(SqQueue*Q) {Q->front=Q->rear=0;} intQueueEmpty(SqQueueQ) {if(Q.front==Q.rear) return1; else return0; } intQueueLength(SqQueueQ) {return(Q.rear+N-Q.front)%N;} voidGetHead(SqQueueQ,ElemType*e) {if(Q.front! =Q.rear) *e=Q.base[Q.front];} intEnQueue(SqQueue*Q,ElemTypee) {if((Q->rear+1)%N==Q->front) return0; Q->base[Q->rear]=e; Q->rear=(Q->rear+1)%N; return1;} intDeQueue(SqQueue*Q,ElemType*e) {if(Q->front==Q->rear) return0; *e=Q->base[Q->front]; Q->front=(Q->front+1)%N; return1;} voidQueueTraverse(SqQueueQ) {inti; printf("\nQueue: "); if(Q.rear Q.rear=Q.rear+N; for(i=Q.front;i printf("%d\t",Q.base[i%N]);} intPalindrome_Test(LinkStack*S,SqQueueQ) { charc,a,b; intflag=0; c=getchar(); while((c! ='@')) { Push(S,c);EnQueue(&Q,c); c=getchar();//同时使用栈和队列两种结构 } while(! StackEmpty(*S)) {Pop(S,&a);DeQueue(&Q,&b); if(a! =b) flag=1; } if(flag==0) return1; else return0; } /*****/ voidnzhi(SqQueueQ) { charch;LinkStackS; InitStack(&S); while(! QueueEmpty(Q)) { DeQueue(&Q,&ch); Push(&S,ch); } while(! StackEmpty(S)) { Pop(&S,&ch); EnQueue(&Q,ch); } while(! QueueEmpty(Q)) { DeQueue(&Q,&ch); printf("%c",ch); } } intmain() { LinkStacks;SqQueueq;charch[N];inti; InitStack(&s);InitQueue(&q); printf("请输入一段字符串,并以@结尾\n"); if(Palindrome_Test(&s,q)) printf("这是回文\n"); else printf("这不是回文\n"); fflush(stdin); printf("\n对一段字符串进行逆置\n"); for(i=0;i<6;i++) { scanf("%c",&ch[i]); EnQueue(&q,ch[i]);} nzhi(q); system("pause"); } 3.停车场管理系统 #include #include #include #defineN2 #definecharge50 /*车节点类型*/ typedefstructParkCar {charADmark; intPlate; time_tt; }ElemType,ParkCar; typedefstruct {ElemType*base; ElemType*top;}SqStack; typedefstructQNode {ElemTypedata; structQNode*next; }QNode,*QueuePtr; typedefstruct {QueuePtrfront; QueuePtrrear; }LinkQueue; SqStacks; voidInitStack(SqStack*S) {S->base=(ElemType*)malloc(N*sizeof(ElemType)); if(! S->base)exit(0); S->top=S->base;} voidDestroyStack(SqStack*S) {free(S->base);} voidClearStack(SqStack*S) {S->top=S->base;} intStackEmpty(SqStackS) {if(S.top==S.base) return1; else return0;} intStackLength(SqStackS) {returnS.top-S.base;} voidGetTop(SqStackS,ElemType*e) {if(S.top>S.base)*e=*(S.top-1);} voidPush(SqStack*S,ElemTypee) {if(S->top-S->base {*(S->top)=e;S->top++;} else printf("\n栈满");} voidPop(SqStack*S,ElemType*e) {if(S->top>S->base) S->top--; *e=*(S->top);} voidInitQueue(LinkQueue*Q) {Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); if(! (Q->front))exit(0); Q->front->next=NULL;} voidDestroyQueue(LinkQueue*Q) {while(Q->front) {Q->rear=Q->front->next; free(Q->front); Q->front=Q->rear;}} intQueueEmpty(LinkQueueQ) {if(Q.front==Q.rear) return1; else return0;} intQueueLength(LinkQueueQ) {QueuePtrp;intn=0; p=Q.front; while(p! =Q.rear) {n++;p=p->next;} returnn;} voidEnQueue(LinkQueue*Q,ElemTypee) {QueuePtrp; p=(QueuePtr)malloc(sizeof(QNode)); if(! p)exit(0); p->data=e;p->next=NULL; Q->rear->next=p; Q->rear=p;} voidDeQueue(LinkQueue*Q,ElemType*e) {Qu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 队列 应用 算法 数据结构 课程设计