C語言實(shí)現(xiàn)鏈隊(duì)列代碼
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)鏈隊(duì)列的具體代碼,供大家參考,具體內(nèi)容如下
#include <stdio.h>
/* 隊(duì)列的結(jié)構(gòu)體 */
typedef int DataType;
#define NODE_LEN sizeof(NODE)
/* 隊(duì)列的節(jié)點(diǎn) */
typedef struct stNode
{
DataType data;
struct stNode* next;
}NODE;
/* 隊(duì)列 */
typedef struct stQueue
{
NODE* head; //隊(duì)列的頭
NODE* tail; //隊(duì)列的尾
}QUEUE;
/* 初始化隊(duì)列,不帶頭結(jié)點(diǎn)*/
int initQueue(QUEUE* INQueue)
{
INQueue->head = NULL;
INQueue->tail = NULL;
return 0;
}
/* 從隊(duì)尾插入一個(gè)元素 */
int enQueue(QUEUE* InQueue,DataType InData)
{
NODE* pNewNode = (NODE*)malloc(NODE_LEN);
if (pNewNode == NULL)
{
return -1;
}
pNewNode->data = InData;
pNewNode->next = NULL;
/* 判斷,現(xiàn)在隊(duì)列里面有沒有節(jié)點(diǎn) */
if (InQueue->head == NULL)
{
InQueue->head = pNewNode;
InQueue->tail = pNewNode;
}
else
{
InQueue->tail->next = pNewNode;
InQueue->tail = pNewNode;
}
return 0;
}
/* 遍歷該隊(duì)列 */
int visitQueue(QUEUE InQueue)
{
QUEUE* pstTemp = &InQueue;
/* 判斷隊(duì)列是否為空隊(duì)列 */
if (pstTemp->head == NULL)
{
printf("visitQueue: this queue is empty\n");
return -1;
}
/* 遍歷該隊(duì)列中的所有元素 */
while (pstTemp->head->next != NULL)
{
printf("%d ", pstTemp->head->data);
pstTemp->head = pstTemp->head->next;
}
printf("%d \n", pstTemp->head->data);
return 0;
}
/* 出隊(duì)列 */
int delQueue(QUEUE* InQueue,DataType* OutData)
{
if (InQueue->head == NULL)
{
printf("delQueue: this queue is empty\n");
return -1;
}
*OutData = InQueue->head->data;
NODE* pstTemp = InQueue->head;
InQueue->head = InQueue->head->next;
delete pstTemp;
return 0;
}
/* 判斷隊(duì)列是否是空隊(duì)列 */
int isEmptyQueue(QUEUE InQueue)
{
if (InQueue.head == NULL)
{
return 0; //是空隊(duì)列
}
return 1; //不是空隊(duì)列
}
int main()
{
/* 創(chuàng)建一個(gè)隊(duì)列 */
QUEUE queue;
DataType data;
initQueue(&queue);
/* 入隊(duì)列 */
enQueue(&queue, 12);
enQueue(&queue, 11);
enQueue(&queue, 2);
visitQueue(queue);
/* 出隊(duì)列 */
delQueue(&queue, &data);
visitQueue(queue);
printf("data = %d\n", data);
visitQueue(queue);
if (0 == isEmptyQueue(queue))
{
printf("This is empty queue\n");
}
else
{
printf("This is not empty queue\n");
}
return 0;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語言循環(huán)隊(duì)列與用隊(duì)列實(shí)現(xiàn)棧問題解析
- C語言超詳細(xì)講解棧與隊(duì)列實(shí)現(xiàn)實(shí)例
- C語言數(shù)據(jù)結(jié)構(gòu)進(jìn)階之棧和隊(duì)列的實(shí)現(xiàn)
- C語言實(shí)現(xiàn)循環(huán)隊(duì)列基本操作
- C語言實(shí)現(xiàn)鏈隊(duì)列基本操作
- C語言實(shí)現(xiàn)循環(huán)隊(duì)列
- C語言版實(shí)現(xiàn)鏈隊(duì)列
- C語言實(shí)現(xiàn)鏈隊(duì)列
- C語言?隊(duì)列的實(shí)現(xiàn)全解析
相關(guān)文章
遞歸法求最大公約數(shù)和最小公倍數(shù)的實(shí)現(xiàn)代碼
今天整理了一下用遞歸法求最大公約數(shù)(gcd)和最小公倍數(shù)(lcm)。主要的工作是求最大公約數(shù)。數(shù)學(xué)上可以用輾轉(zhuǎn)法求最大公約數(shù)2013-05-05
c++中string類成員函數(shù)c_str()的用法
c_str()函數(shù)返回一個(gè)指向正規(guī)c字符串的指針,內(nèi)容和string類的本身對(duì)象是一樣的,通過string類的c_str()函數(shù)能夠把string對(duì)象轉(zhuǎn)換成c中的字符串的樣式2013-09-09
C++實(shí)現(xiàn)循環(huán)隊(duì)列和鏈?zhǔn)疥?duì)列的示例
下面小編就為大家分享一篇C++實(shí)現(xiàn)循環(huán)隊(duì)列和鏈?zhǔn)疥?duì)列的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
詳解應(yīng)用程序與驅(qū)動(dòng)程序通信DeviceIoControl
這種通信方式,就是驅(qū)動(dòng)程序和應(yīng)用程序自定義一種IO控制碼,然后調(diào)用DeviceIoControl函數(shù),IO管理器會(huì)產(chǎn)生一個(gè)MajorFunction為IRP_MJ_DEVICE_CONTROL,MinorFunction為自己定義的控制碼的IRP,系統(tǒng)就調(diào)用相應(yīng)的處理IRP_MJ_DEVICE_CONTROL的派遣函數(shù)2021-06-06
Unity3D實(shí)現(xiàn)經(jīng)典小游戲Pacman
這篇文章主要介紹了基于Unity3D制作一做個(gè)經(jīng)典小游戲Pacman,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Unity3D有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12
C語言實(shí)現(xiàn)簡單的學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的學(xué)生學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

