-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
203 lines (160 loc) · 4.58 KB
/
linked_list.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node{
int data;
struct Node *next;
}node;
// typedef struct Node node;
node* head; // 要宣告global 才方便操作
node* deleteNode(node* linkedList, int find){
node* cursor = linkedList;
node* follow = linkedList;
if (cursor == NULL){
printf("Not Found\n");
return NULL;
}
// find 在第一個
if (cursor->data == find){
linkedList = linkedList->next;
free(cursor);
return linkedList;
}
cursor = linkedList->next;
while (cursor != NULL){
if (cursor->data == find){
follow->next = cursor->next;
free(cursor);
return linkedList;
}
cursor = cursor->next;
follow = follow->next;
}
printf("Element not found...\n");
return linkedList;
}
int listLength(node* linkedList){
int count = 0;
if (linkedList == NULL)
return 0;
node* traverse = linkedList;
while (traverse != NULL){
count++;
traverse = traverse->next;
}
return count;
}
node* addNode(node* linkedList,int insertIndex, int val){
if (insertIndex < 0 || insertIndex >= listLength(linkedList) + 1){
printf("Index Error\n");
return linkedList;
}
node* temp = linkedList;
node* skip;
// 插入在最前面
if (insertIndex == 0){
skip = (node*)malloc(sizeof(node));
skip->next = temp;
skip->data = val;
return skip;
}
for (int i=1;i<=insertIndex;i++){
if (i == insertIndex){
// 插入在最後面
if (insertIndex == listLength(linkedList)){
temp->next = (node*)malloc(sizeof(node));
temp = temp->next;
temp->data = val;
temp->next = NULL;
return linkedList;
}
// 插入在中間
skip = temp->next;
temp->next = (node*)malloc(sizeof(node));
temp = temp->next;
temp->data = val;
temp->next = skip;
return linkedList;
}
temp = temp->next;
}
}
/* 又有一隻貓貓在這邊留下了腳印 2022/9/25(日)
5rrrrrrr
fortran'''''''''''''''''''''''
}
6555555555555555555tttttttttttttttttt6
*/
node* updateNode(node* linkedList, int nodeIndex, int newVal){
node* cursor = linkedList;
for (int i=0;i<nodeIndex;i++)
cursor = cursor->next;
cursor->data = newVal;
return linkedList;
}
int searchNodeVal(node* linkedList, int val){
int index;
return index;
}
// not finished
void sortList(node* linkedList){
node* temp1 = linkedList;
node* temp2 = linkedList->next;
int reg;
for (int i=0;i<listLength(linkedList)-1;i++){
for (int j=0;j<listLength(linkedList)-1-i;j++){
if (temp1->data > temp2->data){
reg = temp1->data;
temp1->data = temp2->data;
temp2->data = reg;
}
temp1 = temp1->next;
temp2 = temp2->next;
}
}
}
void printAll(node* linkedList){
node* cursor = linkedList;
if (cursor == NULL)
printf("Nothing in list...\n");
else
// while (cursor != NULL){
// printf("%d - ",cursor->data);
// cursor = cursor->next;
// }
for (;cursor != NULL; cursor = cursor->next)
printf("%d - ",cursor->data);
printf("\n");
}
void createLinkedList(int nodeCount){
head = (node*)malloc(sizeof(node));
node* current;
if (nodeCount == 0)
return;
head->data = 0;
head->next = NULL;
current = head;
for (int i=1;i<nodeCount;i++){
current->next = (node*)malloc(sizeof(node)); // 先從現有的生出下個節點
current = current->next; // 把current移動,不先移動就改data改動到的是上一個(head)的值
current->data = i;
current->next = NULL; // 讓程式不會無限循環
}
}
int main(){
int nodeCount;
printf("Enter how many Nodes: ");
scanf("%d",&nodeCount);
createLinkedList(nodeCount);
addNode(head, listLength(head),1);
addNode(head, listLength(head),2);
addNode(head, listLength(head),4);
addNode(head, listLength(head),1);
addNode(head, listLength(head),3);
addNode(head, listLength(head),4);
printAll(head);
sortL(head);
printAll(head);
// 曾經有一隻貓貓說: -=0perdfgttttttttttttttttttttttttttttttthyjjjjjjjjjuiiiiiiii 貓貓在此給我靈感www
return 0;
}