数据结构之队列
来源:互联网 发布:c语言fabs函数用法 编辑:程序博客网 时间:2024/05/02 04:40
一、队列的介绍
队列(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_Hclass 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;}
0 0
- 数据结构之队列(链式队列)
- 数据结构之队列(数组队列)
- 数据结构之队列-顺序队列
- 数据结构之循环队列
- 数据结构之顺序队列
- 数据结构之链队列
- java数据结构之队列
- 数据结构之队列
- 数据结构复习之【队列】
- 内核数据结构之队列
- 数据结构之队列实例
- 数据结构学习之队列
- 复习数据结构之队列
- C 队列之数据结构
- 基础数据结构之队列
- 数据结构之队列
- 大话数据结构之队列
- 数据结构之循环队列
- iOS线程死锁分析
- UVa 1600:Patrol Robot(IDA*)
- hdu1573 中国剩余问题定理 模数非互质
- zoj1505 双向bfs
- Java知识整理(十一)之枚举
- 数据结构之队列
- C++友元函数
- 图像局部特征学习(笔记1之Harris角点)
- CodeForces 578C Weakness and Poorness(三分法+最大子段和)
- WORD编辑
- iOS中的动画
- Java知识整理(十二)之反射
- POJ - 1384 Piggy-Bank(01背包)
- oracle 获取表结构信息的SQL