3

链式队列的实现_五个板栗的技术博客_51CTO博客

 2 years ago
source link: https://blog.51cto.com/u_15515702/5364594
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
链式队列的实现_入队

一、什么是队列

只允许在一端进行插入数据,在另一端进行删除数据的特殊的线性表。

链式队列的实现_链式队列_02

二、队列的特点

1.队列是一种操作受限的线性表。

2.队头作为允许删除的一端。

3.队尾作为允许插入的一端。

4.没有元素的队列称为空队列。

三、队列的基本操作

1.队列的初始化

2.队列的判空操作

3.队列的入队操作

4.队列的出队操作

5.获取队列中的元素

7.计算队列中元素的个数

6.销毁队列

四、代码实现

(1)Queue.c文件

#include "Queue.h"
//创建新结点
static QNode *BuyLQNode(QElemType data)
{
QNode *pLQNode = (QNode *)malloc(sizeof(QNode));
if (NULL == pLQNode)
{
printf("申请空间失败!\n");
assert(pLQNode);
}
pLQNode->data = data;
pLQNode->_pNext = NULL;

return pLQNode;
}
void LQueueInit(LQueue *q)
{
assert(q);
q->pFront = q->pRear = NULL;
}
void LQueuePush(LQueue *q, QElemType data)
{
assert(q);
if (NULL == q->pFront)
{
q->pFront = q->pRear = BuyLQNode(data);
return;
}
q->pRear->_pNext = BuyLQNode(data);
q->pRear = q->pRear->_pNext;
}
void LQueuePop(LQueue *q)
{
assert(q);
QNode *pDel;
if (NULL == q->pFront)
{
return;
}

if (q->pFront == q->pRear)
{
q->pRear = NULL;
}

pDel = q->pFront;
q->pFront = q->pFront->_pNext;
free(pDel);
}
QElemType LQueueTop(LQueue *q)
{
assert(q);
return q->pFront->data;
}
QElemType LQueueBack(LQueue *q)
{
assert(q);
return q->pRear->data;
}
int LQueueSize(LQueue *q)
{
int count = 0;
QNode *pCur;
assert(q);
pCur = q->pFront;
while (pCur)
{
pCur = pCur->_pNext;
count++;
}
return count;
}
int LQueueEmpty(LQueue *q)
{
return NULL == q->pFront;
}
void LQueueDestroy(LQueue* q)
{
assert(q);
QNode* pCur=q->pFront;
while(pCur)
{
QNode* next=pCur->_pNext;
free(pCur);
pCur=next;
}
q->pFront=q->pRear=NULL;
}
void LQueuePrint(const LQueue* q)
{
for(QNode* cur=q->pFront;cur!=NULL;cur=cur->_pNext)
{
printf("%d-->",cur->data);
}
printf("\n");
}

(2)Queue.h文件

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef int QElemType;
//typedef struct BTNode* QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *_pNext;
}QNode;

typedef struct LQueue
{
QNode *pFront;
QNode *pRear;
}LQueue;


//初始化
void LQueueInit(LQueue *q);

//入队列
void LQueuePush(LQueue *q, QElemType data);

//出队列
void LQueuePop(LQueue *q);

//返回队头元素
QElemType LQueueTop(LQueue *q);

//返回返回队列长度
int LQueueSize(LQueue *q);

//队列是否为空
int LQueueEmpty(LQueue *q);
void LQueueDestroy(LQueue* q);
void LQueuePrint(const LQueue* q);

(3)test.c文件

include "Queue.h"
void menu() {
printf("***********请输入你的操作 *************\n");
printf("***********1.初始化队列 *************\n");
printf("***********2.入队 *************\n");
printf("***********3.出队 *************\n");
printf("***********4.获取队列大小 *************\n");
printf("***********5.查看队列元素 *************\n");
printf("***********6.销毁队列 *************\n");
printf("***********7.退出 *************\n");
printf("\n");
}
int main() {
LQueue q;
int enEle = 0;
int deEle = 0;
int Qsize = 0;
int ope=0;
bool loop = true;
while (loop) {
menu();
printf("请输入你的操作:");
scanf("%d", &ope);
switch (ope)
{
case 1:
LQueueInit(&q);
printf("初始化队列成功!\n");
break;
case 2:
printf("输入入队元素:");
scanf("%d", &enEle);
LQueuePush(&q, enEle);
printf("入队成功!\n");
break;
case 3:
printf("输入出队元素:");
scanf("%d", &deEle);
LQueuePop(&q);
printf("出队元素为:%d\n", deEle);
break;
case 4:
Qsize = LQueueSize(&q);
printf("%d\n", Qsize);
break;
case 5:
printf("队列的元素为:\n");
LQueuePrint(&q);
break;
case 6:
LQueueDestroy(&q);
printf("销毁成功\n");
break;
case 7:
loop = false;
printf("退出成功!\n");
break;

default:
printf("输入不合理,请重新输入!\n");
break;
}
}

}

(4)makefile文件

src=$(wildcard *.c)
obj=$(patsubst %.c,%.o,$(src))
ALL:test
test:$(obj)
gcc $^ -pthread -o $@

clean:
-rm -rf $(obj) test
%.o:%.c
gcc -c $< -o $@
.PHONY:clean ALL

五、结果显示

链式队列的实现_初始化_03
链式队列的实现_C语言_04
链式队列的实现_C语言_05

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK