-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.c
215 lines (210 loc) · 4.99 KB
/
DoublyLinkedList.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// main.c
// DoublyLinkedList
//
// Created by Vicky_Jha on 30/11/19.
// Copyright © 2019 test. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
struct DLList
{
int data;
struct DLList *next , *prev;
}*head = NULL;
void beforeHead(int );
void afterTail(int );
void insertInDLinkedList(int , int );
void deleteNode(int );
void deleteDLinkedList(void);
int count(void);
void display(void);
int main(int argc, const char * argv[]) {
int data , ch , position;
while(1)
{
printf("Enter your choice\n1.insert node in the beginning\n2.insert node at the end\n3.insert node by position\n4.display\n5.Delete a node by Position\n6.Delete Full list\n7.Exit\n");
scanf("%d",&ch);
switch (ch) {
case 1:
printf("Enter node data\n");
scanf("%d",&data);
beforeHead(data);
break;
case 2:
printf("Enter node data\n");
scanf("%d",&data);
afterTail(data);
break;
case 3:
printf("Enter position of the new node\n");
scanf("%d",&position);
printf("Enter node data\n");
scanf("%d",&data);
insertInDLinkedList(data, position);
break;
case 4:
display();
break;
case 5:
printf("ENter the position of the node to delete\n");
scanf("%d",&position);
deleteNode(position);
break;
case 6:
deleteDLinkedList();
break;
case 7:exit(1);
break;
default:printf("Invalid choice\n");
break;
}
}
return 0;
}
int count()
{
int count=0;
struct DLList *temp = head;
while(temp){
count++;
temp = temp->next;
}
return count;
}
void beforeHead(int data)
{
struct DLList *newNode = (struct DLList*)malloc(sizeof(struct DLList));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if(!newNode){
printf("Memory error\n");
return;
}
if(head == NULL)
head = newNode;
else{
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
void afterTail(int data)
{
struct DLList *newNode = (struct DLList*)malloc(sizeof(struct DLList)),*temp = head;
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if(!newNode){
printf("Memory error\n");
return;
}
if(head == NULL)
head = newNode;
else{
while(temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
}
void insertInDLinkedList(int data , int position)
{
struct DLList *p, *newNode , *c;
newNode = (struct DLList*)malloc(sizeof(struct DLList));
if(!newNode){
printf("Memory error\n");
return;
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
p = head;
if(position == 1){
newNode->next = p;
p->prev = newNode;
head = newNode;
}
else{
if(position > count())
{
printf("Entered position of newNode is Invalid\nCurrent size of List = %d\n",count());
return;
}
else
{
position--;
while(position--){ //number of loops = position
c = p;
p = p->next;
}
newNode->next = p;
p->prev = newNode;
c->next = newNode;
newNode->prev = c;
}
}
}
void display()
{
struct DLList *temp = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
else{
while (temp) {
printf("%d ",temp->data);
temp = temp->next;
}
}
printf("\n");
}
void deleteNode(int position)
{
struct DLList *p = head,*c,*q;
if(head == NULL)
{
printf("List is Empty\n");
return;
}
else{
if(position == 1){
head = p->next;
free(p);
return;
}
else{
if(position > count())
{
printf("Entered position of newNode is Invalid\nCurrent size of List = %d\n",count());
return;
}
else
{
position--;
while(position--){ //number of loops = position
c = p;
p = p->next;
}
q = p->next;
c->next = q;
q->prev = p->prev;
free(p);
// free(c);
}
}
}
}
void deleteDLinkedList()
{
struct DLList *iterator = head , *temp;
while (iterator) {
temp = iterator->next;
free(iterator);
iterator = temp;
}
head = NULL;
}