数据结构-【队列】链式队列和循环队列

来源:互联网 发布:咏春拳网络公开课20 编辑:程序博客网 时间:2024/04/28 12:16
队列特性:先进先出(FIFO)——先进队列的元素先出队列。来源于我们生活中的队列(先排队的先办完事)。
其结构图如下所示:
单队列结构图
循环队列结构图
链式队列的实现:
#include <iostream>#include <windows.h>using namespace std;typedef struct Queue{int number;Queue *pNext;}Qu;Qu *pHead=NULL;Qu *pEnd=NULL;void InQueue(int num);void OutQueue();void Length();bool Empty();void Get_Head();void Clear_Queue();void Destory_Queue();void Create_Queue();void Traverse();void Create_Queue() //常见队列;{int num;cout<<"please input the data,end with 0:"<<endl;cin>>num;while(num!=0){Qu *pp=new Qu;pp->number=num;pp->pNext=NULL;if(NULL==pHead){pHead=pp;pEnd=pHead;}else{pEnd->pNext=pp;pEnd=pp;}cin>>num;}}void InQueue(int num)//入队列;{Qu *temp=new Qu;temp->number=num;temp->pNext=NULL;pEnd->pNext=temp;pEnd=temp;}void OutQueue() //出队列;{if(Empty()){cout<<"the queue is NULL!"<<endl;return;}else{Qu *p=NULL;p=pHead;cout<<"出队元素:"<<pHead->number<<endl;pHead=pHead->pNext;delete p;}}bool Empty(){if(!pHead)return true;elsereturn false;}void Get_Head() //队列头结点;{if(Empty()){cout<<"the queue is NULL!"<<endl;return;}else{cout<<"队首元素:"<<pHead->number<<endl;}}void Length() //长度;{int count=0;Qu *p=pHead;while(p){count++;p=p->pNext;}cout<<"the length of the Queue is: "<<count<<endl;}void Clear_Queue() //清空队列;{if(Empty()){cout<<"the queue is NULL!"<<endl;return;}else{while(pHead){Qu *p=pHead;pHead=pHead->pNext;delete p;}}}void Destory_Queue()//销毁队列;{if(Empty()){cout<<"the queue is NULL!"<<endl;return;}else{Clear_Queue();}}void Traverse() //遍历队列{if(Empty()){cout<<"the queue si NULL!"<<endl;return;}else{Qu *p=pHead;while(p){cout<<p->number<<" ";p=p->pNext;}}}void menue(){while(1){int choice=0;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//设置代码输出的颜色代码,green,red,blue;cout<<"请选择你要执行的操作:"<<endl<<endl;cout<<"1.创建初始队列:"<<endl;cout<<"2.遍历队列:"<<endl;cout<<"3.返回队列的首个元素:"<<endl;cout<<"4.队列的长度:"<<endl;cout<<"5.出队:"<<endl;cout<<"6.入队:"<<endl;cout<<"7.清空队列:"<<endl;cout<<"8.销毁队列:"<<endl;cout<<"9.退出:"<<endl;cin>>choice;switch(choice){case 1:Create_Queue();break;case 2:Traverse();break;case 3:Get_Head();break;case 4:Length();break;case 5:OutQueue();break;case 6:int num;cout<<"please input the data to stack,end with 0:"<<endl;cin>>num;while(0!=num){InQueue(num);cin>>num;}break;case 7:Clear_Queue();break;case 8:Destory_Queue();break;case 9:return;default:cout<<"你正确输入你要执行的操作:"<<endl;}}}int main(){menue();system("pause");return 0;}<strong></strong>
运行结果:

循环队列:
实现代码:
#include <iostream>#include <windows.h>using namespace std;#define  MAXSIZE 20int front=0;int rear=0;int data[MAXSIZE];bool Empty();bool Full();void InQueue();void OutQueue();void Length();void Clear_Queue();void Get_Head();void Traverse();void InQueue(int num){if(Full()){cout<<"the QUEUE is FULL!"<<endl;return;}else{data[rear]=num;<span style="color:#ff0000;">rear=(rear+1) % MAXSIZE; //入队;</span>}}bool Empty(){<span style="color:#ff0000;">if(rear==front)   //判断循环队列为空;</span>return true;elsereturn false;}bool Full(){<span style="color:#ff0000;">if((rear+1) % MAXSIZE==front)//判断循环队列已满;</span>return true;else return false;}void OutQueue(){if(Empty()){cout<<"the QUEUE is FULL!"<<endl;return;}else{cout<<data[front]<<" ";data[front]=0;<span style="color:#ff0000;">front=(front+1)%MAXSIZE;  //出队列;</span>}}void Length(){<span style="color:#ff0000;">int count=(rear-front+MAXSIZE)%MAXSIZE;  //长度;</span>cout<<"队列的长度:"<<count<<endl;}void Traverse(){int fronts=front;int rears=rear;if(Empty()){cout<<"the queue is NULL!"<<endl;return;}while(rears!=fronts){cout<<data[fronts]<<" ";fronts++;}}void Clear_Queue(){data[MAXSIZE]=0;cout<<"the queue has been Cleared!"<<endl;}void Get_Head(){cout<<"队首元素:"<<data[front]<<endl;}void Create_Queue(){int num;cout<<"please input the number,end with 0"<<endl;cin>>num;while(num!=0){if(Full()){cout<<"the queue is full!"<<endl;return;}data[rear]=num;rear=(rear+1)%MAXSIZE;cin>>num;}}void menue(){while(1){int choice=0;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//设置代码输出的颜色代码,green,red,blue;cout<<"请选择你要执行的操作:"<<endl<<endl;cout<<"1.创建初始队列:"<<endl;cout<<"2.遍历队列:"<<endl;cout<<"3.返回队列的首个元素:"<<endl;cout<<"4.队列的长度:"<<endl;cout<<"5.出队:"<<endl;cout<<"6.入队:"<<endl;cout<<"7.清空队列:"<<endl;cout<<"8.退出:"<<endl;cin>>choice;switch(choice){case 1:Create_Queue();break;case 2:Traverse();break;case 3:Get_Head();break;case 4:Length();break;case 5:OutQueue();break;case 6:int num;cout<<"please input the data to stack,end with 0:"<<endl;cin>>num;while(0!=num){InQueue(num);cin>>num;}break;case 7:Clear_Queue();break;case 8:return;default:cout<<"你正确输入你要执行的操作:"<<endl;}}}int main(){menue();system("pause");return 0;}

4 0
原创粉丝点击