-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly linked list.c
152 lines (152 loc) · 4.47 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * prev;
struct node * next;
}*head, *last;
void createList(int n)
{ int i, data;
struct node *newNode;
if(n >= 1)
{ head = (struct node *)malloc(sizeof(struct node));
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++)
{ newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last;
newNode->next = NULL;
last->next = newNode;
last = newNode;
}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void displayList()
{ struct node * temp;
int n = 1;
if(head == NULL)
printf("List is empty.\n");
else
{ temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{ printf("DATA of %d node = %d\n", n, temp->data);
n++;
temp = temp->next;
}
}
}
void insertAtBeginning(int data)
{ struct node * newNode;
if(head == NULL)
printf("Error, List is Empty!\n");
else
{ newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
head->prev = newNode;
head = newNode;
printf("NODE INSERTED SUCCESSFULLY AT THE BEGINNING OF THE LIST\n");
}
}
void insertAtEnd(int data)
{ struct node * newNode;
if(last == NULL)
printf("Error, List is empty!\n");
else
{ newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = last;
last->next = newNode;
last = newNode;
printf("NODE INSERTED SUCCESSFULLY AT THE END OF LIST\n");
}
}
void insertAtN(int data, int position)
{ int i;
struct node * newNode, *temp;
if(head == NULL)
printf("Error, List is empty!\n");
else
{ temp = head;
i=1;
while(i<position-1 && temp!=NULL)
{ temp = temp->next;
i++;
}
if(position == 1)
insertAtBeginning(data);
else if(temp == last)
insertAtEnd(data);
else if(temp!=NULL)
{ newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = temp->next;
newNode->prev = temp;
if(temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
else
printf("Error, Invalid position\n");
}
}
int main()
{ int n, data, choice=1;
head = NULL;
last = NULL;
while(choice != 0)
{ printf("1. Create List\n");
printf("2. Insert node - at beginning\n");
printf("3. Insert node - at end\n");
printf("4. Insert node - at N\n");
printf("5. Display list\n");
printf("0. Exit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{ case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
printf("Enter data of first node : ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 3:
printf("Enter data of last node : ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 4:
printf("Enter the position where you want to insert new node: ");
scanf("%d", &n);
printf("Enter data of %d node : ", n);
scanf("%d", &data);
insertAtN(data, n);
break;
case 5:
displayList();
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-5");
}
printf("\n\n");
}
return 0;
}