赞
踩
1、预习要求:队列相关概念及运算,完成队列相关运算。
2、实验目的:
(1)了解队列相关概念;
(2)理解队列运算过程和结构定义;
(3)掌握算法转换为程序的过程中的变化。
3、实验内容及要求:
(1)完成若干数据元素的进队、出队等运算;
(2)给出程序及队列运算过程的结果。
#include <iostream.h> #include <string.h> typedef enum Status{ ERROR,OK}; typedef struct { char name[20]; char number[20]; }student; typedef student Etype; typedef int HeadEtype; struct QueueNode { Etype data; QueueNode *link; }; struct ChainQueue { HeadEtype Hdata; QueueNode *front; QueueNode *rear; }; void Creat(ChainQueue *&Q) {//构造一个空队 Q= new ChainQueue; Q->front =NULL; QueueNode *rear=NULL; } bool Isempty(ChainQueue *&Q) {//判断队列是否为空 if (!Q->front) return true; return false; } Status Getfront(ChainQueue *&Q,Etype &x) {//返回队顶元素的值 if(Isempty(Q)) return ERROR; QueueNode *p=Q->front; strcpy(x.name,p->data.name); strcpy(x.number,p->data.number); return OK; } bool EnQueue(ChainQueue *Q,Etype &x) { QueueNode *q = new QueueNode; strcpy(q->data.name , x.name); strcpy(q->data.number , x.number); q->link = NULL; if(Isempty(Q)) { Q->front = q; Q->rear = q; } else { QueueNode *p = Q->rear; p->link = q; Q->rear = q; } return OK; }//进栈,分为队列为空和队列不空两种情况 Status DeQueue(ChainQueue *&Q,Etype &x) { QueueNode *p=Q->front; strcpy(x.name,p->data.name); strcpy(x.number,p->data.number); Q->front=p->link; delete p; return OK; }//出栈 void main() { ChainQueue *Q; char name[20]; char number[20]; Etype x,x1; Creat(Q); int i,n; cout<<"This is the Simple of Queue!/n"; cout<<"Please Input the Amount of Student's Information"<<endl; cin>>n; cout<<endl; for(i=0;i<n;i++) { cout<<"The"<<i+1<<"'Student'Information is :"<<endl; cin>>name; cin>>number; strcpy(x.name,name); strcpy(x.number,number); EnQueue(Q,x);//进栈 } cout<<"The Last Student's Information is:"<<x.name<<" "<<x.number<<endl; Getfront(Q,x1); cout<<"The Student's Information is Following!/n"<<endl; for(i=0;i<n;i++) { DeQueue(Q,x); cout<<x.name<<" "<<x.number<<endl; } }