当前位置:文档之家› 数据结构实验5 (1)

数据结构实验5 (1)

《数据结构》实验报告实验序号:5 实验项目名称:队列的操作学号姓名专业、班实验地点指导教师实验时间一、实验目的及要求1. 熟悉队列的基本概念;2. 掌握队列的链式表存储结构;3.掌握队列的应用。

二、实验设备(环境)及要求微型计算机;windows 操作系统;Microsoft Visual Studio 6.0集成开发环境。

三、实验内容与步骤1.C++的库函数中已经实现了队列,引用方法为#include <queue>,请上网查阅资料,完成习题。

①创建一个队列。

②将a、b、c、d、e、f依次入队。

③若队列不为空,将元素出队并打印输出。

2.以下的链式队列采用队头指针和队尾指针分别跟踪队列的头尾两端,请删除队尾指针,只使用队头指针实现队列的初始化、入队、出队和销毁队列。

#include <stdlib.h>#include <stdio.h>#define ERROR 1#define OK 0#define OVERFLOW 1typedef int QElemType;typedef int Status;//用 e 返回其值,并返回OK;否则返回ERRORQueuePtr p;if (Q.front == Q.rear)return ERROR;p = Q.front->next;e = p->data;Q.front->next = p->next;if (Q.rear == p)Q.rear = Q.front;free (p);return OK;}int main(){int i,n=10;QElemType e;LinkQueue Q;InitQueue(Q); //初始化队列printf("元素入队");for(i=0;i<n;i++){printf(" %d ",i);EnQueue(Q,i); //元素入队}printf("\n元素出队");for(i=0;i<n;i++){DeQueue(Q,e); //元素出队printf(" %d ",e);}DestroyQueue(Q);return 0;}3.以下的循环队列采用空一个空间的方式来识别队列满与空,请修改程序,引入一个变量追踪循环队列被使用的空间数,从而达到100%队列空间可用。

#include <stdlib.h>#include <stdio.h>#define ERROR 1#define OK 0printf("元素入队列");for(i=0 ; i<10; i++){printf(" %d ",j);EnQueue(S,j); //元素入队列j++;}printf("\n元素出队列");for(i=0 ; i<10; i++){DeQueue(S,j); //元素出队列printf(" %d ",j);}}以下题目为选做题:4.参考P61-62的代码用链式表实现队列,并在主函数中测试。

四、实验结果与数据处理详细记录程序在调试过程中出现的问题及解决方法。

记录程序执行的结果(贴图)。

五、分析与讨论对上机实践结果进行分析,上机的心得体会。

