【C语言】链表实现队列


前言

利用动态数组实现了循环队列,这是静态的队列,缺点是需要预设大小,当队列满时,无法再插入新的数据,只有等队头的数据被取走以后才能往队列放入新的数据。
除了动态数组分配实现的队列之外,还可以使用链表实现队列,这种方式动态创建节点需要的内存,当有新的数据节点要加入时,才去申请内存空间,不需要预设大小,整个队列需要的内存空间不需要连续,并且插入删除更容易实现。但是同时也带来存取速度慢的缺点,操作也比数组的方式更加复杂。
其实用链表实现队列的方式十分简单,只需要在单链表的基础上,增加一个尾指针即可。因为队列的特点是“先进先出”,因此我们只需要在一头一尾两个指针,就可以快速地在队头取出数据,在队尾插入数据。


提示:以下是本篇文章正文内容,下面案例可供参考

一、实现过程

1、定义结构体结构

  • 定义结构体结构
typedef struct Queue_Node {
	QUEUE_TYPE data;
	struct Queue_Node *next;
}QueueNode;

2、创建一个结构体指针

  • ①、创建头指针:QueueNode *head;
  • ②、创建尾指针:QueueNode *tail;
QueueNode *head;
QueueNode *tail;

3、数据入队列

  • ①、创建一个结构体指针 new_node
  • ②、通过malloc动态分配地址块,将该地址块的首地址传给 new_node
  • ③、通过判断 new_node == NULL 查看 malloc 是否分配内存空间成功,为空则表示分配失败,打印错误信息
  • ④、将数据赋值给 new_node 指针指向的data域
  • ⑤、通过判断 head == NULL 确定该队列是否为
  • 1.1)队列为空则将头指针 head尾指针 tail 都指向新节点【new_node 指向的内存空间】
  • 2.1)队列不为空则将 “ 尾指针 tail 指向的内存空间中的next域 ” 指向 “ new_node 指向的内存空间
  • 2.2)将尾指针 tail 指向 “ new_node 指向的内存空间 ”【尾指针指向新节点】
/*
 * Data queuing
 */
void enqueue(QUEUE_TYPE value) {
	QueueNode *new_node;
	new_node = (QueueNode *)malloc(sizeof(QueueNode));
	if (new_node == NULL)
		perror("malloc fail\n");
	new_node->data = value;
	if (head == NULL) {
		head = new_node;
		tail = new_node;
	} else {
		tail->next = new_node;
		tail = new_node;
	}
}

4、数据出队列

  • ①、判断该队列是否为空
  • ②、创建一个结构体指针 front_node
  • ③、将 “ 头指针指向的内存空间 ” 传给 “ front_node ”,使得 front_node 指向第一个节点
  • ④、将 “ front_node 指向内存空间中的next的值 ”【第二个节点的首地址】传给 头指针 head,使得头指针 head 指向第二个节点
  • ⑤、将 front_node 指向的内存空间通过 free() 释放掉【释放第一个节点的内存空间
  • ⑥、如果这时 头指针 head 指向为空,说明该队列已为空,将尾指针 tail 也设置为指向为空
/*
 * Data out of queue
 */
void dequeue(void) {
	assert(!is_empty());
	QueueNode *front_node;
	front_node = head;
	head = front_node->next;
	free(front_node);
	if (head == NULL)
		tail = NULL;
}

5、判断队列是否为空

  • ①、在队列中,头指针 head 指向为空,则说明该队列为空,所以只需要返回 head == NULL 的结果
  • 返回值为1:表明队列为空
  • 返回值为0:说明队列不为空
/*
 * Is the Queue empty?
 * return: 
 * 0 - is not empty
 * 1 - empty
 */
int is_empty(void) {
	return head == NULL;
}

6、获取队列第一个数据

  • ①、判断队列是否为空
  • ②、返回头指针 head 所指向 内存空间 中的 data域数据
/*
 * Gets the first data in the queue 
 */
QUEUE_TYPE get_head(void) {
	assert(!is_empty());
	return head->data;
}

7、获取队列最后一个数据

  • ①、判断队列是否为空
  • ②、返回尾指针 tail 所指向 内存空间 中的 data域数据
/*
 * Gets the last data in the queue
 */
QUEUE_TYPE get_tail(void) {
	assert(!is_empty());
	return tail->data;
}

8、清空队列数据

  • ①、只要当前队列不为空,就逐个对数据进行出队列操作,直到队列为空
/*
 * Destory Queue
 */
void empty_queue(void) {
	while (!is_empty())
		dequeue();
}

二、具体代码

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>

#define QUEUE_TYPE int

