WBlog

wangzhiwei blog

0%

c语言链表操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97


#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;

// 定义链表头结构体
typedef struct LinkedList {
Node* head;
} LinkedList;

// 创建一个空链表
LinkedList* createLinkedList() {
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
if (list == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
list->head = NULL;
return list;
}

// 在链表头部插入节点
void insertAtHead(LinkedList* list, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = list->head;
list->head = newNode;
}

// 删除链表中的第一个节点
void deleteFromHead(LinkedList* list) {
if (list->head == NULL) {
fprintf(stderr, "List is empty\n");
return;
}
Node* temp = list->head;
list->head = list->head->next;
free(temp);
}

// 遍历链表并打印所有节点的数据
void traverseLinkedList(LinkedList* list) {
Node* current = list->head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// 释放链表的所有内存
void freeLinkedList(LinkedList* list) {
Node* current = list->head;
Node* temp;
while (current != NULL) {
temp = current;
current = current->next;
free(temp);
}
free(list);
}

int main() {
// 创建一个空链表
LinkedList* list = createLinkedList();

// 插入节点
insertAtHead(list, 10);
insertAtHead(list, 20);
insertAtHead(list, 30);

// 遍历链表
printf("Linked List after insertions:\n");
traverseLinkedList(list);

// 删除节点
deleteFromHead(list);
printf("Linked List after deleting from head:\n");
traverseLinkedList(list);

// 释放链表内存
freeLinkedList(list);

return 0;
}