六、教师评语签名:日期:成绩附源程序清单:11#include<stdio.h>#include<string.h>#include<queue>using namespace std;int main(){queue<char> q;int i;char f[5],m;for(i=0;i<5;i++){scanf("%c",&f[i]);q.push(f[i]);}for(i=0;i<5;i++){printf("%c ",q.front());q.pop();}return 0;}2#include <stdlib.h>#include <stdio.h>#define ERROR 1#define OK 0#define OVERFLOW 1typedef int QElemType;typedef int Status;typedef struct QNode {// 结点类型QElemType data;struct QNode *next;}QNode,*QueuePtr;typedef struct { // 链队列类型QueuePtr front; // 队头指针}LinkQueue;Status InitQueue (LinkQueue &Q) {// 构造一个空队列QQ.front=(QueuePtr)malloc(sizeof(QNode));if (!Q.front)exit (OVERFLOW);//存储分配失败Q.front->next = NULL;return OK;}Status DestroyQueue (LinkQueue &Q) {// 销毁队列QQ.front=NULL;return OK;}Status EnQueue (LinkQueue &Q,QElemType e) {// 如果是第一次进入队列,让队头指针跟他相等,之后每次入队在节点下一个为空前停住,让当前不为空节点等于新的产生的存储节点,完成入队//这个是循环次数结果入队,所以少一个传进函数的形式参量(记录入队次数的)QueuePtr p,q;p = (QueuePtr) malloc (sizeof (QNode));p->data = e;p->next = NULL;if(e==0){Q.front->next=p;}else{q=Q.front->next;while(q){if(q->next==NULL)break;q=q->next;}q->next=p;}if(!p)exit (OVERFLOW); //存储分配失败return OK;}Status DeQueue (LinkQueue &Q,QElemType &e) {// 若队列不空,则删除Q的队头元素(删除队尾就两说了)//用e 返回其值,并返回OK;否则返回ERRORQueuePtr p;if (Q.front->next==NULL)//当队头不为空,队列合法return ERROR;p=Q.front->next;e = p->data;Q.front->next = p->next;free (p);return OK;}int main(){int i,n=10;QElemType e;LinkQueue Q;InitQueue(Q); //初始化队列printf("元素入队");for(i=0;i<n;i++){printf(" %d ",i);EnQueue(Q,i); //元素入队}printf("\n元素出队");for(i=0;i<n;i++){DeQueue(Q,e); //元素出队printf(" %d ",e);}DestroyQueue(Q);return 0;}//destory删除队列函数有问题从写3#include <stdlib.h>#include <stdio.h>#define ERROR 1#define OK 0#define OVERFLOW 1typedef int QElemType;typedef int Status;#define MAXQSIZE 100 //最大队列长度typedef struct {QElemType *base; // 动态分配存储空间int front; // 头指针,若队列不空,指向队列头元素int rear; // 尾指针,若队列不空,//指向队列尾元素的下一个位置}SqQueue;Status InitQueue (SqQueue &Q) { // 构造一个空队列QQ.base = (QElemType *) malloc (MAXQSIZE *sizeof (QElemType));if (!Q.base)exit (OVERFLOW); //存储分配失败Q.front = Q.rear = 0;return OK;}Status EnQueue (SqQueue &Q, QElemType e) { // 插入元素e为Q的新的队尾元素if ((Q.rear+1) % MAXQSIZE == Q.front)return ERROR; //队列满Q.base[Q.rear] = e;Q.rear = (Q.rear+1) % MAXQSIZE;return OK;}Status DeQueue (SqQueue &Q, QElemType &e) { // 若队列不空,则删除Q的队头元素,用e 返回其值,并返回OK; 否则返回ERRORif (Q.front == Q.rear) return ERROR;e = Q.base[Q.front];Q.front = (Q.front+1) % MAXQSIZE;return OK;}int QueueLength(SqQueue &Q){return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;//追踪空间书}void main(){int i,a;QElemType j = 0;SqQueue S;InitQueue(S); //初始化队列printf("元素入队列");for(i=0 ; i<10; i++){printf("%d ",j);EnQueue(S,j); //元素入队列j++;printf("空间:");a=QueueLength(S);printf("%d ",a);}printf("\n元素出队列");for(i=0 ; i<10; i++){DeQueue(S,j); //元素出队列printf("%d ",j);printf("空间:");a=QueueLength(S);printf("%d ",a);}}4#include<stdio.h>#include<stdlib.h>#define ERROR 1#define OK 0#define MAXSIZE 100typedef int QElemType;typedef int Status;typedef struct QNode{Status data;struct QNode *next;}*Queueptr;typedef struct{Queueptr front;Queueptr rear ;Status *base;}Squeue;Status Initqueue(Squeue &Q){Q.front=Q.rear=(Queueptr)malloc(sizeof(QElemType));if(!Q.front)exit(ERROR);Q.front->next=NULL;return OK;}Status Enqueue(Squeue &Q,int e){Queueptr p;p=(Queueptr)malloc(sizeof(QNode));if(!p)exit(ERROR);p->data=e;p->next=NULL;Q.rear->next=p;Q.rear=p;return OK;}Status destoryQ(Squeue &Q){Queueptr p;while(Q.front){p=Q.front->next;Q.front->next=Q.front->next->next;free(p);}return OK;}Status deQueue(Squeue &Q,QElemType &e) {Queueptr p;p=Q.front->next;e=p->data;Q.front->next=p->next;free(p);return OK;}int main(){int i,j,k,o;Squeue Q;Initqueue(Q);scanf("%d",&i);for(j=0;j<i;j++){scanf("%d",&k);Enqueue(Q,k);}printf("\n");for(j=0;j<i;j++){deQueue(Q,o);printf("%d ",o);}destoryQ(Q);return 1;}。

相关主题