typedef struct Queue_Node {
	QUEUE_TYPE data;
	struct Queue_Node *next;
}QueueNode;

QueueNode *head;
QueueNode *tail;

/*
 * Is the Queue empty?
 * return: 
 * 0 - is not empty
 * 1 - empty
 */
int is_empty(void) {
	return head == NULL;
}

/*
 * Data queuing
 */
void enqueue(QUEUE_TYPE value) {
	QueueNode *new_node;
	new_node = (QueueNode *)malloc(sizeof(QueueNode));
	if (new_node == NULL)
		perror("malloc fail\n");
	new_node->data = value;
	if (head == NULL) {
		head = new_node;
		tail = new_node;
	} else {
		tail->next = new_node;
		tail = new_node;
	}
}

/*
 * Data out of queue
 */
void dequeue(void) {
	assert(!is_empty());
	QueueNode *front_node;
	front_node = head;
	head = front_node->next;
	free(front_node);
	if (head == NULL)
		tail = NULL;
}

/*
 * Gets the first data in the queue 
 */
QUEUE_TYPE get_head(void) {
	assert(!is_empty());
	return head->data;
}

/*
 * Gets the last data in the queue
 */
QUEUE_TYPE get_tail(void) {
	assert(!is_empty());
	return tail->data;
}

/*
 * Destory Queue
 */
void empty_queue(void) {
	while (!is_empty())
		dequeue();
}

/*
 * Print Queue information
 */
void print(void) {
	printf("List data: ");
	if (is_empty()) {
		printf("NULL\n");
	} else {
		QueueNode *node;
		node = head;
		while (node) {
			printf("%d ", node->data);
			node = node->next;
		}
		printf("\n");
		printf("front data: %d\t", get_head());
		printf("tail data: %d\n", get_tail());
	}
	printf("\n");
}

/*
 * The main function
 */
int main(int argc, char **argv) {
	print();
	printf("Data queuing\n");
	enqueue(10); enqueue(8); enqueue(6);
	enqueue(4);  enqueue(2); enqueue(0);
	print();
	printf("Data out of queue\n");
	dequeue(); dequeue();
	print();
	printf("Data queuing\n");
	enqueue(1); enqueue(3); enqueue(5);
	print();
	printf("Empty queue\n");
	empty_queue();
	print();
} /*-----End of main function-----*/





三、测试结果

在这里插入图片描述

总结

以上是对链表实现队列的一些理解,如有写的不好的地方,还请各位大佬不吝赐教。

  • 11
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C语言链表实现循环队列的代码实现和详细步骤: 1.定义循环队列的结构体和初始化函数 ```c #define MAXSIZE 100 // 循环队列的最大长度 typedef struct SLnode { int data; struct SLnode* next; }SLnode; typedef struct CQueue { SLnode* front; // 队头指针 SLnode* tail; // 队尾指针 int length; // 队列长度 }CQueue; // 初始化循环队列 void InitCQueue(CQueue* pq) { pq->front = pq->tail = (SLnode*)malloc(sizeof(SLnode)); pq->front->next = NULL; pq->length = 0; } ``` 2.实现入队操作 ```c // 入队操作 void EnCQueue(CQueue* pq, int x) { SLnode* s = (SLnode*)malloc(sizeof(SLnode)); s->data = x; s->next = NULL; pq->tail->next = s; pq->tail = s; pq->length++; } ``` 3.实现出队操作 ```c // 出队操作 int DeCQueue(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法出队!\n"); return -1; } SLnode* p = pq->front->next; int x = p->data; pq->front->next = p->next; if (pq->tail == p) { pq->tail = pq->front; } free(p); pq->length--; return x; } ``` 4.实现获取队列长度的函数 ```c // 获取队列长度 int CQueueLength(CQueue* pq) { SLnode* cur = pq->front->next; int size = 0; while (cur != NULL) { cur = cur->next; ++size; } return size; } ``` 5.实现判断队列是否为空的函数 ```c // 判断队列是否为空 int IsCQueueEmpty(CQueue* pq) { if (pq->front == pq->tail) { return 1; } else { return 0; } } ``` 6.实现判断队列是否已满的函数 ```c // 判断队列是否已满 int IsCQueueFull(CQueue* pq) { if (pq->length == MAXSIZE) { return 1; } else { return 0; } } ``` 7.实现获取队头元素的函数 ```c // 获取队头元素 int GetCQueueFront(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队头元素!\n"); return -1; } return pq->front->next->data; } ``` 8.实现获取队尾元素的函数 ```c // 获取队尾元素 int GetCQueueRear(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队尾元素!\n"); return -1; } return pq->tail->data; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simply myself

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值