武汉理工数据结构实验2栈和队列基本操作和应用.docx

上传人:b****7 文档编号:9070669 上传时间:2023-02-03 格式:DOCX 页数:16 大小:17.01KB
下载 相关 举报
武汉理工数据结构实验2栈和队列基本操作和应用.docx_第1页
第1页 / 共16页
武汉理工数据结构实验2栈和队列基本操作和应用.docx_第2页
第2页 / 共16页
武汉理工数据结构实验2栈和队列基本操作和应用.docx_第3页
第3页 / 共16页
武汉理工数据结构实验2栈和队列基本操作和应用.docx_第4页
第4页 / 共16页
武汉理工数据结构实验2栈和队列基本操作和应用.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

武汉理工数据结构实验2栈和队列基本操作和应用.docx

《武汉理工数据结构实验2栈和队列基本操作和应用.docx》由会员分享,可在线阅读,更多相关《武汉理工数据结构实验2栈和队列基本操作和应用.docx(16页珍藏版)》请在冰豆网上搜索。

武汉理工数据结构实验2栈和队列基本操作和应用.docx

武汉理工数据结构实验2栈和队列基本操作和应用

实验2栈和队列的基本操作和应用

1实验目的

(1)熟练掌握顺序栈的基本操作。

(2)掌握顺序栈的应用。

(3)掌握顺序循环队列的基本操作。

(4)掌握链式队列的基本操作。

2实验内容

(1)设计一个顺序栈的基本操作的演示程序;

(2)利用顺序栈,进行整数的不同进制之间的转换;

(3)设计一个顺序循环队列的基本操作演示程序;

(4)设计一个链式队列的基本操作演示程序。

【基本要求】

I.实验内容

(1)的基本要求:

编写一个程序,将一个顺序栈的元素依次取出,并打印其元素值。

II.实验内容

(2)的基本要求:

编写一个程序,将一个非负的十进制整数转换成二进制。

III.实验内容(3)的基本要求:

编写一个程序,将一个顺序队列的元素依次取出,并打印其元素值。

IV.实验内容(4)的基本要求:

编写一个程序,将一个链式队列的元素依次取出,并打印其元素值。

【测试数据】自定

3实验结果

按照学校实验格式要求撰写实验报告,内容主要包括1)实验目的;2)实验内容;3)实验环境和方法;4)实验过程描述;5)实验心得体会

参考程序如下:

实验内容

(1)参考程序

/*sqStack.h文件*/

#defineINIT_SIZE100

#defineINCREMENT10

typedefintElemType;

//typedefcharElemType;

typedefstructSqStack{

ElemType*base;

ElemType*top;

intstacksize;

}SqStack;

enumStatus{

OK,

ERROR,

OVERFLOW

};

/*sqStackOp.h文件*/

#include"sqStack.h"

StatusInitStack(SqStack&S);

StatusGetTop(SqStackS,ElemType&e);

StatusPush(SqStack&S,ElemTypee);

StatusPop(SqStack&S,ElemType&e);

boolStackEmpty(SqStack&S);

/*sqStackOp.cpp文件*/

#include

#include

#include"sqStackOp.h"

StatusInitStack(SqStack&S){

//构造一个空的栈

S.base=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType));

if(!

S.base)exit(OVERFLOW);//存储分配失败

S.top=S.base;

S.stacksize=INIT_SIZE;

returnOK;

}//InitStack

StatusGetTop(SqStackS,ElemType&e){

//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR

if(S.top==S.base)returnERROR;

e=*(S.top-1);

returnOK;

}//GetTop

StatusPush(SqStack&S,ElemTypee){

//插入元素e为新的栈顶元素

if(S.top-S.base>=S.stacksize){//栈满,追加存储空间

S.base=(ElemType*)realloc(S.base,(S.stacksize+INCREMENT)*sizeof(ElemType));

if(!

S.base)exit(OVERFLOW);//存储分配失败

S.top=S.base+S.stacksize;

S.stacksize+=INCREMENT;

}

*S.top++=e;

returnOK;

}//Push

StatusPop(SqStack&S,ElemType&e){

//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR

if(S.top==S.base)returnERROR;

e=*(--S.top);

returnOK;

}//Push

//判断栈是否为空

boolStackEmpty(SqStack&S){

if(S.top==S.base)

returntrue;

else

returnfalse;

}

