《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx

上传人:b****6 文档编号:20684789 上传时间:2023-01-25 格式:DOCX 页数:17 大小:147.24KB
下载 相关 举报
《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx_第1页
第1页 / 共17页
《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx_第2页
第2页 / 共17页
《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx_第3页
第3页 / 共17页
《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx_第4页
第4页 / 共17页
《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx

《《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx》由会员分享,可在线阅读,更多相关《《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx(17页珍藏版)》请在冰豆网上搜索。

《数据结构》课程实验实训报告堆栈及队列的基本操作Word格式.docx

初始化、非空否、入栈、出栈、取栈顶数据元素。

(2)设计一个主函数对链式堆栈进行测试。

测试方法为依次把数据元素1、2、3、4、5入栈,然后出栈并在屏幕上显示出栈的数据元素。

(3)定义数据元素的数据类型为如下形式的结构体:

typedefstruct{

chartaskName[10];

//任务名

inttaskNo;

//任务号

}DataType;

首先设计一个包含5个数据的测试数据,然后设计一个主函数对链式堆栈进行测试。

测试的方法为:

依次把5个元素入栈,然后出栈并在屏幕上显示出栈的数据元素。

3.实现课本中顺序循环队列(p75-p77)的基本操作,并编制主函数实际运行验证其正确性。

4.对顺序循环队列,常规的方法是使用队尾指针和队头指针,队尾指针用于指示当前的队尾位置下标,队头指针用于指示当前的队头位置下标。

现要求:

(1)设计一个使用队头指针和计数器的顺序循环队列抽象数据类型,其中操作包括:

初始化、入队列、出队列、取队头元素和判断队列是否为空。

(2)设计一个测试主函数进行测试。

三、实验结果与分析(程序代码按序粘贴在下面,并将运行结果截图)

1.#include<

stdio.h>

#include<

stdlib.h>

typedefintDataType;

typedefstructsnode

{

DataTypedata;

structsnode*next;

}LSNode;

/*初始化操作:

*/

voidStackInitiate(LSNode**head)

/*初始化带头结点链式堆栈*/

if((*head=(LSNode*)malloc(sizeof(LSNode)))==NULL)exit

(1);

(*head)->

next=NULL;

}/*判非空操作:

intStackNotEmpty(LSNode*head)/*判堆栈是否非空,非空返回1;

空返回0*/

if(head->

next==NULL)return0;

elsereturn1;

}/*入栈操作:

intStackPush(LSNode*head,DataTypex)/*把数据元素x插入链式堆栈head的栈顶作为新的栈顶*/{LSNode*p;

if((p=(LSNode*)malloc(sizeof(LSNode)))==NULL){printf("

内存空间不足无法插入!

\n"

);

return0;

}p->

data=x;

p->

next=head->

next;

/*新结点链入栈顶*/head->

next=p;

/*新结点成为新的栈顶*/return1;

}/*出栈操作:

intStackPop(LSNode*head,DataType*d)/*出栈并把栈顶元素由参数d带回*/{LSNode*p=head->

if(p==NULL){

printf("

堆栈已空出错!

"

}

head->

next=p->

/*删除原栈顶结点*/

*d=p->

data;

/*原栈顶结点元素赋予d*/

free(p);

/*释放原栈顶结点内存空间*/

return1;

}/*取栈顶数据元素操作:

intStackTop(LSNode*head,DataType*d)/*取栈顶元素并把栈顶元素由参数d带回*/

LSNode*p=head->

if(p==NULL)

{

printf("

return0;

*d=p->

}/*撤销*/

voidDestroy(LSNode*head)

LSNode*p,*p1;

p=head;

while(p!

=NULL)

p1=p;

p=p->

free(p1);

}

voidmain(void)

LSNode*myStack;

inti,x;

StackInitiate(&

myStack);

for(i=0;

i<

5;

i++)

{

if(StackPush(myStack,i+1)==0)

{

printf("

error!

\n"

return;

}

if(StackTop(myStack,&

x)==0)

return;

else

Theelementoflocaltopis:

%d\n"

x);

Thesequenceofoutingelementsis:

while(StackNotEmpty(myStack))

StackPop(myStack,&

x);

%d"

x);

Destroy(myStack);

Thisprogramismadeby10273206\n"

2.#include<

#defineMaxStackSize100

typedefstruct

DataTypestack[MaxStackSize];

;

inttop;

}SeqStack;

voidStackInitiate(SeqStack*S)

S->

top=0;

}

intStackNotEmpty(SeqStackS)

if(S.top<

=0)

return1;

intStackPush(SeqStack*S,DataTypex){

if(S->

top>

=MaxStackSize)

堆栈已满无法插入!

S->

stack[S->

top]=x;

top++;

intStackPop(SeqStack*S,DataType*d)

top<

堆栈已空无数据元素出栈!

top--;

*d=S->

top];

intStackTop(SeqStackS,DataType*d)

堆栈已空!

*d=S.stack[S.top-1];

SeqStackmyStack;

inti,x;

if(StackPush(&

myStack,i+1)==0)

错误\n"

}

当前栈顶元素为:

依次出栈的数据元素序列如下:

StackPop(&

myStack,&

3.#include"

stdio.h"

#include"

string.h"

stdlib.h"

#defineMaxQueueSize100

typedefstruct

{

DataTypequeue[MaxQueueSize];

intrear;

intfront;

intcount;

}SeqCQueue;

voidQueueuInitiate(SeqCQueue*Q)

Q->

rear=0;

front=0;

count=0;

intQueueNotEmpty(SeqCQueueQ)

if(Q.count!

intQueueAppend(SeqCQueue*Q,DataTypex)

if(Q->

count>

0&

&

Q->

rear==Q->

front)

队列已满无法插入!

Q->

queue[Q->

rear]=x;

rear=(Q->

rear+1)%MaxQueueSize;

count++;

intQueueDelete(SeqCQueue*Q,DataType*d)

if(Q->

count==0)

队列已空无数据元素出队列!

return0;

else

*d=Q->

front];

Q->

front=(Q->

front+1)%MaxQueueSize;

count--;

return1;

intQueueGet(SeqCQueueQ,DataType*d)

if(Q.count==0)

队列已空无数据可取\n"

*d=Q.queue[Q.front];

SeqCQueuemyQueue;

QueueuInitiate(&

myQueue);

if(QueueAppend(&

myQueue,i+1)==0)

if(QueueGet(myQueue,&

队头元素是:

依次出栈数据元素顺序是:

while(QueueNotEmpty(myQueue))

QueueDelete(&

myQueue,&

4.#include"

if(Q.front!

=Q.rear)

front==(Q->

rear+1)%MaxQueueSize)

front==Q->

rear)

if(Q.front==Q.rear)

四、指导老师评语

指导老师签名:

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

当前位置:首页 > 求职职场 > 面试

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

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