-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
62 lines (56 loc) · 959 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node * next;
} node_t;
node_t * head = NULL;
void insert(int val) {
node_t *new_node = malloc(sizeof(node_t));
new_node->val = val;
if (head == NULL) {
head = new_node;
} else {
node_t *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = new_node;
}
printf("Node inserted\n");
}
void print() {
if (head == NULL) {
printf("Empty!\n");
} else {
node_t *curr = head;
while (curr != NULL) {
printf("(%d)->", curr->val);
curr = curr->next;
}
}
}
void delete(int val) {
if (head == NULL) {
return;
}
if (head->val == val) {
head = head->next;
}
node_t *curr = head;
while (curr->next != NULL) {
if (curr->next->val == val) {
node_t *tmp = curr->next;
curr->next = curr->next->next;
free(tmp);
break;
}
curr = curr->next;
}
}
int main() {
insert(1);
insert(2);
print();
return 0;
}