您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# C语言如何实现链队列
## 1. 链队列概述
链队列是一种基于链表实现的队列数据结构,它克服了顺序队列可能产生的"假溢出"问题。与顺序队列相比,链队列具有动态分配内存、无需预先确定队列长度等优势。
### 1.1 队列的基本特性
- **先进先出(FIFO)**:最先入队的元素最先出队
- **两个基本操作**:入队(enqueue)和出队(dequeue)
- **两个关键指针**:队头指针(front)和队尾指针(rear)
### 1.2 链式存储的优势
- 动态内存分配,避免空间浪费
- 理论上只要内存足够,可以无限扩展
- 不存在顺序队列的假溢出问题
## 2. 链队列的数据结构设计
### 2.1 节点结构定义
```c
typedef struct QNode {
int data; // 存储队列元素
struct QNode *next; // 指向下一个节点
} QNode;
typedef struct {
QNode *front; // 队头指针
QNode *rear; // 队尾指针
int size; // 队列当前长度(可选)
} LinkedQueue;
+------+ +------+ +------+ +------+
| front|--->| node1|--->| node2|--->| node3|
+------+ +------+ +------+ +------+
^ ^
| |
+------+ +------+
| rear | | next |--->NULL
+------+ +------+
void InitQueue(LinkedQueue *q) {
q->front = q->rear = NULL;
q->size = 0;
}
int IsEmpty(LinkedQueue *q) {
return q->front == NULL;
// 或者 return q->size == 0;
}
int EnQueue(LinkedQueue *q, int value) {
QNode *newNode = (QNode*)malloc(sizeof(QNode));
if (!newNode) {
printf("Memory allocation failed!\n");
return 0;
}
newNode->data = value;
newNode->next = NULL;
if (IsEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
q->size++;
return 1;
}
int DeQueue(LinkedQueue *q, int *value) {
if (IsEmpty(q)) {
printf("Queue is empty!\n");
return 0;
}
QNode *temp = q->front;
*value = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
q->size--;
return 1;
}
int GetFront(LinkedQueue *q, int *value) {
if (IsEmpty(q)) {
printf("Queue is empty!\n");
return 0;
}
*value = q->front->data;
return 1;
}
void ClearQueue(LinkedQueue *q) {
while (!IsEmpty(q)) {
QNode *temp = q->front;
q->front = q->front->next;
free(temp);
}
q->rear = NULL;
q->size = 0;
}
void DestroyQueue(LinkedQueue *q) {
ClearQueue(q);
// 如果队列结构体本身是动态分配的,还需要free(q)
}
#include <stdio.h>
#include <stdlib.h>
typedef struct QNode {
int data;
struct QNode *next;
} QNode;
typedef struct {
QNode *front;
QNode *rear;
int size;
} LinkedQueue;
void InitQueue(LinkedQueue *q) {
q->front = q->rear = NULL;
q->size = 0;
}
int IsEmpty(LinkedQueue *q) {
return q->front == NULL;
}
int EnQueue(LinkedQueue *q, int value) {
QNode *newNode = (QNode*)malloc(sizeof(QNode));
if (!newNode) {
printf("Memory allocation failed!\n");
return 0;
}
newNode->data = value;
newNode->next = NULL;
if (IsEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
q->size++;
return 1;
}
int DeQueue(LinkedQueue *q, int *value) {
if (IsEmpty(q)) {
printf("Queue is empty!\n");
return 0;
}
QNode *temp = q->front;
*value = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
q->size--;
return 1;
}
int GetFront(LinkedQueue *q, int *value) {
if (IsEmpty(q)) {
printf("Queue is empty!\n");
return 0;
}
*value = q->front->data;
return 1;
}
void ClearQueue(LinkedQueue *q) {
while (!IsEmpty(q)) {
QNode *temp = q->front;
q->front = q->front->next;
free(temp);
}
q->rear = NULL;
q->size = 0;
}
void PrintQueue(LinkedQueue *q) {
if (IsEmpty(q)) {
printf("Queue is empty!\n");
return;
}
printf("Queue elements: ");
QNode *current = q->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
LinkedQueue queue;
InitQueue(&queue);
// 测试入队
for (int i = 1; i <= 5; i++) {
EnQueue(&queue, i*10);
}
PrintQueue(&queue);
printf("Queue size: %d\n", queue.size);
// 测试获取队头
int frontValue;
if (GetFront(&queue, &frontValue)) {
printf("Front element: %d\n", frontValue);
}
// 测试出队
int dequeuedValue;
while (DeQueue(&queue, &dequeuedValue)) {
printf("Dequeued: %d\n", dequeuedValue);
}
// 测试空队列操作
DeQueue(&queue, &dequeuedValue);
GetFront(&queue, &frontValue);
return 0;
}
操作 | 时间复杂度 |
---|---|
入队 | O(1) |
出队 | O(1) |
获取队头 | O(1) |
判空 | O(1) |
typedef struct DQNode {
int data;
struct DQNode *prev;
struct DQNode *next;
} DQNode;
链队列通过链表实现,具有动态扩展、无空间限制等优点,是C语言中实现队列的重要方式。掌握链队列的实现原理和操作特性,能够帮助开发者更好地处理需要先进先出特性的应用场景。在实际开发中,应根据具体需求选择合适的队列实现方式,并注意内存管理和线程安全等问题。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。