#include<stdio.h>
2 #include<stdlib.h>
3 #include<stdbool.h>
4 typedef struct Node
5 {
6 int data;
7 struct Node* next;
8 }Node;
9
10 Node* creat_node(int data)
11 {
12 Node* node = malloc(sizeof(Node));
13 node->data = data;
14 node->next = NULL;
15 return node;
16 }
17 typedef struct List
18 {
19 Node* head;
20 Node* tail;
21 size_t size;
}List;
23
24 List* creat_list()
25 {
26 List* list = malloc(sizeof(List));
27 list->head = NULL;
28 list->tail = NULL;
29 list->size = 0;
30 return list;
31 }
32 void add_head_list(List* list,int data)
33 {
34 Node* node = creat_node(data);
35 if(0 == list->size)
36 {
37 list->head = node;
38 list->tail = node;
39 list->size++;
return;
41 }
42 node->next = list->head;
43 list->head = node;
44 list->size++;
45 for(Node* node1 = list->head;node->next!= NULL;node=node->next)
46 {
47 for(Node* node2 = node1->next;NULL!=node2;node2=node2->next)
48 {
49 if(node1->data>node2->data)
50 {
51 int temp = node1->data;
52 node1->data = node2->data;
53 node2->data = temp;
54 }
55 }
56 }
57 }
void show(List* list)
59 {
60 for(Node* node = list->head;node != NULL;node=node->next)
61 {
62 printf("%d",node->data);
63 }
64 }
65 bool head_del_list(List* list)
66 {
67 if(0 == list->size)return false;
68 if(1 == list->size)
69 {
70 list->head = NULL;
71 list->tail = NULL;
72 list->size--;
73 return true;;
74 }
75 Node* node = list->head->next;
76 list->head = node;
77 list->size--;
78 return true;
}
81 void free_list(List* list)
82 {
83 while(NULL!=list->head)
84 {
85 head_del_list(list);
86 }
87 free(list);
88 }
89 void main()
90 {
91 List* list = creat_list();
92 add_head_list(list,1);
93 add_head_list(list,13);
94 add_head_list(list,11);
95 add_head_list(list,5);
96 show(list);
97 }
简单的链表创建,添加节点(链表数据从小到大排序),显示,销毁函数
最新推荐文章于 2023-01-08 16:32:54 发布