数据结构实验4队列
- 格式:doc
- 大小:31.00 KB
- 文档页数:6
数据结构实验实验内容和目的:掌握几种基本的数据结构:集合、线性结构、树形结构等在求解实际问题中的应用,以及培养书写规范文档的技巧。
学习基本的查找和排序技术。
让我们在实际上机中具有编制相当规模的程序的能力。
养成一种良好的程序设计风格。
实验教材:数据结构题集(C语言版)清华大学出版社2007年实验项目:实验一、栈和循环队列㈠、实验内容:①栈掌握栈的特点(先进后出FILO)及基本操作,如入栈、出栈等,栈的顺序存储结构和链式存储结构,以便在实际问题背景下灵活应用。
本程序采用的是链栈结构,具有初始化一个栈、PUSH、POP、显示所有栈里的元素四个功能。
②循环队列掌握队列的特点(先进先出FIFO)及基本操作,如入队、出队等,学会循环队列的实现,以便在实际问题背景下灵活运用。
本程序具有初始化一个队列、入队、出队、显示队列的所有元素、队列长度五个功能。
㈡、实验代码①栈程序代码:#include <stdio.h>#include <malloc.h>#define Stack_Size 6#define ERROR 0#define OK 1typedef int SElemType;typedef struct SNode{SElemType data;struct SNode *next;}SNode,*LinkStack;int CreatTwo(LinkStack &head,int n){int i;SNode *p;head=(LinkStack)malloc(sizeof(SNode));head->next=NULL;printf("请输入数据(数字):\n");for(i=n;i>0;--i){p=(SNode *)malloc(sizeof(SNode));scanf("%d",&p->data);p->next=head->next;head->next=p;}return 1;}int menu_select(){int sn;for(;;){scanf("%d",&sn);if(sn<1||sn>6)printf("\n\t输入错误,请重新输入\n");elsebreak;}return sn;}int Push(LinkStack &top,SElemType e){SNode *q;q=(LinkStack)malloc(sizeof(SNode));if(!q){printf("溢出!\n");return(ERROR);}q->data=e;q->next=top->next;top->next=q;return(OK);}int Pop(LinkStack &top,SElemType &e){SNode *q;if(!top->next){printf("error!\n");return(ERROR);}e=top->next->data;q=top->next;top->next=q->next;free(q);return(OK);}void main(){ int e;LinkStack top;printf("1.初始化一个栈;\n2.PUSH;\n3.POP;\n4.显示所有栈里的元素;\n5.结束;\n");while(1){switch(menu_select()){case 1:if(CreatTwo(top,Stack_Size))printf("Success!\n");break; case 2:printf("Push:\n");scanf("%d",&e);if(Push(top,e))printf("Success!\n");break;case 3:if(Pop(top,e))printf("Success!\n");printf("%d\n",e);break;case 4:LinkStack p;printf("所有栈里的元素:\n");p=top;while(p->next){p=p->next;printf("%7d",p->data);}printf("\n");break;case 5:return;}}}运行结果:②循环队列程序代码:#include<stdlib.h>#include<stdio.h>#define OVERFLOW -1#define OK 1#define ERROR 0#define MAXSIZE 100typedef struct{int *elem;//队列存储空间int front;int rear;}SqQueue;//判断选择是否正确int menu_select(){int sn;for(;;){scanf("%d",&sn);if(sn<1||sn>6)printf("\n\t输入错误,请重新输入\n");elsebreak;}return sn;}//参数(传出)SqQueue &Q,循环队列(空)int InitQueue(SqQueue &Q){Q.elem=(int *)malloc(MAXSIZE*sizeof(int));if(!Q.elem)exit(OVERFLOW);Q.front=Q.rear=-1;for(int i=0;i<MAXSIZE;i++)Q.elem[i]=-1;return OK;}//返回Q的元素个数int QueueLength(SqQueue Q){return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;}//显示队列的元素void Display(SqQueue Q){for(int i=0;i<=QueueLength(Q);i++)if(Q.elem[i]!=-1)printf("%d ",Q.elem[i]);printf("\n");}//入队int EnQueue(SqQueue &Q,int e){Q.rear=(Q.rear+1)%MAXSIZE;if(Q.rear==Q.front)return ERROR;Q.elem[Q.rear]=e;return OK;}//出队int DeQueue(SqQueue &Q,int &e){if(Q.front==Q.rear)return ERROR;e=Q.elem[Q.front+1];Q.elem[Q.front+1]=-1;Q.front=(Q.front+1)%MAXSIZE;return OK;}void main(){SqQueue Q;InitQueue(Q);int elem,e;printf("请输入队列元素(以0结束):\n");scanf("%d",&elem);while(elem!=0){EnQueue(Q,elem);scanf("%d",&elem);}printf("队列为:\n");Display(Q);printf("1.初始化一个队列;\n2.入队;\n3.出队;\n4.显示队列的所有元素;\n5.队列长度:\n6.结束;\n");while(1){switch(menu_select()){case 1:printf("请输入队列元素(以0结束):\n");scanf("%d",&elem);while(elem!=0){EnQueue(Q,elem);scanf("%d",&elem);}printf("队列为:\n");Display(Q);fflush(stdin);break;case 2:scanf("%d",&elem);EnQueue(Q,elem);printf("队列为:\n");Display(Q);fflush(stdin);break;case 3:DeQueue(Q,elem);printf("队列为:\n");Display(Q);break;case 4:printf("\n队列的所有元素:\n");Display(Q);break;case 5:printf("%d\n",QueueLength(Q));break;case 6:return;}}}运行结果:实验二、数组㈠、实验内容:数组一般不做插入或删除操作,也就是说,一旦建立了数组,则结构中的数据元素个数和元素之间的关系就不再发生变动。
实验报告课题名称:数据结构与算法班级:09网络工程姓名:杨益良学号:2009181105指导老师:梁海丽实验一:线性表的顺序存储一、实验目的1.掌握用C语言调试程序的基本方法。
2.掌握线性表顺序存储的基本运算,如插入、删除等。
二、实验内容线性表在顺序存储结构上的插入元素,删除元素运算三、实验要求1.C++/C完成算法设计和程序设计并上机调试通过。
2.撰写实验报告,提供实验结果和数据。
3.分析算法,要求给出具体的算法分析结果,并简要给出算法设计小结和心得。
四、源程序#include<stdio.h>#define MAXSIZE 100int list[MAXSIZE];int n;/*insert in a seqlist*/int sq_insert(int list[], int *p_n, int i, int x){int j;if (i<0 || i>*p_n) return(1);if (*p_n==MAXSIZE) return(2);for (j=*p_n+1; j>i; j--)list[j]=list[j-1];list[i]=x;(*p_n)++;return(0);}/*delete in a seq list*/int sq_delete(int list[], int *p_n, int i){int j;if (i<0 || i>=*p_n) return(1);for (j = i+1; j<=*p_n; j++)list[j-1] = list[j];(*p_n)--;return(0);}void main(){int i,x,temp;printf("please input the number for n\n");printf("n=");scanf("%d",&n);for (i=0; i<=n; i++){printf("list[%d]=",i);scanf("%d",&list[i]);}printf("The list before insertion is\n");for (i=0; i<=n; i++) printf("%d ",list[i]);printf("\n");printf("please input the position where you want to insert a value\nposition=");scanf("%d",&i);printf("please input the value you want to insert.\nx=");scanf("%d",&x);temp=sq_insert(list,&n,i,x);switch(temp){case 0:printf("The insertion is successful!\n");printf("The list is after insertion is\n");for(i=0; i<=n; i++) printf("%d ",list[i]);printf("\n");printf("%d\n",n);break;case 1:case 2:printf("The insertion is not successful!\n");break;}/*deleting*/printf("The list before deleting is\n");for (i=0; i<=n; i++) printf("%d ",list[i]);printf("\n");printf("please input the position where you want to delete a value\nposition=");scanf("%d",&i);temp=sq_delete(list,&n,i);switch(temp){case 0:printf("The deleting is successful!\n");printf("The list is after deleting is\n");for(i=0; i<=n; i++) printf("%d ",list[i]);printf("\n");printf("%d",n);break;case 1:printf("The deleting is not successful!");break;}}五、程序运行情况六、实验结果分析在顺序表中插入一个元素后,插入位置及之后的元素后移表长增长1;在顺序表中删除一个元素后,插入位置及之前的元素左移,表长减1;在该实验通过c语言调试程序了解并掌握了线性表在顺序存储结构上插入元素、删除元素的算法设计及相关程序设计和实现方法。
数据结构实验报告实验总结本次数据结构实验主要涉及线性表、栈和队列的基本操作以及链表的应用。
通过实验,我对这些数据结构的特点、操作和应用有了更深入的了解。
下面对每一部分实验进行总结。
实验一:线性表的基本操作线性表是一种常见的数据结构,本实验要求实现线性表的基本操作,包括插入、删除、查找、遍历等。
在实验过程中,我对线性表的结构和实现方式有了更清晰的认识,掌握了用数组和链表两种方式实现线性表的方法。
实验二:栈的应用栈是一种后进先出(LIFO)的数据结构,本实验要求利用栈实现简单的括号匹配和后缀表达式计算。
通过实验,我了解到栈可以方便地实现对于括号的匹配和后缀表达式的计算,有效地解决了对应的问题。
实验三:队列的应用队列是一种先进先出(FIFO)的数据结构,本实验要求利用队列实现银行排队和迷宫求解。
通过实验,我对队列的应用有了更加深入的了解,了解到队列可以解决需要按顺序处理的问题,如排队和迷宫求解等。
实验四:链表的应用链表是一种常用的数据结构,本实验要求利用链表实现学生信息管理系统。
通过实验,我对链表的应用有了更深入的了解,了解到链表可以方便地实现对于数据的插入、删除和修改等操作,并且可以动态地调整链表的长度,适应不同的需求。
通过本次实验,我掌握了线性表、栈、队列和链表的基本操作,并了解了它们的特点和应用方式。
同时,通过实际编程的过程,我对于数据结构的实现方式和效果有了更直观的认识,也锻炼了自己的编程能力和解决问题的能力。
在实验过程中,我遇到了一些问题,如程序逻辑错误和内存泄漏等,但通过调试和修改,最终成功解决了这些问题,对自己的能力也有了更多的信心。
通过本次实验,我深刻体会到了理论与实践的结合的重要性,也对于数据结构这门课程有了更加深入的理解。
总之,本次数据结构实验给予了我很多有益的启发和收获,对于数据结构的概念、特点和应用有了更深入的理解。
在以后的学习中,我会继续加强对数据结构的学习和研究,不断提高自己的编程能力和解决问题的能力。
数据结构实验4:C++实现循环队列实验44.1 实验⽬的熟练掌握队列的顺序存储结构和链式存储结构。
熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现。
根据具体给定的需求,合理设计并实现相关结构和算法。
4.2 实验要求4.2.1 循环顺序队列的实验要求循环顺序队列结构和运算定义,算法的实现以库⽂件⽅式实现,不得在测试主程序中直接实现;实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件⼯程要求;程序有适当的注释。
4.3 实验任务4.3.1 循环顺序队列实验任务编写算法实现下列问题的求解。
<1>初始化⼀个队列。
<2>判断是否队空。
<3>判断是否队满。
设队列最⼤长度:MaxLen=100第⼀组数据:⼊队n个元素,判断队满第⼆组数据:⽤循环⽅式将1到99,99个元素⼊队,判队满<4>⼊队第⼀组数据:4,7,8,12,20,50第⼆组数据:a,b,c,d,f,g<5>出队<6>取队头元素<7>求当前队列中元素个数<8>编写算法实现①初始化空循环队列;②当键盘输⼊奇数时,此奇数⼊队;③当键盘输⼊偶数时,队头出队;④当键盘输⼊0时,算法退出;⑤每当键盘输⼊后,输出当前队列中的所有元素。
4.5 运⾏结果截图及说明图1 测试(1)、(2)、(3)、(5)、(6)、(7)图2 测试(4)图3 测试(4)图4 测试(8)4.6 附源代码1// stdafx.h : include file for standard system include files,2// or project specific include files that are used frequently, but3// are changed infrequently4//56#if !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)7#define AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_89#if _MSC_VER > 100010#pragma once11#endif// _MSC_VER > 10001213 #include <stdc++.h>1415using namespace std;1617 typedef int elementType;18 typedef char elementType1;19const int maxn = 100;2021// TODO: reference additional headers your program requires here2223//{{AFX_INSERT_LOCATION}}24// Microsoft Visual C++ will insert additional declarations immediately before the previous line.2526#endif// !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)1// _SeqCircleQueue.h: interface for the _SeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45#if !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 6#define AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_78#if _MSC_VER > 10009#pragma once10#endif// _MSC_VER > 10001112class _SeqCircleQueue13 {14public:15 _SeqCircleQueue();16virtual ~_SeqCircleQueue();17bool emptySeqCircleQueue();18bool fullSeqCircleQueue();19bool enQueue( elementType value );20bool deQueue( elementType &value );21bool getFront( elementType &value );22int length();23void oddOrEven( elementType value );24 friend ostream &operator<<( ostream &os, _SeqCircleQueue &scq )25 {26if( ( scq._front - 1 ) % maxn == scq._rear )27return os;28int column = 0;29for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )30 {31 os << setw(3) << setiosflags(ios::left) << scq.data[i] << "";32 column ++;33if( column % 10 == 0 )34 os << endl;35 }36 os << endl;37 }39 elementType data[maxn];40int _front;41int _rear;4243 };4445#endif// !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 1// _SeqCircleQueue.cpp: implementation of the _SeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45 #include "stdafx.h"6 #include "_SeqCircleQueue.h"78//////////////////////////////////////////////////////////////////////9// Construction/Destruction10//////////////////////////////////////////////////////////////////////1112 _SeqCircleQueue::_SeqCircleQueue()13 {14 _front = _rear = 0;15 }1617 _SeqCircleQueue::~_SeqCircleQueue()18 {19 ios::sync_with_stdio(false);20 cout << "The _SeqCircleQueue destruction has been called!" << endl;21 }2223bool _SeqCircleQueue::emptySeqCircleQueue()24 {25return _front == _rear;26 }2728bool _SeqCircleQueue::fullSeqCircleQueue()29 {30return ( _rear + 1 ) % maxn == _front;31 }3233bool _SeqCircleQueue::enQueue( elementType value )34 {35if( fullSeqCircleQueue() )36 {37 cerr << "Seq-Circle-Queue is full!Error in _SeqCircleQueue::enQueue()!" << endl;38return false;39 }40 data[_rear] = value;41 _rear = ( _rear + 1 ) % maxn;42return true;43 }4445bool _SeqCircleQueue::deQueue( elementType &value )46 {47if( emptySeqCircleQueue() )48 {49 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::popFront()!" << endl;50return false;51 }52 value = data[_front];53 _front = ( _front + 1 ) % maxn;54return true;55 }5657bool _SeqCircleQueue::getFront( elementType &value )58 {59if( emptySeqCircleQueue() )60 {61 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::getFront()!" << endl;62return false;63 }64 value = data[_front];65return true;66 }6768int _SeqCircleQueue::length()69 {70if( emptySeqCircleQueue() )72 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::length()!" << endl;73return -1;74 }75return ( _rear - _front + maxn ) % maxn;76 }7778void _SeqCircleQueue::oddOrEven( elementType value )79 {80if( value & 1 )81 {82 enQueue(value);83 cout << value << " will be added to the queue!" << endl;84 cout << (*this);85 }86else if( !( value & 1) && value != 0 )87 {88 elementType x;89 deQueue(x);90 cout << x << " has been deleted from the queue!" << endl;91 cout << (*this);92 }93else//if( value == 0 )94 {95 cout << "The _SeqCircleQueue::oddOrEven() has been stoped!" << endl;96return;97 }98 }1// charSeqCircleQueue.h: interface for the charSeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45#if !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)6#define AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_78#if _MSC_VER > 10009#pragma once10#endif// _MSC_VER > 10001112class charSeqCircleQueue13 {14public:15 charSeqCircleQueue();16virtual ~charSeqCircleQueue();17bool emptyCharSeqCircleQueue();18bool fullCharSeqCircleQueue();19bool enQueue( elementType1 value );20bool deQueue( elementType1 &value );21bool getFront( elementType1 &value );22int length();23 friend ostream &operator<<( ostream &os, charSeqCircleQueue &cscq )24 {25if( ( cscq._front - 1 ) % maxn == cscq._rear )26return os;27int column = 0;28for( int i = cscq._front; i % maxn != cscq._rear; i = ( i + 1 ) % maxn )29 {30 os << setw(3) << setiosflags(ios::left) << cscq.data[i] << "";31 column ++;32if( column % 10 == 0 )33 os << endl;34 }35 os << endl;36 }37private:38 elementType1 data[maxn];39int _front;40int _rear;4142 };4344#endif// !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_) 1// charSeqCircleQueue.cpp: implementation of the charSeqCircleQueue class.3//////////////////////////////////////////////////////////////////////45 #include "stdafx.h"6 #include "charSeqCircleQueue.h"78//////////////////////////////////////////////////////////////////////9// Construction/Destruction10//////////////////////////////////////////////////////////////////////1112 charSeqCircleQueue::charSeqCircleQueue()13 {14 _front = _rear = 0;15 }1617 charSeqCircleQueue::~charSeqCircleQueue()18 {19 ios::sync_with_stdio(false);20 cout << "The charSeqCircleQueue destruction has been called!" << endl;21 }2223bool charSeqCircleQueue::emptyCharSeqCircleQueue()24 {25return _front == _rear;26 }2728bool charSeqCircleQueue::fullCharSeqCircleQueue()29 {30return ( _rear + 1 ) % maxn == _front;31 }3233bool charSeqCircleQueue::enQueue( elementType1 value )34 {35if( fullCharSeqCircleQueue() )36 {37 cerr << "Seq-Circle-Queue is full!Error in charSeqCircleQueue::::enQueue()!" << endl; 38return false;39 }40 data[_rear] = value;41 _rear = ( _rear + 1 ) % maxn;42return true;43 }4445bool charSeqCircleQueue::deQueue( elementType1 &value )46 {47if( emptyCharSeqCircleQueue() )48 {49 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::popFront()!" << endl; 50return false;51 }52 value = data[_front];53 _front = ( _front + 1 ) % maxn;54return true;55 }5657bool charSeqCircleQueue::getFront( elementType1 &value )58 {59if( emptyCharSeqCircleQueue() )60 {61 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::getFront()!" << endl; 62return false;63 }64 value = data[_front];65return true;66 }6768int charSeqCircleQueue::length()69 {70if( emptyCharSeqCircleQueue() )71 {72 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::length()!" << endl; 73return -1;74 }75return ( _rear - _front + maxn ) % maxn;76 }4.7 调试过程中出现的bug总结注意细节!。
数据结构队列实验报告
《数据结构队列实验报告》
实验目的:通过实验观察和分析队列数据结构的基本特性和操作,加深对队列
的理解和掌握。
实验内容:
1. 实现队列的基本操作:入队、出队、判空、判满等;
2. 使用队列解决实际问题:如模拟排队、实现广度优先搜索等;
3. 对队列的性能进行分析和比较。
实验步骤:
1. 设计并实现队列的基本操作函数;
2. 编写测试程序,对队列进行入队、出队等操作;
3. 使用队列解决实际问题,观察队列的应用效果;
4. 对队列的性能进行分析,比较不同实现方式的性能差异。
实验结果:
1. 队列的基本操作函数能够正确地实现入队、出队等操作;
2. 使用队列解决实际问题时,队列能够有效地管理数据,提高问题的解决效率;
3. 对队列的性能进行分析后发现,在数据量较大时,不同实现方式的性能差异
较大。
实验结论:
通过本次实验,深入理解了队列数据结构的基本特性和操作,并掌握了队列的
应用方法。
同时,通过对队列的性能进行分析和比较,也加深了对数据结构的
性能优化的理解。
队列作为一种重要的数据结构,在实际应用中具有广泛的用
途,对其理解和掌握对于提高程序的效率和性能具有重要意义。
队列的建⽴及操作数据结构与算法 --> 实验报告 4实验项⽬名称:队列的建⽴及操作⼀、实验⽬的1.掌握队列存储结构的表⽰和实现⽅法。
2.掌握队列的⼊队和出队等基本操作的算法实现。
⼆、实验题建⽴顺序循环队列,并在顺序循环队列上实现⼊队、出队基本操作。
三、实验过程及结果①基本思路:采⽤⼀种循环的结构去实现队列的顺序存储,队满和队空时标志都是 Q->front=Q->rear;为了区别两种情况,我的思路是:修改队满条件,浪费⼀个元素空间,当只有⼀个空闲单元时队满。
程序代码:#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0#define OVERFLOW -1#define MAXSIZE 10typedef int QElemType;typedef struct{QElemType \*base;int front;int rear;}SqQueue;Status InitQueue(SqQueue \*Q){Q->base = (QElemType *)malloc(MAXSIZE * sizeof(QElemType));if (Q->base==NULL) exit(OVERFLOW);Q->front = Q->rear = 0;return OK;}Status EnQueue(SqQueue \*Q,QElemType e){if ((Q->rear + 1) % MAXSIZE == Q->front)return ERROR;Q->base[Q->rear] = e;Q->rear = (Q->rear + 1) % MAXSIZE;return OK;}Status DeQueue(SqQueue *Q, QElemType *e){if (Q->front == Q->rear)return ERROR;*e = Q->base[Q->front];Q->front = (Q->front + 1) % MAXSIZE;return OK;}int main() {SqQueue Q;QElemType e;InitQueue(&Q);for (int i = 2; i < 7; i++){EnQueue(&Q, i);printf("⼊队元素为%d\n", i);}for (int j=2; j <7; j++) {DeQueue(&Q, &e);printf("出队元素为%d\n", e);}return 0;}②实验结果:四、实验总结队列的顺序存储采⽤循环队列,为了区分队空和队满,当只有⼀个空闲单元时队满。
队列实验报告队列实验报告引言:队列是一种常见的数据结构,它按照先进先出(FIFO)的原则管理数据。
在计算机科学中,队列被广泛应用于各种算法和数据处理任务中。
本实验旨在通过实际操作和观察,深入了解队列的特性和应用。
实验目的:1. 理解队列的基本概念和特性;2. 学会使用队列进行数据处理;3. 掌握队列在实际问题中的应用。
实验步骤:1. 队列的创建和初始化:首先,我们需要创建一个队列并进行初始化。
队列可以使用数组或链表来实现。
在本实验中,我们选择使用链表实现队列。
通过定义一个队列类,我们可以创建一个空队列,并为其设置头节点和尾节点。
2. 入队操作:入队操作是将元素添加到队列的末尾。
我们可以通过调用队列类的入队方法,在尾节点后插入新的节点。
在插入操作之前,我们需要判断队列是否为空。
如果队列为空,新节点将成为头节点和尾节点;如果队列不为空,新节点将链接到当前尾节点的后面,并成为新的尾节点。
3. 出队操作:出队操作是将队列中的第一个元素移除,并返回该元素的值。
我们可以通过调用队列类的出队方法,将头节点的下一个节点作为新的头节点,并返回旧的头节点的值。
在出队操作之前,我们同样需要判断队列是否为空。
如果队列为空,则无法进行出队操作。
4. 遍历队列:为了观察队列中的元素,我们可以使用遍历操作。
通过遍历队列,我们可以依次访问每个节点,并输出节点的值。
在遍历过程中,我们需要从头节点开始,依次访问每个节点的下一个节点,直到尾节点为止。
实验结果:通过上述实验步骤,我们可以得到以下结果:1. 队列的创建和初始化成功;2. 入队操作能够将元素添加到队列的末尾;3. 出队操作能够将队列中的第一个元素移除,并返回该元素的值;4. 遍历操作能够依次输出队列中的每个元素。
实验应用:队列在实际问题中有着广泛的应用。
以下是一些典型的应用场景:1. 消息队列:在分布式系统中,消息队列被用于异步通信和解耦。
生产者可以将消息发送到队列,而消费者可以从队列中获取消息并进行处理。
实验4 病人看病模拟程序
【问题描述】
编写一个程序,反映病人到医院看病,排队看医生的情况。
在病人排队的过程中,主要重复两件事:
(1)病人到达诊室,将病历本交给护士,排到等待队列中候诊。
(2)护士从等待队列中取出下一位病人的病历,该病人进入诊室就诊。
要求模拟病人等待就诊这一过程。
程序采用菜单方式,其选项及功能说明如下:
(1)排队――输入排队病人的病历号,加入病人排队队列中。
(2)就诊――病人排队队列中最前面的病人就诊,并将其从队列中删除;
(3)查看排队――从对首到队尾列出所有的排队病人的病历号;(4)不再排队,余下一次就诊――从对首到队尾列出所有的排队病人的病历号,并退出运行;
(5)下班――退出运行;
#include<stdio.h>
#include<malloc.h>
typedef struct qnode
{ int data;
struct qnode *next;
}QNode; //链队结点类型
typedef struct
{
QNode *front,*rear;
}QuType; //链队类型
void seedoctor() //模拟病人看病的过程
{
int sel,flag=1,find,no;
QuType *qu;
QNode *p;
qu=(QuType *)malloc(sizeof(QuType)); //创建空队
qu->front=qu->rear=NULL;
while(flag==1)
{
printf("1:排队 2:就诊 3:查看排队 4:不再排队,余下依次就诊 5:下班请选择:");
scanf("%d",&sel);
switch(sel)
{
case 1: printf(">>输入病历号:");
do
{ scanf("%d",&no);
find=0;
p=qu->front;
while(p!=NULL && !find)
{if(p->data==no)
find=1;
else
p=p->next;
}
if(find)
printf(">>输入的病历号重复,重新输入:");
}while(find==1);
p=(QNode *)malloc(sizeof(QNode)); //创建结点
p->data=no; p->next=NULL;
if(qu->rear==NULL) //第一个病人排队
{
qu->front=qu->rear=p;
}
else
{qu->rear->next=p;
qu->rear=p; //将*p结点入队
}
break;
case 2: if(qu->front==NULL) //队空
printf(">>没有排队的病人!\n");
else
{ p=qu->front;
printf(">>病人%d就诊\n",p->data);
if(qu->rear==p) //只有一个病人排队的情况
{
qu->front=qu->rear=NULL;
}
else
qu->front=p->next;
free(p);
}
break;
case 3:if(qu->front==NULL) // 队空
printf(">>没有排队的病人!\n");
else //队不空
{ p=qu->front;
printf(">>排队病人:");
while(p!=NULL)
{ printf("%d",p->data);
p=p->next;
}
printf("\n");
}
break;
case 4:if(qu->front==NULL) // 队空
printf(">>没有排队的病人!\n");
else //队不空
{ p=qu->front;
printf(">>排队病人:");
while(p!=NULL)
{ printf("%d",p->data);
p=p->next;
}
printf("\n");
}
flag=0; //退出
break;
case 5: if(qu->front!=NULL) //队不空
printf(">>请排队的病人明天就医!\n");
flag=0;
break;
}
}
}
void main() { seedoctor(); }。