-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathdoubly_linked_list.c
73 lines (67 loc) · 1.71 KB
/
doubly_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
#include <stdio.h>
#include <stdlib.h>
//Node struct for the Linked List
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
//An empty list is created.
void create_list(struct Node **head_ref) {
*head_ref = NULL;
printf("List created successfully.");
return;
}
/* Given reference to the head of the list, this function
inserts a node at the beginning of the List*/
void insert(struct Node **head_ref, int new_data) {
//Allocate memory for the node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
/*Given reference to the head of the list, insert_before()
adds a node before a given node*/
void insert_before(struct Node *next_node, int new_data) {
if (next_node == NULL) {
printf("The given Next node is NULL");
return;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
}
// Print the list in both directions
void print_list(struct Node *node) {
struct Node* last;
printf("\nTraversal in forward direction \n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\nTraversal in reverse direction \n");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}
printf("\n");
}
int main() {
struct Node *head;
create_list(&head);
insert(&head, 7);
insert(&head, 1);
insert(&head, 4);
insert_before(head->next, 8);
print_list(head);
return 0;
}