国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

數(shù)據(jù)結構--循環(huán)隊列、鏈隊

這篇具有很好參考價值的文章主要介紹了數(shù)據(jù)結構--循環(huán)隊列、鏈隊。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

基礎知識

//循環(huán)隊列數(shù)據(jù)結構
typedef struct
{
QElemType data[MaxQSize];//數(shù)據(jù)域
int front,rear; //隊頭隊尾指針
}SqQueue;

//鏈隊結點數(shù)據(jù)結構
typedef struct QNode
{
int data;//數(shù)據(jù)域
struct QNode* next;//指針域

}QNode, * QueuePtr;
typedef struct
{
struct QNode* front, * rear;//rear指針指向隊尾 用于入隊 front指針指向隊頭 用于出隊
}LinkQueue;

隊列是一種常見的數(shù)據(jù)結構,它遵循==先進先出(FIFO)==的原則。以下是隊列的基本操作:
1、入隊(Enqueue):將元素添加到隊列的末尾。
2、出隊(Dequeue):從隊列的頭部移除一個元素,并返回其值。
3、隊列長度(Size):獲取隊列中元素的個數(shù)。
4、隊列是否為空(IsEmpty):檢查隊列是否為空。
5、獲取隊首元素(Front):獲取隊列頭部的元素值,但不移除該元素。文章來源地址http://www.zghlxwxcb.cn/news/detail-615149.html

C++ 實現(xiàn)隊列的基本操作的示例代碼:

#include <iostream>
#include <queue>

int main() {
    std::queue<int> myQueue;

    // 入隊
    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);

    // 隊列長度
    std::cout << "隊列長度:" << myQueue.size() << std::endl;

    // 出隊
    int frontElement = myQueue.front();
    myQueue.pop();
    std::cout << "出隊元素:" << frontElement << std::endl;

    // 獲取隊首元素
    std::cout << "隊首元素:" << myQueue.front() << std::endl;

    // 隊列是否為空
    std::cout << "隊列是否為空:" << (myQueue.empty() ? "是" : "否") << std::endl;

    return 0;
}

在C語言中,隊列的基本操作可以通過結構體和指針來實現(xiàn)。

#include <stdio.h>
#include <stdlib.h>

// 定義隊列結構體
typedef struct {
    int *array;  // 存儲隊列元素的數(shù)組
    int front;   // 隊首索引
    int rear;    // 隊尾索引
    int size;    // 隊列的容量
} Queue;

// 初始化隊列
void initQueue(Queue *queue, int capacity) {
    queue->array = (int*)malloc(capacity * sizeof(int));
    queue->front = 0;
    queue->rear = -1;
    queue->size = 0;
}

// 銷毀隊列
void destroyQueue(Queue *queue) {
    free(queue->array);
}

// 入隊
void enqueue(Queue *queue, int element) {
    queue->rear = (queue->rear + 1) % queue->size;
    queue->array[queue->rear] = element;
    queue->size++;
}

// 出隊
int dequeue(Queue *queue) {
    int dequeuedElement = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->size;
    queue->size--;
    return dequeuedElement;
}

// 獲取隊列長度
int getSize(Queue *queue) {
    return queue->size;
}

// 檢查隊列是否為空
int isEmpty(Queue *queue) {
    return queue->size == 0;
}

// 獲取隊首元素
int getFront(Queue *queue) {
    return queue->array[queue->front];
}

int main() {
    Queue myQueue;
    initQueue(&myQueue, 5);

    // 入隊
    enqueue(&myQueue, 10);
    enqueue(&myQueue, 20);
    enqueue(&myQueue, 30);

    // 隊列長度
    printf("隊列長度:%d\n", getSize(&myQueue));

    // 出隊
    int frontElement = dequeue(&myQueue);
    printf("出隊元素:%d\n", frontElement);

    // 獲取隊首元素
    printf("隊首元素:%d\n", getFront(&myQueue));

    // 隊列是否為空
    printf("隊列是否為空:%s\n", isEmpty(&myQueue) ? "是" : "否");

    destroyQueue(&myQueue);
    return 0;
}

循環(huán)隊列

#define MaxSize 100
typedef struct { int data[MaxSize]; int front, rear; }SqQueue;

//***********************************   基本操作函數(shù)  *******************************************

int InitQueue(SqQueue &Q)
{
	Q.front = Q.rear = 0;
	return 1;

}
bool QueueEmpty(SqQueue& Q) {
	if (Q.front != Q.rear)	return true;
	else   return false;
}