/*main.cpp文件*/

#include

#include

#include"sqStackOp.h"

voidmain()

{

printf("Hellowstack\n");

SqStackS;//定义顺序栈S

if(OK!

=InitStack(S)){

printf("顺序栈初始化出错,退出....\n");

exit(-1);

}

Push(S,1);

Push(S,2);

Push(S,3);

inte;

Pop(S,e);

printf("出栈元素=%d\n",e);

Push(S,4);

Push(S,5);

while(!

StackEmpty(S)){

Pop(S,e);

printf("出栈元素=%d\n",e);

}

/*

SqStackS;charx,y;

InitStack(S);x='c';y='k';

Push(S,x);Push(S,'a');Push(S,y);

Pop(S,x);Push(S,'t');Push(S,x);

Pop(S,x);Push(S,'s');

while(!

StackEmpty(S)){Pop(S,y);printf("%c",y);};

printf("%c",x);

*/

getchar();

}

实验内容

(2)参考程序

/*sqStack.h文件*/

#defineINIT_SIZE100

#defineINCREMENT10

typedefintElemType;

typedefstructSqStack{

ElemType*base;

ElemType*top;

intstacksize;

}SqStack;

enumStatus{

OK,

ERROR,

OVERFLOW

};

/*sqStackOp.h文件*/

#include"sqStack.h"

StatusInitStack(SqStack&S);

StatusGetTop(SqStackS,ElemType&e);

StatusPush(SqStack&S,ElemTypee);

StatusPop(SqStack&S,ElemType&e);

boolStackEmpty(SqStack&S);

/*sqStackOp.cpp文件*/

#include

#include

#include"sqStackOp.h"

StatusInitStack(SqStack&S){

//构造一个空的栈

S.base=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType));

if(!

S.base)exit(OVERFLOW);//存储分配失败

S.top=S.base;

S.stacksize=INIT_SIZE;

returnOK;

}//InitStack

StatusGetTop(SqStackS,ElemType&e){

//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR

if(S.top==S.base)returnERROR;

e=*(S.top-1);

returnOK;

}//GetTop

StatusPush(SqStack&S,ElemTypee){

//插入元素e为新的栈顶元素

if(S.top-S.base>=S.stacksize){//栈满,追加存储空间

S.base=(ElemType*)realloc(S.base,(S.stacksize+INCREMENT)*sizeof(ElemType));

if(!

S.base)exit(OVERFLOW);//存储分配失败

S.top=S.base+S.stacksize;

S.stacksize+=INCREMENT;

}

*S.top++=e;

returnOK;

}//Push

StatusPop(SqStack&S,ElemType&e){

//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR

if(S.top==S.base)returnERROR;

e=*(--S.top);

returnOK;

}//Push

//判断栈是否为空

boolStackEmpty(SqStack&S){

if(S.top==S.base)

returntrue;

else

returnfalse;

}

/*main.cpp文件*/

#include

#include

#include"sqStackOp.h"

voidmain()

{

SqStacks;

intx;

InitStack(s);

scanf("%d",&x);//%d--十进制输入;%O--八进制输入;%x--十六进制输入

//修改这里输入进制和下面整除和余数计算,就可以获得其他进制的转换

while(x!

=0){

Push(s,x%8);

x=x/8;

}

while(!

StackEmpty(s)){

Pop(s,x);

printf("%d",x);

}

printf("\n");

getchar();

}

实验内容(3)参考程序

/*sqQueue.h文件*/

#defineMAXQSIZE100

typedefintQElemType;

typedefstructSqQueue{

QElemType*base;

intfront;

intrear;

}SqQueue;

enumStatus{

OK,

ERROR,

OVERFLOW

};

/*sqQueueOp.h文件*/

#include"sqQueue.h"

StatusInitQueue(SqQueue&Q);

StatusEnQueue(SqQueue&Q,QElemTypee);

StatusDeQueue(SqQueue&Q,QElemType&e);

boolQueueEmpty(SqQueue&Q);

intQueueLength(SqQueueQ);

/*sqQueueOp.cpp文件*/

#include

#include

#include"sqQueueOp.h"

StatusInitQueue(SqQueue&Q){

//构造一个空队列Q

Q.base=(QElemType*)malloc

(MAXQSIZE*sizeof(QElemType));

if(!

Q.base)exit(OVERFLOW);

//存储分配失败

Q.front=Q.rear=0;

returnOK;

}

