实验二 栈和队列及其应用.docx
- 文档编号:29820170
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:19
- 大小:17.04KB
实验二 栈和队列及其应用.docx
《实验二 栈和队列及其应用.docx》由会员分享,可在线阅读,更多相关《实验二 栈和队列及其应用.docx(19页珍藏版)》请在冰豆网上搜索。
实验二栈和队列及其应用
实验二栈和队列及其应用
09计算机1白杨0929210028
(一)栈操作的验证
1.问题描述
对于顺序栈或链栈的基本操作进行验证。
2.基本要求
考虑各种可能情况(包括溢出等)。
3.测试数据
自拟。
(二)队列操作的验证
1.问题描述
对于顺序队列或链队列的基本操作进行验证。
2.基本要求
考虑各种可能情况(包括溢出等)。
3.测试数据
自拟。
(三)队列元素倒置(选做,详见实验手册)
说明:
本实验最低要求为在4个学时内完成实验内容
(一)和
(二)中一种存储结构下的操作验证。
/*顺序栈操作验证*/
#include
#defineMAX100
#defineTURE1
#defineFALSE0
#defineERROR0
#defineOK1
typedefstruct
{
intdata[MAX];
inttop;
}sqstack;
intmenu(void)
{
intnum;
//clrscr();
printf("\n\n");
printf(" MENU \n");
printf(" ===========================\n");
printf(" operations\n");
printf(" 1.StackEmpty\n");
printf(" 2.push\n");
printf(" 3.printf\n");
printf(" 4.pop\n");
printf(" 5.gettop\n");
printf(" 6.init\n");
printf(" 0.exit \n");
printf(" ===========================\n");
printf(" Choice(0,1,2,3,4,5,6):
");
scanf("%d",&num);
returnnum;
}
voidInitStack(sqstack*s)
{
s->top=-1;
}
intStackEmpty(sqstack*s)
{
if(s->top==-1)returnTURE;
elsereturnFALSE;
}
intPush(sqstack*s,int&e)
{
if(s->top==MAX-1)returnERROR;
else
{
s->top++;
s->data[s->top]=e;
returnOK;
}
}
intPop(sqstack*s,int&e)
{
if(StackEmpty(s))returnERROR;
else
{
e=s->data[s->top];
s->top--;
returnOK;
}
}
intGetTop(sqstack*s,int&e)
{
if(StackEmpty(s))returnERROR;
else
{
e=s->data[s->top];
returnOK;
}
}
voidSqprintf(sqstack*s)
{
inti=0;
if(!
StackEmpty(s))
{
printf("TheStack'datais:
(fromflowtotop)\n");
for(;i<=s->top;i++)
printf("%d",s->data[i]);
}
elseprintf("Thestackempty");
}
intmain()
{
sqstackstack,*s;
intflag=1,x,e=0;
s=&stack;
InitStack(s);
do{
intnum=menu();
//clrscr();
switch(num)
{
case1:
if(StackEmpty(s))
printf("Thestackempty");
elseprintf("The!
stackempty");
break;
case2:
printf("inputthestackdata(endby0):
\n");
scanf("%d",&x);
while(x!
=0)
{
if(!
Push(s,x))printf("ERROR");
elsescanf("%d",&x);
}
break;
case3:
Sqprintf(s);
break;
case4:
printf("thepopdatais:
\n");
if(Pop(s,e))
printf("%d",e);
elseprintf("Error");
break;
case5:
if(!
GetTop(s,e)) printf("Error");
elseprintf("thetopdatais:
%d",e);
break;
case6:
InitStack(s);
break;
case0:
flag=0;
}
}while(flag==1);
return0;
}
/*测试数据
1
3
2
1230
1
3
5
2
980
5
3
4
5
3
4
5
3
0
*/
/*链栈操作验证*/
#include
#include
#defineMAX100
#defineERROR0
#defineOK1
#defineLENsizeof(structsnode)
typedefstructsnode
{
intdata;
structsnode*next;
}linkstack;
intmenu(void)
{
intnum;
//clrscr();
printf("\n\n");
printf(" MENU \n");
printf(" ===========================\n");
printf(" operations\n");
printf(" 1.push\n");
printf(" 2.pop\n");
printf(" 3.top\n");
printf(" 4.print\n");
printf(" 5.init\n");
printf(" 0.exit\n");
printf(" ===========================\n");
printf(" Choice(0,1,2,3,4):
");
scanf("%d",&num);
returnnum;
}
voidInit(linkstack*&top)
{
top=(linkstack*)malloc(LEN);
top->next=NULL;
}
intEmpty(linkstack*s)
{
if(s->next==NULL)
returnOK;
returnERROR;
}
voidPush(linkstack*&top,intx)
{
linkstack*s;
s=(linkstack*)malloc(LEN);
s->data=x;
s->next=top;
top=s;
}
intPop(linkstack*&top,int&x)
{
linkstack*s;
if(Empty(top))returnERROR;
x=top->data;
s=top;
top=top->next;
free(s);
returnOK;
}
intTop(linkstack*&top,int&x)
{
if(Empty(top))returnERROR;
x=top->data;
returnOK;
}
voidPrint(linkstack*s)
{
if(s->next==NULL)
printf("ERROR");
else
while(s->next!
=NULL)
{
printf("%d",s->data);
s=s->next;
}
}
intmain()
{
inte,x,flag=1;
linkstack*top,**s;
Init(top);
s=⊤
do{
intnum=menu();
//clrscr();
switch(num)
{
case1:
printf("inputanumbertopush:
e=");
scanf("%d",&e);
Push(top,e);
break;
case2:
if(!
Pop(top,x))
printf("ERROR");
elseprintf("thepopdatais:
%d\n",x);
break;
case3:
if(!
Top(top,x))
printf("ERROR");
elseprintf("thetopdatais:
%d\n",x);
break;
case4:
Print(*s);
break;
case5:
Init(top);
s=⊤
break;
case0:
flag=0;
}
}while(flag==1);
return0;
}
/*测试数据
1
2
1
3
3
2
3
4
5
1
2
1
3
1
4
4
0
*/
/*顺序队列操作验证*/
#include
#defineOK1
#defineERROR0
#defineMAX100
typedefstruct
{
int data[MAX];
intfront,rear;
}SqQueue;
intmenu(void)
{
intnum;
printf("\n\n");
printf(" MENU \n");
printf(" ===========================\n");
printf(" operations\n");
printf(" 1.EnQueue\n");
printf(" 2.DeQueue\n");
printf(" 3.printf\n");
printf(" 0.exit \n");
printf(" ===========================\n");
printf(" Choice(0,1,2,3):
");
scanf("%d",&num);
returnnum;
}
intEnQueue(SqQueue*sq,inte)
{
if((sq->rear+1)%MAX==sq->front)
{
printf("OVERFLOWE\n");
returnERROR;
}
else
{
sq->rear=(sq->rear+1)%MAX;
sq->data[sq->rear]=e;
returnOK;
}
}
intDeQueue(SqQueue*sq,int*e)
{
if(sq->rear==sq->front)
{
printf("EMPTY\n");
returnERROR;
}
else
{
sq->front=(sq->front+1)%MAX;
*e=sq->data[sq->front];
printf("thenumberofDeQueueis:
%d\n",*e);
returnOK;
}
}
voidprint(SqQueue*sq)
{
if(sq->rear==sq->front)
{
printf("EMPTY\n");
return;
}
while(sq->front!
=sq->rear)
{
printf("%d",sq->data[(sq->front+1)%MAX]);
sq->front=(sq->front+1)%MAX;
}
printf("\n");
}
intmain()
{
SqQueue*sq,a;
intx,*e,b,flag;
flag=1;sq=&a;e=&b;
sq->front=MAX-1;
sq->rear=MAX-1;
do{
intnum=menu();
switch(num)
{
case1:
printf("inputnumbertoEnQueue:
x=");
scanf("%d",&x);
EnQueue(sq,x);
break;
case2:
DeQueue(sq,e);
break;
case3:
print(sq);
break;
case0:
flag=0;
break;
}
}while(flag==1);
return0;
}
/*测试数据
1
1
1
2
1
3
1
4
1
5
2
2
2
3
0
*/
/*链队列操作验证*/
#include
#include
#defineLENsizeof(node)
typedefstructqnode
{
intdata;
structqnode*next;
}node;
typedefstruct
{
node*front,*rear;
}Lqueue;
intmenu(void)
{
intnum;
printf("\n\n");
printf(" MENU \n");
printf(" ===========================\n");
printf(" operations\n");
printf(" 1.EnQueue\n");
printf(" 2.DeQueue\n");
printf(" 3.printf\n");
printf(" 4.creat\n");
printf(" 0.exit \n");
printf(" ===========================\n");
printf(" Choice(0,1,2,3,4):
");
scanf("%d",&num);
returnnum;
}
voidcreatqueue(Lqueue*s)
{
node*p,*q;
intx;
p=q=(node*)malloc(LEN);
s->front=p;
scanf("%d",&x);
p->next=0;
while(x)
{
p->data=x;q->next=p;
q=p;
p=(node*)malloc(LEN);
scanf("%d",&x);
}
q->next=0;
s->rear=q;
}
voidEnQueue(Lqueue*s,intx)
{
node*q;
q=(node*)malloc(LEN);
q->data=x;
q->next=0;
if(s->front==0&&s->rear==0)
s->front=s->rear=q;
else
{
s->rear->next=q;
s->rear=q;
}
}
voidDeQueue(Lqueue*s)
{
node*q;
q=(node*)malloc(LEN);
q=s->front;
inte=q->data;
s->front=q->next;
free(q);
printf("thenumberofDeQueueis:
%d\n",e);
}
voidprint(Lqueue*s)
{
node*p;
p=s->front;
do
{
printf("%d",p->data);
p=p->next;
}while(p);
}
intmain()
{
Lqueue*s;
intx,flag=1;
s=(Lqueue*)malloc(LEN);
s->front=s->rear=0;
do{
intnum=menu();
switch(num)
{
case1:
printf("inputnumbertoEnQueue:
x=");
scanf("%d",&x);
EnQueue(s,x);
break;
case2:
DeQueue(s);
break;
case3:
print(s);
break;
case4:
printf("inputnumbertoEnQueueend(by=0):
\n");
creatqueue(s);
break;
case0:
flag=0;
break;
}
}while(flag==1);
return0;
}
/*测试数据
1
1
1
2
1
3
1
4
1
5
2
2
2
3
12340
2
2
3
0
*/
/*队列元素倒置*/
#include
#include
#defineMax100
typedefstruct
{
intdata[Max];
inttop;
}SeqStack;
typedefstruct
{
intdata[Max];
intrear,front;
}SeQueue;
intmain()
{
SeQueue*sq;
SeqStack*s;
intx1,x2;
inti,n;
sq=(SeQueue*)malloc(sizeof(SeQueue));
s=(SeqStack*)malloc(sizeof(SeqStack));
printf("Inputthenumberstoreverse:
n=");
scanf("%d",&n);
printf("Inputthennumbers:
\n");
for(i=0;i scanf("%d",&sq->data[i]); sq->front=-1; sq->rear=n-1; s->top=-1; while(sq->front! =sq->rear) { sq->front++; x1=sq->data[sq->front]; s->top++; s->data[s->top]=x1; } sq->rear=-1; sq->front=-1; while(s->top>=0) { x2=s->data[s->top]; s->top--; sq->rear++; sq->data[sq->rear]=x2; } for(i=0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验二 栈和队列及其应用 实验 队列 及其 应用