forked from ksw2000/Data-Structure-in-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-基礎格式.c
47 lines (41 loc) · 1 KB
/
01-基礎格式.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char data[4];
struct node* next;
};
typedef struct node* Node;
void append(Node* first, Node* trail, char* str) {
Node n = malloc(sizeof(*n));
strcpy(n->data, str);
n->next = NULL;
// 如果為空串列
if (!(*first)) {
*first = n;
*trail = n;
return;
}
(*trail)->next = n;
*trail = (*trail)->next;
}
void linkedListPrintLn(Node first) {
Node current;
for (current = first; current; current = current->next) {
printf("%s -> ", current->data);
}
printf("NULL\n");
}
int main() {
Node first, trail;
first = trail = NULL;
// 示範 linked list append (即佇列的功能)
append(&first, &trail, "BAT");
append(&first, &trail, "CAT");
append(&first, &trail, "EAT");
append(&first, &trail, "FAT");
append(&first, &trail, "HAT");
linkedListPrintLn(first);
// OUTPUT: BAT -> CAT -> EAT -> FAT -> HAT -> NULL
return 0;
}