//入隊:尾部加1;	出隊:頭部加1
bool EnQueue(SqQueue& Q, int& e) {
	if (Q.front ==Q.rear)	return false;
	e = Q.data[Q.rear];
	Q.rear = (Q.rear + 1) % MaxSize; //指針加1 取模
	return true;
}

bool DeQueue(SqQueue& Q, int &e) {
	if (Q.front == Q.rear)	return false;
	e = Q.data[Q.front];
	Q.front = (Q.front + 1) % MaxSize; //指針加1 取模
	return true;
}

bool GetHead(SqQueue& Q, int &e)
{
	if (Q.front == Q.rear)	return false;//隊空 					
	e = Q.data[Q.front];
	return true;
}



//********************************功能實現(xiàn)函數(shù)**************************************//

void EnterToQueue(SqQueue& Q)
{
	int n; int e; int flag;
	printf("請輸入入隊元素個數(shù)(>=1):\n");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		printf("請輸入第%d個元素的值:", i + 1);
		scanf("%d", &e);
		flag = EnQueue(Q, e);
		if (flag)printf("%d已入隊\n", e);
		else { printf("隊已滿?。?!\n"); break; }
	}
}

void DeleteFromQueue(SqQueue& Q)
{
	int n; int e; int flag;
	printf("請輸入出隊元素個數(shù)(>=1):\n");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		flag = DeQueue(Q, e);
		if (flag)printf("%d已出隊\n", e);
		else { printf("隊已空?。?!\n"); break; }
	}
}

void GetHeadOfQueue(SqQueue Q)
{
	int e; bool flag;
	flag = GetHead(Q, e);
	if (flag)printf("隊頭元素為:%d\n", e);
	else printf("隊已空?。?!\n");
}



void menu() {
	printf("********1.入隊          2.出隊*********\n");
	printf("********3.取隊頭元素    4.退出*********\n");
}

int main() {
	SqQueue Q;
	int choice = 0;;
	InitQueue(Q);

	while (1)
	{
		menu();
		printf("請輸入菜單序號:\n");
		scanf("%d", &choice);
		if (choice == 4) break;
		switch (choice)
		{
		case 1:EnterToQueue(Q); break;
		case 2:DeleteFromQueue(Q); break;
		case 3:GetHeadOfQueue(Q); break;
		default:printf("輸入錯誤?。?!\n");
		}
		system("pause");
		system("cls");
	}

	return 0;
}

鏈隊

#define MaxSize 100

//鏈隊結點數(shù)據(jù)結構
typedef struct QNode
{
	int data;//數(shù)據(jù)域
	struct QNode* next;//指針域

}QNode, * QueuePtr;
typedef struct
{
	struct QNode* front, * rear;//rear指針指向隊尾 用于入隊 front指針指向隊頭 用于出隊
}LinkQueue;



//***********************************   基本操作函數(shù)  *******************************************

int InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new QNode;
	Q.front->next = NULL;
	return 1;

}


int EnQueue(LinkQueue& Q, int& e) {
	QNode* p;
	p = new QNode;//生成新節(jié)點
	p->data = e;    //賦值
	p->next = NULL;
	Q.rear->next = p;//加入隊尾
	Q.rear = p;      //尾指針后移
	return 1;
}

bool DeQueue(LinkQueue &Q, int &e) {
	QueuePtr p;
	if (Q.front == Q.rear)return false;//隊空
	e = Q.front->next->data;           //e返回值 之前寫的Q.front->data 炸了,頭結點沒數(shù)據(jù)的,一定要注意頭結點
	p = Q.front->next;                //保留,一會兒釋放空間
	Q.front->next = p->next;          //出隊,注意Q.front->next 不是Q.front 還有頭結點
	if (Q.rear == p)Q.rear = Q.front;    //最后一個元素出隊,rear指向頭結點
	free(p);
	return true;
}

//取隊頂函數(shù) 用e返回
bool GetHead(LinkQueue &Q, int &e)
{
	if (Q.front == Q.rear)	return false;//隊空 					
	e = Q.front->next->data;
	return true;
}



//********************************功能實現(xiàn)函數(shù)**************************************//

void EnterToQueue(LinkQueue& Q)
{
	int n; int e; int flag;
	printf("請輸入入隊元素個數(shù)(>=1):\n");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		printf("請輸入第%d個元素的值:", i + 1);
		scanf("%d", &e);
		flag = EnQueue(Q, e);
		if (flag)printf("%d已入隊\n", e);
	}
}

