-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
64 lines (53 loc) · 1.04 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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* create(int data) {
Node* node = (Node*) malloc(sizeof(Node));
node->next = NULL;
node->data = data;
return node;
}
void append(Node* head, int data) {
Node* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = create(data);
}
void prepend(Node** head, int data) {
Node* p = create(data);
p->next = *head;
*head = p;
}
void destroy(Node* head) {
Node* p = head;
while (p->next != NULL) {
Node* temp = p;
p = p->next;
free(temp);
}
head = NULL;
}
void visualize(Node* head) {
printf("%d", head->data);
Node* p = head;
while (p->next != NULL) {
p = p->next;
printf(" -> %d", p->data);
}
}
int main() {
Node* head = create(2);
append(head, 14);
prepend(&head, 3);
append(head, 8);
append(head, 11);
prepend(&head, 7);
append(head, 4);
visualize(head);
destroy(head);
return 0;
}