forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.c
152 lines (144 loc) · 3.41 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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
struct Node *create_ll(int data)
{
struct Node *head = malloc(sizeof(struct Node));
head->data = data;
head->next = NULL;
head->prev = NULL;
}
void insertNode_beg(struct Node *head, int data)
{
struct Node *n = malloc(sizeof(struct Node));
n->data = head->data;
n->next = head->next;
n->prev = head;
head->data = data;
head->next = n;
}
void insertNode_bet(struct Node *head, int e, int data)
{
struct Node *ptr = head, *n = malloc(sizeof(struct Node));
if (ptr != NULL && ptr->data == e)
{
insertNode_beg(head, data);
return;
}
while (ptr != NULL && ptr->data != e)
{
ptr = ptr->next;
}
if (ptr == NULL)
{
printf("Element not found");
return;
}
n->data = data;
n->next = ptr;
n->prev = ptr->prev;
(ptr->prev)->next = n;
ptr->prev = n;
}
void insertNode_end(struct Node *head, int data)
{
struct Node *n = malloc(sizeof(struct Node));
struct Node *ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = n;
n->prev = ptr;
n->data = data;
n->next = NULL;
}
void deleteNode(struct Node *head, int e)
{
struct Node *ptr = head;
if (ptr != NULL && ptr->data == e)
{
ptr = ptr->next;
head->next = ptr->next;
head->data = ptr->data;
free(ptr);
printf("Element deleted.\n");
return;
}
while (ptr != NULL && ptr->data != e)
{
ptr = ptr->next;
}
if (ptr == NULL)
{
printf("Element not found");
return;
}
(ptr->prev)->next = ptr->next;
(ptr->next)->prev = ptr->prev;
free(ptr);
printf("Element deleted.\n");
}
void printList(struct Node *head)
{
struct Node *ptr = head;
printf("Elements entered are: \n");
while (ptr != NULL)
{
printf("%d\t", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
int main()
{
int n = 0, i = 0;
printf("Enter element to create list: ");
scanf("%d", &n);
struct Node *head = create_ll(n);
printf("Press 1 to enter element at the beginning.\nPress 2 to enter element before given element in the list.\nPress 3 to enter element at the end.\nPress 4 to delete element.\nPress 5 to print List\n");
do
{
int j = 0;
scanf("%d", &j);
switch (j)
{
case 1:
printf("Enter element to be inserted at the beginning: ");
scanf("%d", &n);
insertNode_beg(head, n);
break;
case 2:
printf("Enter element in the list: ");
int e = 0;
scanf("%d", &e);
printf("Enter element to be inserted: ");
scanf("%d", &n);
insertNode_bet(head, e, n);
break;
case 3:
printf("Enter element to be inserted at the end: ");
scanf("%d", &n);
insertNode_end(head, n);
break;
case 4:
printf("Enter element to be deleted: ");
scanf("%d", &n);
deleteNode(head, n);
break;
case 5:
printList(head);
break;
default:
printf("Invalid input\n");
}
printf("Press 1 to continue\n");
scanf("%d", &i);
} while (i == 1);
return 0;
}