數據結構(三)隊列

數據結構(三)隊列

  • 隊列
    • 隊列(順序存儲)
  • 循環隊列(順序存儲)
    • 隊列(鏈式存儲)

隊列

隊列是一種受限制的線性表,只允許表的一端插入,在表的另一端刪除

隊列(順序存儲)

// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;#define maxsize 50
#define elemtype int
typedef struct
{elemtype data[maxsize];int front, rear;  //隊頭指針和隊尾指針
}SqQueue;void InitQueue(SqQueue &Q)
{Q.rear = Q.front = 0;   //初始化隊首、隊尾指針
}bool QueueEmpty(SqQueue Q)
{if (Q.front == Q.rear){return true;}return false;
}bool EnQueue(SqQueue& Q, elemtype x)
{if (Q.rear == maxsize){return false;}Q.data[Q.rear++] = x;//添加隊列return true;
}elemtype DeQueue(SqQueue& Q)
{bool ret=QueueEmpty(Q);if (ret){printf("隊列為空!\n");exit(1);}return Q.data[Q.front++];//添加隊列
}bool GetHead(SqQueue Q, elemtype& x)
{if (QueueEmpty(Q)){printf("隊列為空!\n");exit(1);}x = Q.data[Q.front];return true;
}int main()
{SqQueue Q;InitQueue(Q);EnQueue(Q, 5);EnQueue(Q, 8);EnQueue(Q, 10);DeQueue(Q);for (int i = Q.front; i < Q.rear; i++){printf("%d\n",Q.data[i]);}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

循環隊列(順序存儲)

// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;#define maxsize 50
#define elemtype int
typedef struct
{elemtype data[maxsize];int front, rear;  //隊頭指針和隊尾指針
}SqQueue;void InitQueue(SqQueue &Q)
{Q.rear = Q.front = 0;   //初始化隊首、隊尾指針
}bool QueueEmpty(SqQueue Q)
{if (Q.front == Q.rear){return true;}return false;
}bool EnQueue(SqQueue& Q, elemtype x)
{if (Q.rear == maxsize){return false;}Q.data[Q.rear++] = x;//添加隊列return true;
}elemtype DeQueue(SqQueue& Q)
{bool ret=QueueEmpty(Q);if (ret){printf("隊列為空!\n");exit(1);}return Q.data[Q.front++];//添加隊列
}bool GetHead(SqQueue Q, elemtype& x)
{if (QueueEmpty(Q)){printf("隊列為空!\n");exit(1);}x = Q.data[Q.front];return true;
}int main()
{SqQueue Q;InitQueue(Q);EnQueue(Q, 5);EnQueue(Q, 8);EnQueue(Q, 10);DeQueue(Q);for (int i = Q.front; i < Q.rear; i++){printf("%d\n",Q.data[i]);}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

隊列(鏈式存儲)

帶頭結點

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;typedef struct  linknode //鏈式節點
{ElemType data;struct linknode* next;}LinkNode;//鏈式隊列
typedef struct
{LinkNode* front, *rear;}LinkQueue;void InitQueue(LinkQueue &Q)
{//帶頭節點的隊列初始化Q.rear=Q.front=(LinkNode*)malloc(sizeof(LinkNode));Q.front->next = NULL;
}bool IsEmpty(LinkQueue& Q)
{if (Q.rear == Q.front){return true;}return false;}void EnQueue(LinkQueue& Q, ElemType x)
{LinkNode* s= (LinkNode*)malloc(sizeof(LinkNode));s->data = x;s->next = Q.rear->next;Q.rear->next = s;Q.rear = s;}bool DeQueue(LinkQueue& Q, ElemType &x)
{if (IsEmpty(Q)){//隊列為空return false;}LinkNode* p = Q.front->next;x = p->data;Q.front->next = p->next;if (p == Q.rear)  //要刪除的為尾隊列{Q.rear = Q.front;}free(p);return true;
}int main()
{int x;LinkQueue Q;InitQueue(Q);EnQueue(Q, 5);EnQueue(Q, 7);EnQueue(Q, 9);DeQueue(Q, x);LinkNode* p = Q.front->next;while (p != NULL){printf("%d\n",p->data);p = p->next;}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

不帶頭結點

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;typedef struct  linknode //鏈式節點
{ElemType data;struct linknode* next;}LinkNode;//鏈式隊列
typedef struct
{LinkNode* front, * rear;}LinkQueue;void InitQueue(LinkQueue& Q)
{//不帶頭節點的隊列初始化Q.front =Q.rear= NULL;
}bool IsEmpty(LinkQueue& Q)
{if (Q.rear == Q.front){return true;}return false;}void EnQueue(LinkQueue& Q, ElemType x)
{LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));if(Q.front==NULL){s->data = x;s->next = NULL;Q.front = Q.rear = s;return;}s->next = NULL;s->data = x;Q.rear->next = s;Q.rear = s;}bool DeQueue(LinkQueue& Q, ElemType& x)
{if (IsEmpty(Q)){printf("Queue is empty!\n");//隊列為空return false;}LinkNode* p = Q.front->next;free(Q.front);Q.front = p;
}int main()
{int x;LinkQueue Q;InitQueue(Q);EnQueue(Q, 5);EnQueue(Q, 7);EnQueue(Q, 9);DeQueue(Q, x);LinkNode* p = Q.front;while (p != NULL){printf("%d\n", p->data);p = p->next;}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384382.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384382.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384382.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux fcntl函數詳解

轉載&#xff1a;http://www.cnblogs.com/xuyh/p/3273082.html 功能描述&#xff1a;根據文件描述詞來操作文件的特性。 文件控制函數 fcntl -- file control 頭文件&#xff1a; #include <unistd.h> #include <fcntl.h> 函數原型&#xff1a; …

vs2019使用sqlite數據庫遠程連接linux

vs2019使用sqlite數據庫遠程連接linux一、sqlite3添加到目錄二、添加依賴庫三、測試一、sqlite3添加到目錄 將兩個sqlite3頭文件放入目錄中 二、添加依賴庫 打開項目屬性 添加完成 三、測試 #include <stdio.h> #include <sqlite3.h>int main(int argc, cha…

linux網絡編程(四)線程池

linux網絡編程&#xff08;四&#xff09;線程池為什么會有線程池&#xff1f;實現簡單的線程池為什么會有線程池&#xff1f; 大多數的服務器可能都有這樣一種情況&#xff0c;就是會在單位時間內接收到大量客戶端請求&#xff0c;我們可以采取接受到客戶端請求創建一個線程的…

AIGC:大語言模型LLM的幻覺問題

引言 在使用ChatGPT或者其他大模型時&#xff0c;我們經常會遇到模型答非所問、知識錯誤、甚至自相矛盾的問題。 雖然大語言模型&#xff08;LLMs&#xff09;在各種下游任務中展示出了卓越的能力&#xff0c;在多個領域有廣泛應用&#xff0c;但存在著幻覺的問題&#xff1a…

關于C++子類父類成員函數的覆蓋和隱藏

轉載&#xff1a;http://blog.csdn.net/worldmakewayfordream/article/details/46827161 函數的覆蓋 覆蓋發生的條件&#xff1a; &#xff08;1&#xff09; 基類必須是虛函數&#xff08;使用virtual 關鍵字來進行聲明&#xff09; &#xff08;2&#xff09;發生覆蓋的兩個函…

數據結構(四)串的順序存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char ch[MAXLEN]; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;char* …

數據結構(四)串的動態數組存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char* ch; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;T.ch (char*)m…

C++名字隱藏

轉載&#xff1a;http://www.weixueyuan.net/view/6361.html 如果派生類中新增一個成員變量&#xff0c;該成員變量與基類中的成員變量同名&#xff0c;則新增的成員變量就會遮蔽從基類中繼承過來的成員變量。同理&#xff0c;如果派生類中新增的成員函數與基類中的成員函數同…

c語言深入淺出(一)strcpy和memcpy的區別

c語言深入淺出&#xff08;一&#xff09;strcpy和memcpy的區別strcpy和memcpy都是c語言的庫函數 strcpy:只用于字符串的復制&#xff0c;當碰到‘\0’就停止了 memcpy:用于這個內存的拷貝&#xff0c;適用于結構體、字符數組、類等 char * strcpy(char * dest, const char * s…

C++ dynamic_cast操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6377.html 在C中&#xff0c;編譯期的類型轉換有可能會在運行時出現錯誤&#xff0c;特別是涉及到類對象的指針或引用操作時&#xff0c;更容易產生錯誤。Dynamic_cast操作符則可以在運行期對可能產生問題的類型轉換進行測試。…

數據結構(五)樹

數據結構&#xff08;五&#xff09;樹一、基本操作樹是n個節點的有限集&#xff0c;它是一種遞歸的數據結構 一、基本操作 #include <stdio.h> #include <stdlib.h> #include <iostream>#define Elemtype charusing namespace std; typedef struct BiTNod…

C++ typeid操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6378.html typeid操作符用于判斷表達式的類型&#xff0c;注意它和sizeof一樣是一個操作符而不是函數。如果需要使用typeid操作符&#xff0c;最好加上typeinfo頭文件。 給出以下定義 int a;double b;char * c;long d; 下表列…

數據結構(五)層次遍歷

數據結構&#xff08;五&#xff09;層次遍歷// linear_listqueue.cpp : This file contains the main function. Program execution begins and ends there. //#include <iostream> #include <stdlib.h> #include <stdio.h> #define ElemType BiTree using …

C++成員函數指針的應用

轉載&#xff1a;http://www.cppblog.com/colys/archive/2009/08/18/25785.html C中&#xff0c;成員指針是最為復雜的語法結構。但在事件驅動和多線程應用中被廣泛用于調用回叫函數。在多線程應用中&#xff0c;每個線程都通過指向成員函數的指針來調用該函數。在這樣的應用中…

cv2.VideoCapture()無法打開視頻解決方法

cv2.VideoCapture無法打開視頻解決方法問題解決方法問題 cv2.VideoCapture打開mp4文件&#xff0c;直接報錯 解決方法 我們打開D:\opencv_3.4.2_Qt\opencv_3.4.2_Qt\x86\bin\&#xff08;opencv的dll動態庫中找到&#xff09; 找到opencv_ffmpeg342.dll文件&#xff0c;放入…

函數指針指向類的靜態成員函數

轉載&#xff1a;http://www.cnblogs.com/dongyanxia1000/p/4906592.html 1. 代碼 1 #include<iostream>2 #include<stdio.h>3 using namespace std;4 class Point5 {6 public:7 Point(int x0,int y0):x(x),y(y)8 { 9 count; 10 } 11 P…

Qt+OpenCV打開視頻文件并在窗口界面上顯示

QtOpenCV打開視頻文件并在窗口界面上顯示1、新建一個Qt Widgets Application&#xff0c;工程配置文件&#xff08;.pro文件&#xff09;內容如下&#xff1a;#------------------------------------------------- # # Project created by QtCreator 2021-03-19T09:06:07 # #--…

OpenCV Mat的數據類型

OpenCV Mat的數據類型Mattype類型內存拷貝簡單實現Mat Mat類(Matrix的縮寫)是OpenCV用于處理圖像而引入的-一個封裝類。他是一個自動內存管理工具。 Mat:本質上是由兩個數據部分組成的類:(包含信息有矩陣的大小&#xff0c;用于存儲的方法&#xff0c;矩陣存儲的地址等)矩陣頭…

C++指向成員函數的指針

轉載&#xff1a;http://www.cnblogs.com/tracylee/archive/2012/11/15/2772176.html C指向函數的指針定義方式為&#xff1a; 返回類型 &#xff08;*指針名&#xff09;&#xff08;函數參數列表&#xff09;&#xff0c;例如 void &#xff08;*p&#xff09;&#xff08;in…

OpenCV基礎知識 圖像

OpenCV基礎知識 圖像位圖模式灰度模式RGB模式位圖模式 位圖模式是是1位深度的圖像&#xff0c;只有黑和白兩種顏色。它可以由掃描或置入黑色的矢量線條圖像生成&#xff0c;也能由灰度模式轉換而成。其他圖像模式不能直接轉換為位圖模式。 灰度模式 灰度模式是8位的圖像&…