日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言簡明講解隊列的實現方法_C 語言

作者:平凡的人1 ? 更新時間: 2022-06-20 編程語言

前言

大家好啊,我又雙叒叕來水博客了,道路是曲折的,前途是光明的,事物是呈螺旋式上升的,事物最終的發展結果還是我們多多少少能夠決定的,好啦,吹水結束,這與這篇博客的主題并沒有太多聯系。關于棧和隊列這一板塊本來是想不寫(就是想偷懶),但是想了想,覺得這樣不太好,關于數據結構這一塊可能會有缺失,所以最終還是決定寫,必須補齊這一塊,恰好最近有時間寫博客,所以還是寫了,這篇博客將介紹隊列的知識點,理解鏈表那一塊的操作后,棧和隊列的相關操作還是比較容易去理解的。

隊列的表示和實現

隊列的概念及結構

隊列:只允許在一端進行插入數據操作,在另一端進行刪除數據操作的特殊線性表,隊列具有先進先出FIFO(First in First Out)

入隊列:進行插入操作的一端稱為隊尾

出隊列:進行刪除操作的一端稱為隊頭

敲黑板,開始摸魚:

其實也沒什么重點啦,都是一些基本的概念性東西,主要有:

1.要理解FIFO代表的意思

2.什么是隊尾隊頭,別弄混了

隊列的實現有兩種方法:

數組:不適合,隊頭出數據需要挪動數據,一般還會出現假溢出,循環隊列啥的

鏈表:單鏈表較合適,頭刪尾插,效率較高

當然,并不是說一定要用哪種,由于課本是以數組為例,這里以單鏈表為例

代碼實現

還是老樣子,分為三部分,直接上手代碼,來,跟著我一起敲

Queue.h

#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;
 typedef struct Queue
{
	QNode* head;
	QNode* tail;
}Queue;
 //初始化
 void QueueInit(Queue* pq);
 //銷毀
 void QueueDestory(Queue* pq);
 //隊尾入
 void QueuePush(Queue* pq, QDataType x);
 //隊頭出
 void QueuePop(Queue* pq);
 //取隊頭數據
 QDataType QueueFront(Queue* pq);
 //取隊尾數據
 QDataType QueuBack(Queue* pq);
 //個數
 int QueueSize(Queue* pq);
 //判斷是否為空
 bool QueueEmpty(Queue* pq);

Queue.c

#include "Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}
void QueueDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
//隊尾入
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
//隊頭出
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	//1.一個(防止野指針)
	//2.多個
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QDataType QueuBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->tail->data;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		++size;
		cur = cur->next;
	}
	return size;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

Test.c

#include "Queue.h"
void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	printf("%d ", QueueFront(&q));
	QueuePop(&q);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	QueueDestory(&q);
}
int main()
{
	TestQueue();
	return 0;
}

束語

關于堆棧和隊列的相關操作就說到這里了,如果對你有幫助的話,那就點個贊把!

原文鏈接:https://blog.csdn.net/weixin_60478154/article/details/124133722

欄目分類
最近更新