C語言結構及隊列實現(xiàn)示例詳解
1.隊列的概念及結構
隊列:
只允許一端插入數(shù)據(jù),另一端刪除數(shù)據(jù)的特殊線性表
先進先出FIFO(First In First Out)
入隊列:
進行插入操作的一端稱為隊尾
出隊列:
進行刪除操作的一端稱為隊頭

2. 隊列的實現(xiàn)
隊列也可以數(shù)組和鏈表的結構實現(xiàn),使用鏈表的結構實現(xiàn)更優(yōu)一些,因為如果使用數(shù)組的結構,出隊列在數(shù)組頭上出數(shù)據(jù),效率會比較低。

DFS---深度優(yōu)先遍歷 -- 遞歸/棧實現(xiàn)非遞歸
BFS---廣度優(yōu)先遍歷 -- 隊列
// 鏈式結構:表示隊列
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
typedef int QDatatype;
typedef struct QueueNode
{
QDatatype data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
//初始化隊列
void QueueInit(Queue* pq);
//隊尾入隊列
void QueuePush(Queue* pq , QDatatype x);
//隊頭出隊列
void QueuePop(Queue* pq);
//獲取隊頭元素
QDatatype QueueFront(Queue* pq);
//獲取隊尾元素
QDatatype QueueBack(Queue* pq);
//隊列大小
int QueueSize(Queue* pq);
//判斷隊列是否為空
bool QueueEmpty(Queue* pq);
//銷毀隊列
void QueueDestory(Queue* pq);#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
//初始化隊列
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
//銷毀隊列
void QueueDestory(Queue* pq)
{
QNode* cur = pq->phead ;
while (cur)
{
/*QNode* tmp = cur;
cur = cur->next;
free(tmp);*/
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
//隊尾入隊列
void QueuePush(Queue* pq, QDatatype x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->phead == NULL)//isEmpty
{
assert(pq->ptail==NULL);
pq->phead = newnode;
pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
//判斷隊列是否為空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
//return pq->phead == NULL && pq->ptail == NULL;
}
//隊頭出隊列
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));//assert(pq->phead != NULL);
//1、一個節(jié)點
//2、多個節(jié)點
if (pq->phead->next == NULL)//一個節(jié)點要注意ptail別弄成野指針
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
Queue* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//獲取隊頭元素
QDatatype QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
//獲取隊尾元素
QDatatype QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
//隊列大小
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}//測試
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
//QueuePop(&q);
//QueuePop(&q);
//QueuePop(&q);
//while (!QueueEmpty(&q))
//{
// printf("%d ", QueueFront(&q));
//
// QueuePop(&q);
//}
printf("\n");
//printf("%d ", QueueFront(&q));
//printf("%d ", QueueSize(&q));
//QueuePop(&q);
printf("%d ", QueueBack(&q));
printf("%d ", QueueBack(&q));
printf("%d ", QueueBack(&q));
QueueDestory(&q);
}
int main()
{
TestQueue();
return 0;
}以上就是C語言結構及隊列實現(xiàn)示例詳解的詳細內(nèi)容,更多關于C語言結構隊列的資料請關注腳本之家其它相關文章!
相關文章
C/C++內(nèi)存管理之new與delete的使用及原理解析
這篇文章主要介紹了C/C++內(nèi)存管理之new與delete的使用及原理解析,本文通過實例代碼圖文相結合給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-08-08
C/C++實現(xiàn)對STORM運行信息查看及控制的方法
這篇文章主要介紹了C/C++實現(xiàn)對STORM運行信息查看及控制的方法,需要的朋友可以參考下2014-07-07
C語言數(shù)據(jù)結構創(chuàng)建及遍歷十字鏈表
這篇文章主要介紹了C語言數(shù)據(jù)結構十字鏈表的創(chuàng)建及遍歷,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2021-10-10

