0
点赞
收藏
分享

微信扫一扫

顶部动态菜单栏的使用

星河出山 2023-11-19 阅读 20

目录

前言

1. 队列

1.1 队列的概念

1.2 队列的结构

2. 队列的实现

2.1 队列的定义

2.2 队列的初始化

2.3 入队

2.4 出队

2.5 获取队头元素

2.6 获取队尾元素

2.7 判断空队列

2.8 队列的销毁

3. 队列完整源码

Queue.h

Queue.c


前言

1. 队列

1.1 队列的概念
  • 入队列:进行插入操作的一端称为队尾
  • 出队列:进行删除操作的一端称为队头
1.2 队列的结构

2. 队列的实现

2.1 队列的定义
typedef int QDateType;

typedef struct QueueNode
{
QDateType val;
struct QueueType* next;
}QNode;

typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
2.2 队列的初始化

这里的 size 用来记录队列中数据的个数。

void QueueInit(Queue* pq)
{
assert(pq);

pq->phead = pq->ptail = NULL;
pq->size = 0;
}
2.3 入队

入队相当于尾插,在入队时我们要考虑链表是否为空。

void QueuePush(Queue* pq, QDateType x)
{
assert(pq);

QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->val = x;

if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
2.4 出队

出队相当于头删,与之前不同的是,当我们删除最后一个节点,还要记得处理尾指针。

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--;
}
2.5 获取队头元素

队头元素就是头指针指向的节点的数据域。

QDateType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);

return pq->phead->val;
}
2.6 获取队尾元素

我们通过尾指针可以直接找到队尾,不用遍历链表。

QDateType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->phead);

return pq->ptail->val;
}
2.7 判断空队列

利用bool的函数判断队列是否为空,当尾指针为空时,返回true;当尾指针不为空时,返回false。

bool QueueEmpty(Queue* pq)
{
assert(pq);

return pq->phead == NULL;
}
2.8 队列的销毁
int QueueSize(Queue* pq)
{
assert(pq);

return pq->size;
}

3. 队列完整源码

Queue.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int QDateType;

typedef struct QueueNode
{
QDateType val;
struct QueueType* next;
}QNode;

typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;

void QueueInit(Queue* pq);

void QueueDstroy(Queue* pq);

void QueuePush(Queue* pq, QDateType x);

void QueuePop(Queue* pq);

QDateType QueueFront(Queue* pq);

QDateType QueueBack(Queue* pq);

bool QueueEmpty(Queue* pq);

int QueueSize(Queue* pq);
Queue.c
#include"Queue.h"

void QueueInit(Queue* pq)
{
assert(pq);

pq->phead = pq->ptail = NULL;
pq->size = 0;
}

void QueueDstroy(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;
}

void QueuePush(Queue* pq, QDateType x)
{
assert(pq);

QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->val = x;

if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
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--;
}

QDateType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);

return pq->phead->val;
}

QDateType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->phead);

return pq->ptail->val;
}

bool QueueEmpty(Queue* pq)
{
assert(pq);

return pq->phead == NULL;
}

int QueueSize(Queue* pq)
{
assert(pq);

return pq->size;
}

举报

相关推荐

0 条评论