数据结构与算法-队列
生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法-队列
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
隊(duì)列:隊(duì)列與棧不同,它是一種先進(jìn)先出的結(jié)構(gòu)
實(shí)現(xiàn):
1、數(shù)組
2、鏈表
記錄的數(shù)據(jù):
1、隊(duì)首位置:第一個(gè)元素的位置
2、隊(duì)尾位置:最后一個(gè)元素的位置
3、隊(duì)列大小:size
隊(duì)列操作:
entryQueue():入隊(duì) exitQueue():出隊(duì) isQueueEmpty():隊(duì)列為空 isQueueFull():隊(duì)列滿隊(duì)列的實(shí)現(xiàn)(順序數(shù)組的實(shí)現(xiàn))
將隊(duì)列的定義放在queue.h中
隊(duì)列的具體實(shí)現(xiàn):
#include<queue.h> queue::queue(int size) {a = new int[size];this->size = size;head=0;tail=0; } queue::~queue() {delete[] a; } bool queue::isQueueEmpty() {return (head == tail); } bool queue::isQueueFull() {return tail+1 == size } void queue::entryQueue(int item) {if(isQueueFull())print("the queue is full");else{a[tail ++] = item;} } int queue::exitQueue() {if(isQueueEmpty())print("the queue is empty");else{int result = a[head];head = head+1;return result;} } int queue::getSize() {return size; }上述使用順序數(shù)組的形式實(shí)現(xiàn)了隊(duì)列,但是這種方式有非常大的弊端:當(dāng)刪除隊(duì)列中的元素時(shí),數(shù)組中head之前空間我們無法再次使用,這樣就造成了極大的空間浪費(fèi)。解決的方法是使用循環(huán)隊(duì)列或者直接使用鏈?zhǔn)酱鎯?chǔ)。
總結(jié)
以上是生活随笔為你收集整理的数据结构与算法-队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法-链表
- 下一篇: 数据结构与算法-栈