一:目的
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; }