数据结构与算法-队列
生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法-队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隊列:隊列與棧不同,它是一種先進先出的結構
實現:
1、數組
2、鏈表
記錄的數據:
1、隊首位置:第一個元素的位置
2、隊尾位置:最后一個元素的位置
3、隊列大小:size
隊列操作:
entryQueue():入隊 exitQueue():出隊 isQueueEmpty():隊列為空 isQueueFull():隊列滿隊列的實現(順序數組的實現)
將隊列的定義放在queue.h中
隊列的具體實現:
#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; }上述使用順序數組的形式實現了隊列,但是這種方式有非常大的弊端:當刪除隊列中的元素時,數組中head之前空間我們無法再次使用,這樣就造成了極大的空間浪費。解決的方法是使用循環隊列或者直接使用鏈式存儲。
總結
以上是生活随笔為你收集整理的数据结构与算法-队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法-链表
- 下一篇: 数据结构与算法-栈