怎樣編輯網(wǎng)頁網(wǎng)站關(guān)鍵詞排名優(yōu)化
數(shù)據(jù)結(jié)構(gòu)——隊(duì)列的實(shí)現(xiàn)(單鏈表)
- 一.隊(duì)列
- 1.1隊(duì)列的概念及結(jié)構(gòu)
- 二.隊(duì)列的實(shí)現(xiàn)
- 2.1 頭文件的實(shí)現(xiàn)——(Queue.h)
- 2.2 源文件的實(shí)現(xiàn)—— (Queue.c)
- 2.3 源文件的實(shí)現(xiàn)—— (test.c)
- 三.隊(duì)列的實(shí)際數(shù)據(jù)測(cè)試展示
- 3.1正常出隊(duì)列入隊(duì)列
- 3.2 入隊(duì)列的同時(shí)存在出隊(duì)列
一.隊(duì)列
1.1隊(duì)列的概念及結(jié)構(gòu)
二.隊(duì)列的實(shí)現(xiàn)
2.1 頭文件的實(shí)現(xiàn)——(Queue.h)
Queue.h
#pragma once#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>typedef int QDataType;
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;//初始化/銷毀
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);//出隊(duì)/入隊(duì)
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
//獲取隊(duì)頭元素/隊(duì)尾元素
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
//判空/統(tǒng)計(jì)隊(duì)列元素個(gè)數(shù)
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
2.2 源文件的實(shí)現(xiàn)—— (Queue.c)
Queue.c
#include"Queue.h"//初始化/銷毀
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* Next = cur->next;free(cur);cur = Next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}//隊(duì)尾入隊(duì)/隊(duì)首出隊(duì)
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* Newnode = (QNode*)malloc(sizeof(QNode));if (Newnode == NULL){perror("malloc fail");return;}Newnode->val = x;Newnode->next = NULL;if (pq->phead == NULL){pq->phead = pq->ptail = Newnode;}else{pq->ptail->next = Newnode;pq->ptail = pq->ptail->next;}pq->size++;
}
void QueuePop(Queue* pq)
{assert(pq);assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;if (pq->phead == NULL)pq->ptail = NULL;pq->size--;}//獲取隊(duì)頭元素/隊(duì)尾元素
QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;}
//判空/統(tǒng)計(jì)隊(duì)列元素個(gè)數(shù)
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
2.3 源文件的實(shí)現(xiàn)—— (test.c)
test.c
#include"Queue.h"int main()
{Queue S;QueueInit(&S);QueuePush(&S, 1);QueuePush(&S, 2);printf("%d ", QueueFront(&S));QueuePop(&S);QueuePush(&S, 3);QueuePush(&S, 4);printf("%d ", QueueFront(&S));QueuePop(&S);QueuePush(&S, 5);while (!QueueEmpty(&S)){printf("%d ", QueueFront(&S));QueuePop(&S);}QueueDestroy(&S);
}
三.隊(duì)列的實(shí)際數(shù)據(jù)測(cè)試展示
1.出入隊(duì)列的方式:隊(duì)尾進(jìn)入,對(duì)首出隊(duì)列。
2.出隊(duì)列和入隊(duì)列的關(guān)系是:一對(duì)一的。