雙向佇列 (Deques) 是英文名稱 (Double-ends Queues) 的縮寫, 雙向佇列就是一種前後兩端都可以輸入或取出資料的有序串列. 更詳細請參考以下範例代碼 :
範例代碼 :
這裡以鏈結串列來實現雙向佇列.
* JQueueInLS.h 代碼 :
- #include
- #include
- using namespace std;
- struct QueueData{
- int data;
- struct QueueData *next;
- };
- typedef struct QueueData QueueNode;
- typedef QueueNode *QueueByLinkedList;
- class JQueueInLS{
- int maxSize;
- QueueByLinkedList front;
- QueueByLinkedList rear;
- public:
- JQueueInLS(int size){
- front = NULL;
- rear = NULL;
- maxSize = size;
- }
- JQueueInLS(){
- front = NULL;
- rear = NULL;
- maxSize = 20;
- }
- ~JQueueInLS(){
- while(front != NULL) {
- QueueByLinkedList tmpList = front;
- front = front->next;
- delete tmpList;
- }
- }
- bool add(int val); // 添加新資料到佇列
- bool isEmpty(); //判斷佇列是否為空
- bool isFull(); // 判斷佇列是否已滿
- bool get(int *val); // 取回佇列的值
- int length(); // 目前佇列的資料數量.
- int dequeue(int action); //action=1 : 從前端取資料, action=2 : 從後端取資料
- };
- #include "JQueueInLS.h"
- bool JQueueInLS::add(int val) {
- if(!isFull()) {
- if(rear == NULL) {
- rear = new QueueNode;
- } else {
- rear->next = new QueueNode;
- rear = rear->next;
- }
- rear->data = val;
- if(this->front == NULL) {
- front = rear;
- }
- rear->next = NULL;
- return true;
- } else {
- cout << "[佇列已滿]" << endl;
- }
- return false;
- }
- bool JQueueInLS::get(int *val) {
- if(!this->isEmpty()) {
- *val = front->data;
- QueueByLinkedList tmpList = front;
- front = front->next;
- delete tmpList;
- return true;
- } else {
- *val = -1;
- cout << "[佇列為空]" << endl;
- }
- return false;
- }
- bool JQueueInLS::isEmpty(){
- if(front == NULL) {
- return true;
- }
- return false;
- }
- bool JQueueInLS::isFull(){
- if(this->length() >= this->maxSize) {
- return true;
- } else {
- return false;
- }
- }
- int JQueueInLS::length() {
- if(this->front != NULL) {
- QueueByLinkedList tmpPoint = front;
- int size = 0;
- while(tmpPoint != NULL) {
- size++;
- tmpPoint = tmpPoint->next;
- }
- return size;
- } else {
- return 0;
- }
- }
- int JQueueInLS::dequeue(int action) {
- if(action == 1) { // 從前端取資料
- int tmp = -1;
- this->get(&tmp);
- return tmp;
- } else if(action == 2) { //從後端取資料
- if(!this->isEmpty()) {
- int val = rear->data;
- if(rear == front) {
- delete rear;
- rear = NULL;
- front = NULL;
- } else {
- QueueByLinkedList tmpPoint = front;
- while(tmpPoint->next != rear) {
- tmpPoint = tmpPoint->next;
- }
- delete rear;
- rear = tmpPoint;
- tmpPoint->next = NULL;
- }
- return val;
- } else {
- cout << "[佇列已空]" << endl;
- }
- } else { // 不合法 action
- cout << "[Illegal Action]" << endl;
- }
- return -1;
- }
- void ch05_04(bool b) {
- if(b) {
- char ch = 'S';
- int val;
- int gval=-1;
- JQueueInLS queue;
- while(ch != 'E') {
- cout << "[A]存入一個數值, [F] 從前端取出資料, [B]從後端取出資料, [E] 結束, [L] 目前佇列長度 :";
- cin >> ch;
- switch(ch) {
- case 'A':
- cout << "請輸入數值: ";
- cin >> val;
- queue.add(val);
- break;
- case 'F':
- gval = queue.dequeue(1);
- if(gval>=0) {
- cout << "取出數值為: " << gval << endl;
- }
- break;
- case 'B':
- gval = queue.dequeue(2);
- if(gval>=0) {
- cout << "取出數值為: " << gval << endl;
- }
- break;
- case 'E':
- cout << "[離開程式]" << endl;
- break;
- case 'L':
- cout << "目前佇列資料數目為: " << queue.length() << endl;
- break;
- default:
- cout << "不合法字元! (" << ch << ")" << endl;
- break;
- }
- }
- }
- }
執行結果 :
補充說明 :
* [ 資料結構 小學堂 ] 佇列 : 認識佇列
沒有留言:
張貼留言