mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:26:30 +08:00
feat(codes/c): Add linkedlist_queue.c (#413)
* feat(codes/c): Add linkedlist_queue.c * feat(codes/c): Prevent null pointer access errors in linkedlist_queue.c * Update linkedlist_queue.c * Update linkedlist_queue.c --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
be0c965886
commit
f73b6a3654
2 changed files with 130 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
|||
add_executable(array_stack array_stack.c)
|
||||
add_executable(linkedlist_stack linkedlist_stack.c)
|
||||
add_executable(array_queue array_queue.c)
|
||||
add_executable(linkedlist_queue linkedlist_queue.c)
|
||||
|
|
128
codes/c/chapter_stack_and_queue/linkedlist_queue.c
Normal file
128
codes/c/chapter_stack_and_queue/linkedlist_queue.c
Normal file
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* File: linkedlist_queue.c
|
||||
* Created Time: 2023-03-13
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
struct LinkedListQueue {
|
||||
ListNode *front, *rear;
|
||||
int queSize;
|
||||
};
|
||||
|
||||
typedef struct LinkedListQueue LinkedListQueue;
|
||||
|
||||
/* 构造方法 */
|
||||
LinkedListQueue *newLinkedListQueue() {
|
||||
LinkedListQueue *queue = (LinkedListQueue *) malloc(sizeof(LinkedListQueue));
|
||||
queue->front = NULL;
|
||||
queue->rear = NULL;
|
||||
queue->queSize = 0;
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
void delLinkedListQueue(LinkedListQueue *queue) {
|
||||
// 释放所有结点
|
||||
for (int i=0; i<queue->queSize && queue->front != NULL; i++) {
|
||||
ListNode *tmp = queue->front;
|
||||
queue->front = queue->front->next;
|
||||
free(tmp);
|
||||
}
|
||||
// 释放 queue 结构体
|
||||
free(queue);
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size(LinkedListQueue *queue) {
|
||||
return queue->queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty(LinkedListQueue *queue) {
|
||||
return (size(queue) == 0);
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
void push(LinkedListQueue *queue, int num) {
|
||||
// 尾结点处添加 node
|
||||
ListNode *node = newListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
if (queue->front == NULL) {
|
||||
queue->front = node;
|
||||
queue->rear = node;
|
||||
}
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
else {
|
||||
queue->rear->next = node;
|
||||
queue->rear = node;
|
||||
}
|
||||
queue->queSize++;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek(LinkedListQueue *queue) {
|
||||
assert(size(queue) && queue->front);
|
||||
return queue->front->val;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
void poll(LinkedListQueue *queue) {
|
||||
int num = peek(queue);
|
||||
ListNode *tmp = queue->front;
|
||||
queue->front = queue->front->next;
|
||||
free(tmp);
|
||||
queue->queSize--;
|
||||
}
|
||||
|
||||
/* 打印队列 */
|
||||
void printLinkedListQueue(LinkedListQueue *queue) {
|
||||
int arr[queue->queSize];
|
||||
// 拷贝链表中的数据到数组
|
||||
int i;
|
||||
ListNode *node;
|
||||
for (i=0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++){
|
||||
arr[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
printArray(arr, queue->queSize);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
LinkedListQueue *queue = newLinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
push(queue, 1);
|
||||
push(queue, 3);
|
||||
push(queue, 2);
|
||||
push(queue, 5);
|
||||
push(queue, 4);
|
||||
printf("队列 queue = ");
|
||||
printLinkedListQueue(queue);
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peekNum = peek(queue);
|
||||
printf("队首元素 peek = %d\r\n", peekNum);
|
||||
|
||||
/* 元素出队 */
|
||||
poll(queue);
|
||||
printf("出队元素 poll = %d,出队后 queue = ", peekNum);
|
||||
printLinkedListQueue(queue);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int queueSize = size(queue);
|
||||
printf("队列长度 size = %d\r\n", queueSize);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = empty(queue);
|
||||
printf("队列是否为空 = %s\r\n", isEmpty ? "true" : "false");
|
||||
|
||||
// 释放内存
|
||||
delLinkedListQueue(queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in a new issue