void DeleteFromQueue(LinkQueue& Q)
{
	int n; int e; int flag;
	printf("請輸入出隊元素個數(shù)(>=1):\n");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		flag = DeQueue(Q, e);
		if (flag)printf("%d已出隊\n", e);
		else { printf("隊已空?。?!\n"); break; }
	}
}

void GetHeadOfQueue(LinkQueue Q)
{
	int e; bool flag;
	flag = GetHead(Q, e);
	if (flag)printf("隊頭元素為:%d\n", e);
	else printf("隊已空?。?!\n");
}



void menu() {
	printf("********1.入隊          2.出隊*********\n");
	printf("********3.取隊頭元素    4.退出*********\n");
}

int main() {
	LinkQueue Q;
	int choice = 0;;
	InitQueue(Q);

	while (1)
	{
		menu();
		printf("請輸入菜單序號:\n");
		scanf("%d", &choice);
		if (choice == 4) break;
		switch (choice)
		{
		case 1:EnterToQueue(Q); break;
		case 2:DeleteFromQueue(Q); break;
		case 3:GetHeadOfQueue(Q); break;
		default:printf("輸入錯誤?。。n");
		}
		system("pause");
		system("cls");
	}

	return 0;
}

到了這里,關于數(shù)據(jù)結構--循環(huán)隊列、鏈隊的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 數(shù)據(jù)結構(C語言實現(xiàn))——棧和隊列的介紹及基本操作的實現(xiàn)(動態(tài)順序棧+鏈隊)

    今天我們來學習另外兩個線性結構——棧和隊列,棧和隊列是操作受限的線性表,因此,可稱為限定性的數(shù)據(jù)結構。 棧:一種特殊的線性表,其只允許在固定的一端進行插入和刪除元素操作。進行數(shù)據(jù)插入和刪除操作的一端 稱為棧頂,另一端稱為棧底。棧中的數(shù)據(jù)元素遵守

    2023年04月19日
    瀏覽(20)
  • 【數(shù)據(jù)結構與算法】用隊列實現(xiàn)棧&&用棧實現(xiàn)隊列&&設計循環(huán)隊列

    【數(shù)據(jù)結構與算法】用隊列實現(xiàn)棧&&用棧實現(xiàn)隊列&&設計循環(huán)隊列

    ?? 作者:@ 阿亮joy. ?? 專欄:《數(shù)據(jù)結構與算法要嘯著學》 ?? 座右銘:每個優(yōu)秀的人都有一段沉默的時光,那段時光是付出了很多努力卻得不到結果的日子,我們把它叫做扎根 請你僅使用兩個隊列實現(xiàn)一個后入先出(LIFO)的棧,并支持普通棧的全部四種操作(push、top、

    2024年01月20日
    瀏覽(26)
  • 【C++】【數(shù)據(jù)結構】循環(huán)隊列的基本操作(初始化、入隊、出隊、取隊頭元素、遍歷輸出隊列、求隊列長度)順序隊列的算法實現(xiàn)【附全代碼】

    【C++】【數(shù)據(jù)結構】循環(huán)隊列的基本操作(初始化、入隊、出隊、取隊頭元素、遍歷輸出隊列、求隊列長度)順序隊列的算法實現(xiàn)【附全代碼】

    使用c++完成數(shù)據(jù)結構循環(huán)隊列的基本操作,包括(初始化、入隊、出隊、取隊頭元素、遍歷輸出隊列、求隊列長度等),可直接編譯運行。 隊列 又稱為 “先進先出” (FIFO)線性表。限定插入操作只能在隊尾進行,而刪除操作只能在隊首進行。 循環(huán)隊列 ——采用 順序存儲結構

    2023年04月16日
    瀏覽(27)
  • 【數(shù)據(jù)結構與算法分析】使用C語言實現(xiàn)隊列的兩種(帶頭結點與不帶頭結點)鏈式存儲,并且給出一種循環(huán)隊列的設計思想

    【數(shù)據(jù)結構與算法分析】使用C語言實現(xiàn)隊列的兩種(帶頭結點與不帶頭結點)鏈式存儲,并且給出一種循環(huán)隊列的設計思想

    ??當我們編寫程序時,經(jīng)常需要處理各種數(shù)據(jù)結構。隊列是一種常見的數(shù)據(jù)結構,它有著廣泛的應用場景。隊列的基本操作包括入隊和出隊,應用于模擬等待隊列、消息隊列、計算機緩存等場合。 ??在實際編程中,我們可以用不同的數(shù)據(jù)結構來實現(xiàn)隊列。本文主要介紹了

    2024年02月08日
    瀏覽(503)
  • 數(shù)據(jù)結構—循環(huán)隊列(環(huán)形隊列)

    數(shù)據(jù)結構—循環(huán)隊列(環(huán)形隊列)

    循環(huán)隊列是一種線性數(shù)據(jù)結構,其操作表現(xiàn)基于 FIFO(先進先出)原則并且 隊尾被連接在隊首之后以形成一個循環(huán) 。它也被稱為“ 環(huán)形緩沖器 ”。 循環(huán)隊列的一個好處是可以利用這個隊列之前用過的空間。在一個普通隊列里,一旦一個隊列滿了,我們就不能插入下一個元素

    2024年02月11日
    瀏覽(19)
  • 數(shù)據(jù)結構--隊列與循環(huán)隊列

    數(shù)據(jù)結構--隊列與循環(huán)隊列

    ? ? ? ? 隊列是什么,先聯(lián)想一下隊,排隊先來的人排前面先出,后來的人排后面后出;隊列的性質也一樣,先進隊列的數(shù)據(jù)先出,后進隊列的后出;就像圖一的樣子: ?圖1 ? ? ? ? 如圖1,1號元素是最先進的,開始出隊時,那么他就是最先出的,然后12進隊,就應該排在最

    2024年02月10日
    瀏覽(15)
  • 數(shù)據(jù)結構:循環(huán)隊列的實現(xiàn)(leetcode622.設計循環(huán)隊列)

    數(shù)據(jù)結構:循環(huán)隊列的實現(xiàn)(leetcode622.設計循環(huán)隊列)

    ? 目錄 一.循環(huán)隊列簡單介紹 二.用靜態(tài)數(shù)組實現(xiàn)循環(huán)隊列 1.數(shù)組循環(huán)隊列結構設計 2.數(shù)組循環(huán)隊列的堆區(qū)內存申請接口? 3.數(shù)據(jù)出隊和入隊的接口實現(xiàn) 4.其他操作接口 5.數(shù)組循環(huán)隊列的實現(xiàn)代碼總覽? 三.靜態(tài)單向循環(huán)鏈表實現(xiàn)循環(huán)隊列? 1.鏈表循環(huán)隊列的結構設計 2.創(chuàng)建靜態(tài)

    2024年02月03日
    瀏覽(21)
  • 【數(shù)據(jù)結構】循環(huán)隊列

    【數(shù)據(jù)結構】循環(huán)隊列

    ?? 作者簡介:一名在后端領域學習,并渴望能夠學有所成的追夢人。 ?? 個人主頁:蝸牛牛啊 ?? 系列專欄:??數(shù)據(jù)結構、??C++ ?? 學習格言:博觀而約取,厚積而薄發(fā) ?? 歡迎進來的小伙伴,如果小伙伴們在學習的過程中,發(fā)現(xiàn)有需要糾正的地方,煩請指正,希望能夠與

    2024年02月12日
    瀏覽(29)
  • 數(shù)據(jù)結構——循環(huán)隊列詳解

    數(shù)據(jù)結構——循環(huán)隊列詳解

    目錄 一、循環(huán)隊列的定義 二、 循環(huán)隊列的基本操作 三、循環(huán)隊列的實現(xiàn)? 1、循環(huán)隊列的定義 2、循環(huán)隊列的初始化? 3、循環(huán)隊列出隊? 4、循環(huán)隊列入隊? 5、隊列判空 6、 隊列判滿 7、取隊頭元素 8、輸出隊列? 9、求隊列長度? 四、完整代碼? 五、小結? 六、參考文獻 一、

    2024年02月04日
    瀏覽(17)
  • 數(shù)據(jù)結構——循環(huán)隊列的實現(xiàn)

    數(shù)據(jù)結構——循環(huán)隊列的實現(xiàn)

    hello hello~ ,這里是大耳朵土土垚~???? ,歡迎大家點贊????關注????收藏?????? ?? 個人主頁:大耳朵土土垚的博客 ?? 所屬專欄:數(shù)據(jù)結構學習筆記 ??對于數(shù)據(jù)結構順序表、鏈表、堆有疑問的都可以在上面數(shù)據(jù)結構的專欄進行學習哦~ 有問題可以寫在評論區(qū)或者私信

    2024年03月24日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包