一:目的
1. 用C++實現鏈隊列的基本操作
2. 用C++實現循環隊列的基本操作
二:鏈隊列的實現
1. 定義數據結構和類,書寫在Queue.h中# include <iostream> using namespace std; typedef int ElemType; typedef struct QNode { ElemType data; QNode *next; }QNode, *QueuePtr; //節點 //單鏈隊列 typedef struct { QueuePtr front; //隊頭指針 QueuePtr rear; //隊尾指針 }LinkQueue; class MyLinkQueue { public: void InitQueue(); //初始化隊列 void DestroyQueue(); //銷燬隊列 void ClearQueue(); //清空隊列 bool QueueEmpty(); //隊列是否爲空 int QueueLength(); //隊列長度 void Enqueue(ElemType val); //在隊尾插入數據 void DeQueue(ElemType & val); //刪除隊頭 void Print(); //從頭到尾打印 private: LinkQueue q; };
2. 具體實現代碼在Queue.cpp中3.簡單測試如下#include "Queue.h" //初始化隊列 void MyLinkQueue::InitQueue() { q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); if(!q.front) { //如果分配失敗 cout <<"初始化失敗"<<endl; return; } q.front->next = NULL; } //銷燬隊列 void MyLinkQueue::DestroyQueue() { while(q.front) { q.rear = q.front->next; free(q.front); q.front = q.rear; } } //在隊尾插入數據 void MyLinkQueue::Enqueue(ElemType val) { QueuePtr ptr = (QueuePtr)malloc(sizeof(QNode)); if(!ptr) { //如果分配失敗 cout << "節點分配失敗" << endl; return; } ptr->data = val; ptr->next = NULL; q.rear->next = ptr; q.rear = ptr; } //刪除隊頭,並返回當前隊頭的值 void MyLinkQueue::DeQueue(ElemType & val) { if(q.front == q.rear) { val = -1; return; } QueuePtr p; val = q.front->next->data; if(q.front->next == q.rear){//隊列只有一個元素 p = q.rear; q.rear = q.front; q.front->next = NULL; }else{ p = q.front->next; q.front->next = p->next; p->next = NULL; } free(p); } //打印 void MyLinkQueue::Print() { if(q.front == q.rear) { cout<< "隊列爲空" << endl; return; } QueuePtr ptr = q.front->next; while(ptr!=q.rear) { cout<<ptr->data<<endl; ptr = ptr->next; } cout<<ptr->data<<endl; } //清空隊列 void MyLinkQueue::ClearQueue() { DestroyQueue(); InitQueue(); } //隊列是否爲空 bool MyLinkQueue::QueueEmpty() { if(q.front == q.rear) return true; else return false; } //隊列長度 int MyLinkQueue:: QueueLength() { if(q.front == q.rear) return 0; QueuePtr ptr = q.front; int index = 0; do { index++; ptr = ptr->next; }while(ptr!=q.rear); return index; }
MyLinkQueue q; bool flag = q.QueueEmpty(); q.InitQueue(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Enqueue(4); q.Enqueue(5); q.Enqueue(6); q.Enqueue(7); int len = q.QueueLength(); q.Print(); int val; q.DeQueue(val); q.DeQueue(val); cout <<"取出兩個隊頭後"<<endl; q.Print(); q.ClearQueue(); q.Print(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Enqueue(4); q.Print();
三:循環隊列的實現
1. 定義數據結構和類,書寫在Queue.h中2.具體實現在Circular_Queue.cpp中# include <iostream> using namespace std; #define MAX_QUEUE_SIZE 100 typedef int ElemType; typedef struct QNode { ElemType data; QNode *next; }QNode, *QueuePtr; //節點 //循環隊列 typedef struct{ ElemType *base; int front; int rear; }SqQueue; class CircularQueue { public: void InitQueue(); //初始化隊列 void DestroyQueue(); //銷燬隊列 void ClearQueue(); //清空隊列 bool QueueEmpty(); //隊列是否爲空 int QueueLength(); //隊列長度 void Enqueue(ElemType val); //在隊尾插入數據 void DeQueue(ElemType & val); //刪除隊頭 void Print(); //從頭到尾打印 private: SqQueue q; };
3. 測試同上,只需要將MyLinkQueue改爲CircularQueue即可#include "Queue.h" //初始化隊列 void CircularQueue::InitQueue() { q.base = (ElemType *)malloc(sizeof(ElemType) * MAX_QUEUE_SIZE); if(!q.base) { //如果分配失敗 cout <<"初始化失敗"<<endl; return; } q.front = q.rear = 0; } //銷燬隊列 void CircularQueue::DestroyQueue() { free (q.base); q.front = q.rear = 0; } //在隊尾插入數據 void CircularQueue::Enqueue(ElemType val) { if((q.rear + 1)%MAX_QUEUE_SIZE == q.front) { cout << "隊列已滿!" << endl; return; } q.base[q.rear] = val; q.rear = (q.rear+1)%MAX_QUEUE_SIZE; return; } //刪除隊頭,並返回當前隊頭的值 void CircularQueue::DeQueue(ElemType & val) { if(q.front == q.rear) { cout<<"隊列爲空!"<<endl; return; } val = q.base[q.front]; q.front = (q.front+1)%MAX_QUEUE_SIZE; return; } //打印 void CircularQueue::Print() { if(q.front == q.rear) { cout<< "隊列爲空" << endl; return; } for(int i = q.front; i < q.rear;i++) cout<< q.base[i]<<endl; return; } //清空隊列 void CircularQueue::ClearQueue() { DestroyQueue(); InitQueue(); } //隊列是否爲空 bool CircularQueue::QueueEmpty() { if(q.front == q.rear) return true; else return false; } //隊列長度 int CircularQueue:: QueueLength() { return (q.rear - q.front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE; }