-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Doubly-Linkedlist.c
97 lines (58 loc) · 1.21 KB
/
Doubly-Linkedlist.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int key;
int data;
struct node *next;
struct node *previous;
};
struct node *head = NULL;
struct node *current = NULL;
// Insert from the beginign
void insertFirst(int key, int data){
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
link->previous = NULL;
link->next = head;
head = link;
}
//Inserting after a certain key
void insertAfterKey(int key, int data, int keyIn){
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
link->key = key;
struct node *temp = head;
struct node *nx = NULL;
while(temp->key != keyIn){
temp = temp->next;
}
nx = temp->next;
temp->next = link;
link->previous = temp;
link->next = nx;
nx->previous = link;
}
//Printing the linked list
void printList(){
struct node *temp = head;
printf("Printing the list [ ");
while(temp != NULL){
printf("(%d, %d)", temp->key, temp->data);
temp = temp->next;
}
printf(" ]");
}
// Main method
int main()
{
insertFirst(0, 0);
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
printList();
insertAfterKey(9, 90, 2);
printf("\n");
printList();
return 0;
}