-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
103 lines (83 loc) · 1.87 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
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node* next;
struct node* prev;
};
struct list {
struct node* head;
struct node* tail;
};
struct node* create_node(int);
struct list* create_list();
struct node* search_list(struct list*, int);
void insert_node(struct list*, struct node*);
void delete_node(struct list*, struct node*);
void print_list(struct list*);
int main() {
struct list* l = create_list();
struct node* n = create_node(5);
l->head = n;
for (int i = 0; i < 10; i++) {
n = create_node(i);
insert_node(l, n);
}
print_list(l);
printf("Delete node of key = 4\n");
n = search_list(l, 4);
delete_node(l, n);
print_list(l);
return 0;
}
struct node* create_node(int k) {
struct node* n = malloc(sizeof(struct node));
n->key = k;
n->next = NULL;
n->prev = NULL;
return n;
}
struct list* create_list() {
struct list* l = malloc(sizeof(struct list));
l->head = NULL;
l->tail = NULL;
}
struct node* search_list(struct list* l, int k) {
struct node* temp = l->head;
while(temp != NULL && temp->key != k) {
temp = temp-> next;
}
return temp;
}
void insert_node(struct list* l, struct node* n) {
if (l->head != NULL) {
(l->head)->prev = n;
}
n->next = l->head;
n->prev = NULL;
l->head = n;
}
void delete_node(struct list* l, struct node* n) {
if (n->prev != NULL) {
(n->prev)->next = n->next;
}
if (n->next != NULL) {
(n->next)->prev = n->prev;
}
free(n);
}
void print_list(struct list* l) {
if (l->head == NULL) {
printf("Empty Linked list\n");
return;
}
printf("Liked list content:\n");
struct node* temp = l->head;
printf("%d", temp->key);
temp = temp->next;
while (temp != NULL) {
printf(" -> %d", temp->key);
temp = temp->next;
}
printf("\n");
}