日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构(8)----栈与队列之循环队列

發布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构(8)----栈与队列之循环队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

循環隊列的使用

循環隊列順序存儲結構

#define MAXSIZE 100 typedef int QElemType; typedef struct SqQueue{QElemType data[MAXSIZE];int front;/*指向隊列頭*/int rear;/*指向隊列尾*/ };

?循環隊列為空的判斷:Q->front == Q->rear

循環隊列隊滿的判斷:(Q->rear + 1) % MAXSIZE == Q->front

隊滿入下圖所示:

計算隊列長度公式:(rear-front+QueueSize)%QueueSize

?

#include<iostream> using namespace std; #define MAXSIZE 100 typedef int QElemType; typedef struct SqQueue{QElemType data[MAXSIZE];int front;/*指向隊列頭*/int rear;/*指向隊列尾*/ }; /*初始化隊列*/ void InitQueue(SqQueue *Q){Q->front = 0;Q->rear = 0; } /*求隊列長度*/ int QueueLength(SqQueue Q){return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; } /*入隊操作*/ void EnQueue(SqQueue *Q, QElemType e){if ((Q->rear + 1) % MAXSIZE == Q->front){cout << "該隊列已滿" << endl;return;}Q->data[Q->rear] = e;Q->rear = (Q->rear + 1) % MAXSIZE; } /*出隊操作*/ void DeQueue(SqQueue *Q){if (Q->front == Q->rear){cout << "該對列已空" << endl;return;}cout << Q->data[Q->front] << endl;Q->front = (Q->front + 1) % MAXSIZE; }int main(){SqQueue Q;InitQueue(&Q);EnQueue(&Q, 3);EnQueue(&Q, 2);EnQueue(&Q, 3);EnQueue(&Q, 4);EnQueue(&Q, 5);DeQueue(&Q);DeQueue(&Q);DeQueue(&Q);DeQueue(&Q);cout << QueueLength(Q) << endl; }

?附:隊列的鏈式存儲結構代碼示例:

#include<iostream> using namespace std;typedef int QElemType;typedef struct QNode{QElemType data;struct QNode *next; }; typedef struct LinkQueue{QNode * front, *rear; }; /*初始化隊列*/ void InitQueue(LinkQueue *Q){QNode *q = new QNode;q->data = 0;q->next = NULL;Q->front = q;Q->rear = q; } /*入隊*/ void EnQueue(LinkQueue *Q, QElemType e){QNode *p = new QNode; p->data = e;p->next = NULL;Q->rear->next = p; //隊列頭指針指向空節點Q->rear = p; } /*出隊*/ void DeQueue(LinkQueue *Q){QNode *p;if (Q->front == Q->rear){cout << "該隊列已空" << endl;return;}p = Q->front->next;cout << p->data<<endl;Q->front->next = p->next;if (Q->rear == p){Q->rear = Q->front;}free(p); }int main(){LinkQueue Q;InitQueue(&Q);EnQueue(&Q, 1);EnQueue(&Q, 2);EnQueue(&Q, 3);EnQueue(&Q, 4);EnQueue(&Q, 5);DeQueue(&Q);DeQueue(&Q);DeQueue(&Q);DeQueue(&Q); }

?

轉載于:https://www.cnblogs.com/EmperLin/p/6550572.html

總結

以上是生活随笔為你收集整理的数据结构(8)----栈与队列之循环队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。