数据结构之队列
2015-09-19 10:17
369 查看
一、队列的介绍
队列(Queue),是一种线性存储结构。它有以下几个特点:
1、队列中数据是按照”先进先出(FIFO, First-In-First-Out)”方式进出队列的。
2、队列只允许在”队首”进行删除操作,而在”队尾”进行插入操作。 队列通常包括的两种操作:入队列 和 出队列。
1. 队列的示意图
![](http://img.blog.csdn.net/20150107150532135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVpY29vbF81MTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
队列中有10,20,30共3个数据。
2. 出队列
![](http://img.blog.csdn.net/20150107150546097?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVpY29vbF81MTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
出队列前:队首是10,队尾是30。
出队列后:出队列(队首)之后。队首是20,队尾是30。
3. 入队列
![](http://img.blog.csdn.net/20150107150610636?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVpY29vbF81MTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
入队列前:队首是20,队尾是30。
入队列后:40入队列(队尾)之后。队首是20,队尾是40。
二、队列的实现
实现1:数组实现
实现2:链表实现
数组实现完整代码:
链表实现完整代码:
队列(Queue),是一种线性存储结构。它有以下几个特点:
1、队列中数据是按照”先进先出(FIFO, First-In-First-Out)”方式进出队列的。
2、队列只允许在”队首”进行删除操作,而在”队尾”进行插入操作。 队列通常包括的两种操作:入队列 和 出队列。
1. 队列的示意图
队列中有10,20,30共3个数据。
2. 出队列
出队列前:队首是10,队尾是30。
出队列后:出队列(队首)之后。队首是20,队尾是30。
3. 入队列
入队列前:队首是20,队尾是30。
入队列后:40入队列(队尾)之后。队首是20,队尾是40。
二、队列的实现
实现1:数组实现
实现2:链表实现
数组实现完整代码:
//头文件 #ifndef QUEUE_H #define QUEUE_H class Queue { public: Queue(); ~Queue(); void EnQueue(int x); int DeQueue(); int GetSize(){ return (tail + MAXSIZE - head) % MAXSIZE; } int GetHead(); int GetTail(); bool Empty(){ return (head == tail) ? 1 : 0; } bool Full(){ return (head == tail + 1) ? 1 : 0; } void Display(); private: int *Data; const int MAXSIZE=50; int head; int tail; }; #endif // !QUEUE_H //实现文件 #include "queue.h" #include <iostream> Queue::Queue() { Data = new int[MAXSIZE]; head = 1; tail = 1; } Queue::~Queue() { while (!Empty()) DeQueue(); delete []Data; } void Queue::EnQueue(int x) { if (Full()) std::cout << "队列已满!" << std::endl; else { if (tail == MAXSIZE) { tail = 1; Data[tail] = x; } else Data[tail++] = x; } } int Queue::DeQueue() { if (Empty()) std::cout << "队列已空!" << std::endl; else { int x = Data[head]; if (head == MAXSIZE) head = 1; else head++; return x; } } int Queue::GetHead() { if (Empty()) std::cout << "队列已空!" << std::endl; else return Data[head]; } int Queue::GetTail() { if (Empty()) std::cout << "队列已空!" << std::endl; else return Data[tail - 1]; } void Queue::Display() { if (Empty()) std::cout << "队列已空!" << std::endl; else { int hd = head; while (hd != tail) { if (hd == MAXSIZE) hd = 1; std::cout << Data[hd++] << " "; } } } //测试文件 #include <iostream> #include "queue.h" using namespace std; int main() { Queue queue; cout << "队列中元素数目:"; cout << queue.GetSize() << endl; for (int i = 0; i < 30; ++i) queue.EnQueue(i); cout << "队列中元素数目:"; cout << queue.GetSize() << endl; for (int i = 0; i < 5; ++i) cout << "删除元素:" << queue.DeQueue() << endl; cout << "队列头元素:"; cout << queue.GetHead() << endl; cout << "队列尾元素:"; cout << queue.GetTail()<< endl; cout << "队列中元素数目:"; cout << queue.GetSize() << endl; for (int i = 0; i < 22; ++i) queue.EnQueue(i); cout << "队列中元素:"; queue.Display(); cout << endl; cout << "队列头元素:"; cout << queue.GetHead() << endl; cout << "队列尾元素:"; cout << queue.GetTail() << endl; cout << "队列中元素数目:"; cout << queue.GetSize() << endl; system("pause"); return 0; }
链表实现完整代码:
//头文件 #ifndef LINKQUEUE_H #define LINKQUEUE_H #include <iostream> struct Node { int data; Node *next; }; class LinkQueue { public: LinkQueue(); ~LinkQueue(); void EnQueue(int x); int DeQueue(); void Display(); bool Empty(){ return (tail == head) ? 1 : 0; } private: Node *head; //head指向无用的头结点, head->pNext才是指向队首元素, tail指向队尾元素 Node *tail; }; #endif // !LINKQUEUE_H //实现文件 #include "linkqueue.h" LinkQueue::LinkQueue() { head = tail = new Node; tail->next = NULL; } LinkQueue::~LinkQueue() { while (Empty()) DeQueue(); delete head; } void LinkQueue::EnQueue(int x) { Node *n = new Node; n->data = x; n->next = NULL; tail->next = n; //将n挂到队列尾部 tail = n; //为指针后移 } int LinkQueue::DeQueue() { if (Empty()) std::cout << "队列为空!" << std::endl; else { Node *temp = head->next; head->next = temp->next; int x = temp->data; delete temp; if (head->next == NULL) tail = head; return x; } } void LinkQueue::Display() { if (Empty()) std::cout << "队列为空!" << std::endl; else { Node *node = head->next; std::cout << "队列中的元素是:"; while (node != tail->next) { std::cout << node->data << " "; node = node->next; } delete node; } } //测试文件 #include "linkqueue.h" using namespace std; int main() { LinkQueue lq; for (int i = 0; i < 20; ++i) lq.EnQueue(i); lq.Display(); cout << endl; for (int i = 0; i < 5; ++i) lq.DeQueue(); lq.Display(); cout << endl; system("pause"); return 0; }
相关文章推荐
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- C语言单链队列的表示与实现实例详解
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- C++循环队列实现模型
- C#内置队列类Queue用法实例
- Array栈方法和队列方法的特点说明
- C#通过链表实现队列的方法
- C#队列Queue多线程用法实例
- C#数据结构与算法揭秘五 栈和队列
- C++中队列的建立与操作详细解析
- PHP实现的memcache环形队列类实例
- php基于双向循环队列实现历史记录的前进后退等功能
- 队列在编程中的实际应用(php)
- Go语言的队列和堆栈实现方法
- C++队列用法实例
- PHP队列用法实例