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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++队列与循环队列

發(fā)布時(shí)間:2024/9/27 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++队列与循环队列 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C/C++數(shù)據(jù)結(jié)構(gòu) - 隊(duì)列 循環(huán)隊(duì)列

快速入門

介紹

1. 隊(duì)列的定義

隊(duì)列是一種線性存儲結(jié)構(gòu),每次對隊(duì)列的增刪操作如下

  • 增:在隊(duì)列尾部添加元素

  • 刪(取出):在隊(duì)列頭部刪除元素

    這種數(shù)據(jù)存儲方式遵循“先進(jìn)先出”(First In First Out)的原則,簡稱FIFO結(jié)構(gòu);

2.隊(duì)列的相關(guān)概念

(1)隊(duì)頭

(2)隊(duì)尾

(3)空隊(duì)列

(4)滿隊(duì)列

3. 隊(duì)列相關(guān)操作

(1)入隊(duì):push()

(2)出隊(duì):pop()

(3)統(tǒng)計(jì)隊(duì)列元素個(gè)數(shù):countSize()

(4)判斷隊(duì)列是否為空:isEmpty()

(5)獲取隊(duì)頭: getFront()

(6)獲取隊(duì)尾:getBack()

示例代碼1:C++庫函數(shù)舉例

C++隊(duì)列queue模板類的定義在<queue>頭文件中,queue 模板類需要兩個(gè)模板參數(shù),一個(gè)是元素類型,一個(gè)容器類型,元素類型是必要的,容器類型是可選的,默認(rèn)為deque 類型。C++隊(duì)列Queue是一種容器適配器,它給予程序員一種先進(jìn)先出(FIFO)的數(shù)據(jù)結(jié)構(gòu)。

1. 頭文件,隊(duì)列聲明,隊(duì)列方法

#include <queue>queue<int> q; int i = 10;q.push(i) 在隊(duì)尾壓入新元素 q.pop() 刪除隊(duì)列首元素但不返回其值 q.size() 返回隊(duì)列中元素的個(gè)數(shù) q.empty() 如果隊(duì)列為空返回true,否則返回false q.front() 返回隊(duì)首元素的值,但不刪除該元素 q.back() 返回隊(duì)列尾元素的值,但不刪除該元素

2.示例

#include <iostream> #include <queue> #include <string>using namespace std;int main() {queue<int> my_q;string log; for (int i = 0; i < 10; i++) {my_q.push(i);}cout << "the front of q is " << my_q.front() << endl; // 返回定義類型cout << "the back of q is " << my_q.back() << endl; // 返回定義類型log = my_q.empty() == true ? "empty":"not empty"; // 返回布爾cout << "my_q is " << log << endl;cout << "my_q size is " << my_q.size() << endl; // 返回整數(shù)while(!my_q.empty()) {cout << "cur pop data = " << my_q.front();my_q.pop(); // 無返回值cout << " is poped" << endl;}return 0; }

示例代碼2:C語言靜態(tài)隊(duì)列(循環(huán)隊(duì)列,定長隊(duì)列)

C語言中,如果采用固定長度的一維數(shù)組來實(shí)現(xiàn)隊(duì)列,通常使用循環(huán)隊(duì)列的形式,避免普通隊(duì)列由于順序存儲導(dǎo)致執(zhí)行操作POP出隊(duì)時(shí)帶來的時(shí)間花費(fèi)問題:每次從數(shù)組頭部刪除元素(出隊(duì))后,需要將頭部以后的所有元素往前移動(dòng)一個(gè)位置,這是一個(gè)時(shí)間復(fù)雜度為O(n)的操作。

循環(huán)隊(duì)列的具體原理可以參考:

https://blog.csdn.net/li1914309758/article/details/81363166

由于循環(huán)隊(duì)列存在弊端:

當(dāng)隊(duì)空時(shí):front=rear
當(dāng)隊(duì)滿時(shí):front=rear 亦成立
因此只憑等式front=rear無法判斷隊(duì)空還是隊(duì)滿。 有兩種方法處理上述問題:
(1)另設(shè)一個(gè)標(biāo)志位以區(qū)別隊(duì)列是空還是滿。
(2)少用一個(gè)元素空間,約定以“隊(duì)列頭指針front在隊(duì)尾指針rear的下一個(gè)位置上”作為隊(duì)列“滿”狀態(tài)的標(biāo)志。即:
隊(duì)空時(shí): front=rear
隊(duì)滿時(shí): (rear+1)%maxsize=front

1. 頭文件定義

本示例代碼采用第一種方式處理循環(huán)隊(duì)列的隊(duì)空隊(duì)滿問題。

typedef struct _queue {enum QUEUE_TYPE type;int qcapacity; // 容量int qsize; // 當(dāng)前隊(duì)列數(shù)據(jù)的數(shù)量int front; // 隊(duì)頭int back; // 隊(duì)尾int *qdata; } Queue;

2. 源文件

#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include "DH_queue.h"static Queue *static_queue(int len) {Queue *queue = (Queue *)malloc(sizeof(Queue));memset(queue, 0, sizeof(Queue));queue->qcapacity = len;queue->qsize = 0;queue->front = 0;queue->back = 0;queue->qdata = (int *)malloc(sizeof(int) * len);memset(queue->qdata, 0, sizeof(int) * len);return queue; }static int static_countSize(Queue *q) {return q->qsize; }static bool static_isEmpty(Queue *q) {if (q->qsize == 0) {return 1; }return 0; }static bool static_isFull(Queue *q) {if (q->qsize == q->qcapacity) {return 1;}return 0; }static int static_getFront(Queue *q) {return q->qdata[q->front]; }static int static_getBack(Queue *q) {int index = ((q->back == 0) ? q->qcapacity - 1 : q->back - 1);return q->qdata[index]; }static bool static_push(Queue *q, void *data) {if (q->qsize == q->qcapacity) {printf("queue is full, push fail\n");return false;}q->qdata[q->back] = *(int *)data;q->qsize += 1;q->back = (q->back + 1) % q->qcapacity;printf("q->back = %d", q->back);return true; }static bool static_pop(Queue *q, int *data) {if (q->qsize == 0) {printf("queue is empty, pop fail\n");return false;}int tmp = 0;tmp = q->qdata[q->front];q->qsize -= 1;q->front = (q->front + 1) % q->qcapacity; return true; }// 主函數(shù):測試代碼 int main() {// 創(chuàng)建隊(duì)列Queue *my_queue = static_queue(5);// 入列for (int i = 0; i < 6; i++) {printf("= %d\n", static_push(my_queue, &i));}// 遍歷for (int i = 0; i < 5; i++) {printf("%d\n", my_queue->qdata[i]);}// 隊(duì)頭和隊(duì)尾printf("my_q front = %d, get front = %d, back = %d, get back = %d\n", my_queue->front, static_getFront(my_queue), my_queue->back, static_getBack(my_queue));// 容積和隊(duì)列元素printf("my_q capacity = %d, qsize = %d\n", my_queue->qcapacity, my_queue->qsize);// 隊(duì)空和隊(duì)滿printf("my_q isEmpty = %d, isFull = %d\n", static_isEmpty(my_queue), static_isFull(my_queue));// 出隊(duì)列for (int i = 0; i < 6; i++) {printf("= %d\n", static_pop(my_queue, &i));}return 0; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的C/C++队列与循环队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。