StatusEnQueue(SqQueue&Q,QElemTypee){//插入元素e为Q的新的队尾元素

if((Q.rear+1)%MAXQSIZE==Q.front)

returnERROR;//队列满

Q.base[Q.rear]=e;

Q.rear=(Q.rear+1)%MAXQSIZE;

returnOK;

}

StatusDeQueue(SqQueue&Q,QElemType&e){//若队列不空,则删除Q的队头元素,

//用e返回其值,并返回OK;否则返回ERROR

if(Q.front==Q.rear)returnERROR;

e=Q.base[Q.front];

Q.front=(Q.front+1)%MAXQSIZE;

returnOK;

}

//判断队列是否为空

boolQueueEmpty(SqQueue&Q){

if(Q.front==Q.rear)

returntrue;

else

returnfalse;

}

//计算循环队列长度

intQueueLength(SqQueueQ){

return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;

}

/*main.cpp文件*/

#include

#include

#include"sqQueueOp.h"

voidmain()

{

printf("HelloQueue\n");

SqQueueQ;//定义顺序队列Q

QElemTypee;

if(OK!

=InitQueue(Q)){

printf("顺序队列初始化出错,退出....\n");

exit(-1);

}

EnQueue(Q,1);

EnQueue(Q,3);

EnQueue(Q,5);

EnQueue(Q,7);

printf("当前队列长度=%d\n",QueueLength(Q));

DeQueue(Q,e);

printf("队首元素%d出队,当前队列长度=%d\n",e,QueueLength(Q));

EnQueue(Q,9);

EnQueue(Q,11);

while(!

QueueEmpty(Q)){

DeQueue(Q,e);

printf("队首元素%d出队,当前队列长度=%d\n",e,QueueLength(Q));

}

getchar();

}

实验内容(4)参考程序

/*linkQueue.h文件*/

typedefintQElemType;

typedefstructQNode{//结点类型

QElemTypedata;

structQNode*next;

}QNode,*QueuePtr;

typedefstruct{//链队列类型

QueuePtrfront;//队头指针

QueuePtrrear;//队尾指针

}LinkQueue;

enumStatus{

OK,

ERROR,

OVERFLOW

};

/*linkQueueOp.h文件*/

#include"linkQueue.h"

StatusInitQueue(LinkQueue&Q);

StatusEnQueue(LinkQueue&Q,QElemTypee);

StatusDeQueue(LinkQueue&Q,QElemType&e);

boolQueueEmpty(LinkQueue&Q);

/*linkQueueOp.cpp文件*/

#include

#include

#include"linkQueueOp.h"

StatusInitQueue(LinkQueue&Q){

//构造一个空队列Q

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

if(!

Q.front)exit(OVERFLOW);

//存储分配失败

Q.front->next=NULL;

returnOK;

}

StatusEnQueue(LinkQueue&Q,QElemTypee){

//插入元素e为Q的新的队尾元素

QueuePtrp=(QueuePtr)malloc(sizeof(QNode));

if(!

p)exit(OVERFLOW);//存储分配失败

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

returnOK;

}

StatusDeQueue(LinkQueue&Q,QElemType&e){

//若队列不空,则删除Q的队头元素,

//用e返回其值,并返回OK;否则返回ERROR

if(Q.front==Q.rear)returnERROR;

QueuePtrp=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.rear==p)Q.rear=Q.front;

free(p);

returnOK;

}

//判断队列是否为空

boolQueueEmpty(LinkQueue&Q){

if(Q.front==Q.rear)

returntrue;

else

returnfalse;

}

/*main.cpp文件*/

#include

#include

#include"linkQueueOp.h"

voidmain()

{

printf("HelloLinkQueue\n");

LinkQueueQ;//定义顺序队列Q

QElemTypee;

if(OK!

=InitQueue(Q)){

printf("顺序队列初始化出错,退出....\n");

exit(-1);

}

EnQueue(Q,1);

EnQueue(Q,3);

EnQueue(Q,5);

EnQueue(Q,7);

DeQueue(Q,e);

printf("队首元素%d出队,\n",e);

EnQueue(Q,9);

EnQueue(Q,11);

while(!

QueueEmpty(Q)){

DeQueue(Q,e);

printf("队首元素%d出队,\n",e);

}

getchar();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1