数据结构栈与队列 源代码.docx
- 文档编号:30484954
- 上传时间:2023-08-15
- 格式:DOCX
- 页数:25
- 大小:134.81KB
数据结构栈与队列 源代码.docx
《数据结构栈与队列 源代码.docx》由会员分享,可在线阅读,更多相关《数据结构栈与队列 源代码.docx(25页珍藏版)》请在冰豆网上搜索。
数据结构栈与队列源代码
实验二栈与队列
姓名:
班级:
学号:
日期:
1、实验目的:
2、实验内容:
3、基本思想,原理和算法描述:
4、源程序:
(1)栈的基本操作及应用:
#include
#include
#include
#include
#defineERROR0
//#defineINFREASIBLE-1
#defineOVERFLOW-2
#defineSIZE100//存储空间初始分配量
//#defineINCREMENT10//存储空间分配增量
typedefintStatus;
#defineTRUE1
#defineFLASE0
#defineOK1
typedefstruct
{
int*top;//栈顶指针
int*base;//栈底指针
intstacksize;//当前已分配的存储空间
}SqStack;
StatusInitStack(SqStack*s)//初始化
{
s->base=(int*)malloc(SIZE*sizeof(int));
if(!
s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=SIZE;
returnOK;
}
StatusClearStack(SqStack*s)//置空
{
s->top=s->base;
returnOK;
}
StatusDestroyStack(SqStack*s)//销毁
{
ClearStack(s);
free(s->base);
returnOK;
}
StatusStackEmpty(SqStack*s)//判断是否为空
{
if(s->top==s->base)
{
returnTRUE;
}
else
{
returnFLASE;
}
}
StatusGetTop(SqStack*s,int*e)//取栈顶
{
//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
if(s->top==s->base)
{
returnERROR;
}
*e=*(s->top-1);
returnOK;
}
StatusPush(SqStack*s,inte)//进栈
{
//插入元素e为新的栈顶元素
if(s->top-s->base>=s->stacksize)
{
//栈满,追加存储空间
s->base=(int*)malloc(SIZE*sizeof(int));
if(!
s->base)
exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=SIZE;
}
*(s->top)++=e;
returnOK;
}
StatusPop(SqStack*s,int*e)//出栈
{
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
if(s->base==s->top)
returnERROR;
*e=*--s->top;
returnOK;
}
StatusStackTraverse(SqStack*s)//遍历
{
while(s->top!
=s->base)
{
printf("%d\t",*--s->top);
}
returnOK;
}
StatusConversion(SqStack*s,intN)//进制转换
{
intm;
printf("输入R进制");
scanf("%d",&m);
inte;
while(N){
Push(s,N%m);
N=N/m;
}
while(s->top!
=s->base)
{
Pop(s,&e);
printf("%d",e);
}
returnOK;
}
intmain()
{
SqStacksq;
InitStack(&sq);
inte;
intN;
intk;
intn=0;
Z:
{
printf("\n\t********************************************");
printf("\n\t***请你输入相应的操作序号进行操作***");
printf("\n\t***1.初始化***");
printf("\n\t***2.置空***");
printf("\n\t***3.销毁***");
printf("\n\t***4.是否空***");
printf("\n\t***5.取栈顶元素***");
printf("\n\t***6.进栈***");
printf("\n\t***7.出栈***");
printf("\n\t***8.遍历***");
printf("\n\t***9.进制转换***");
printf("\n\t***0.退出***\n");
printf("\t********************************************");
printf("\n请选择功能:
");
scanf("%d",&k);
switch(k){
case1:
InitStack(&sq);
gotoZ;
break;
case2:
ClearStack(&sq);
gotoZ;
break;
case3:
DestroyStack(&sq);
gotoZ;
break;
case4:
if(StackEmpty(&sq)){
printf("该栈为空!
");
}
else
{
printf("该栈非空!
");
}
gotoZ;
break;
case5:
GetTop(&sq,&e);
printf("栈顶元素为:
%d",e);
gotoZ;
break;
case6:
printf("请输入要进栈的元素:
");
scanf("%d",&e);
Push(&sq,e);
gotoZ;
break;
case7:
Pop(&sq,&e);
printf("%d",e);
gotoZ;
break;
case8:
StackTraverse(&sq);
gotoZ;
break;
case9:
printf("请输入要转换的十进制数字:
");
scanf("%d",&N);
printf("转换后R进制的数字为:
");
Conversion(&sq,N);
gotoZ;
break;
case0:
exit(0);
break;
default:
break;
}
}
}
(2)队列的基本操作:
#include
#include
usingnamespacestd;
#defineTRUE1
#defineFLASE0
#defineOK1
typedefstructQNode{
chardata;
structQNode*next;
}QNode,*Queue;
typedefstruct{
Queuefront;
Queuerear;
}LinkQueue;
voidInitQueue(LinkQueue&Q)//创造空队
{
Q.front=Q.rear=(Queue)malloc(sizeof(QNode));
if(!
Q.front)
{
cout<<"OVERFLOW"< } Q.front->next=NULL; //returnOK; } voidEnQueue(LinkQueue&Q,inte)//入队 { Queuep=(Queue)malloc(sizeof(QNode)); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; //returnOK; } voidDeQueue(LinkQueue&Q,inte)//出队 { Queuep=(Queue)malloc(sizeof(QNode)); if(Q.front==Q.rear) { cout<<"队空"< } p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); cout< } intlength(LinkQueue&Q)//求队列中元素个数 { Queuep=(Queue)malloc(sizeof(QNode)); p=Q.front->next; inti=0; while(p! =NULL) { p=p->next; i++; } cout<<"队列中元素个数: "< cout< return1; } intQueueEmpty(LinkQueue&Q)//判队列空 { if(Q.front==Q.rear) returnTRUE; else returnFLASE; } intmain() { LinkQueueQ; intk; Z: { cout<<"\n\t********************************************"< cout<<"\n\t***请你输入相应的操作序号进行操作***"< cout<<"\n\t***1.初始化***"< cout<<"\n\t***2.入队***"< cout<<"\n\t***3.出队***"< cout<<"\n\t***4.求队列中元素个数***"< cout<<"\n\t***5.判队列是否为空***"< cout<<"\n\t********************************************"< cout<<"\n请选择功能: "< cin>>k; switch(k) { case1: InitQueue(Q); gotoZ; break; case2: cout<<"请输入要插入的元素"< inte; cin>>e; EnQueue(Q,e); gotoZ; break; case3: cout<<"出队"< intm; DeQueue(Q,m); gotoZ; break; case4: length(Q); gotoZ; break; case5: if(QueueEmpty(Q)) cout<<"队列为空"< else cout<<"队列不为空"< gotoZ; break; default: break; } } } (3)判断回文: #include #include #include constintSTACK_INIT_SIZE=100;//初始分配的长度 constintSTACKINCREMENT=10;//分配内存的增量 //链式队列结构的定义 typedefcharElemType; typedefstructNode { chardata;//元素数据 structNode*next;//链式队列中结点元素的指针 }QNode,*QueuePtr; typedefstruct { QueuePtrfront;//队列头指针 QueuePtrrear;//队列尾指针 }LinkQueue; //栈结构的定义 typedefstructStack { ElemType*base; ElemType*top; intstacksize; }SqStack; boolInitQueue(LinkQueue*Q) { Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); if(! Q->front) { exit(0); } Q->front->next=NULL; returntrue; } boolEnQueue(LinkQueue*Q,ElemTypee) { QueuePtrp=(QueuePtr)malloc(sizeof(QNode)); if(! p) { exit(0); } p->data=e; p->next=NULL; Q->rear->next=p; Q->rear=p; returntrue; } boolDeQueue(LinkQueue*Q,ElemType*e) { if(Q->front==Q->rear) { returnfalse; } QueuePtrp=Q->front->next; *e=p->data; Q->front->next=p->next; if(Q->rear==p) { Q->rear=Q->front; } free(p); returntrue; } boolInitStack(SqStack*S) { S->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(S->base==NULL) { returnfalse; } S->top=S->base; S->stacksize=STACK_INIT_SIZE; returntrue; } boolPush(SqStack*S,ElemTypee) { if(S->top-S->base>=S->stacksize) { S->base=(ElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); if(! S->base) { returnfalse; } S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *(S->top++)=e; returntrue; } boolPop(SqStack*S,ElemType*e) { if(S->top==S->base) returnfalse; *e=(*--S->top); returntrue; } intmain() { //声明一个栈一个队列 SqStackS; LinkQueueL; //初始化一个栈一个队列 InitStack(&S); InitQueue(&L); charc[30]; ElemTypea,b; printf("请输入要判断的字符,以@结束: "); scanf("%s",c); inti=0; intflag1=0; intflag2=0; while(c[i]! ='@') { Push(&S,c[i]);//入栈 EnQueue(&L,c[i]);//入队列 flag1++; i++; } while(! (S.top==S.base)) { Pop(&S,&b);//出栈 DeQueue(&L,&a);//出队列 if(a==b) { flag2++; } else { break; } } if(flag1==flag2) { printf("Right\n\n"); } else { printf("Wrong\n\n"); } system("pause"); return0; } 5、运行结果分析: 栈的基本操作及应用 图1: 栈 图2: 进栈 图3: 出栈 图4: 读取栈顶元素 图5: 判断是否为空 图6: 数值的进制转换 队列的基本操作 图7: 队列 图8: 入队 图9: 求队列中的元素个数 图10: 出队列 图11: 判断是否为空 判断回文 图12: 判断回文 6、实验总结:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 栈与队列 源代码 队列