-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.cpp
executable file
·207 lines (205 loc) · 5.11 KB
/
doubly_linked_list.cpp
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
#include <iostream>
using namespace std;
class Node
{
public:
int key;
int data;
Node *next;
Node *previous;
Node()
{
key = 0;
data = 0;
next = NULL;
previous = NULL;
}
Node(int newKey, int newData)
{
key = newKey;
data = newData;
next = NULL;
previous = NULL;
}
};
class DoublyLinkedList
{
public:
Node *head;
DoublyLinkedList()
{
head = NULL;
}
DoublyLinkedList(Node *node)
{
head = node;
}
// Checking if Node exists or not
Node *nodeExists(int newKey)
{
Node *temp = NULL;
Node *ptr = head;
while (ptr != NULL)
{
if (ptr->key == newKey)
{
temp = ptr;
}
ptr = ptr->next;
}
return temp;
}
// Adding Node at the end of the List
void append(Node *newNode)
{
if (nodeExists(newNode->key) != NULL)
{
cout << "Node Exists With Key " << newNode->key << endl;
}
else if (head == NULL)
{
head = newNode;
cout << "Node Appended as Head" << endl;
}
else
{
Node *ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newNode;
newNode->previous = ptr;
cout << "Node Appended" << endl;
}
}
// Adding node at the beginning of the list
void prepend(Node *newNode)
{
if (nodeExists(newNode->key) != NULL)
{
cout << "Node Exists With Key " << newNode->key << endl;
}
else if (head == NULL)
{
head = newNode;
cout << "Node Prepended as Head" << endl;
}
else
{
newNode->next = head;
head->previous = newNode;
head = newNode;
cout << "Prepend Successfully" << endl;
}
}
// Insert node after particular position
void insert(int newKey, Node *newNode)
{
Node *ptr = nodeExists(newKey);
if (ptr == NULL)
{
cout << "No Node Exists with this key " << newKey << endl;
}
else if (nodeExists(newNode->key) != NULL)
{
cout << "Node Already Exists With Key " << newNode->key << endl;
}
else
{
Node *nextNode = ptr->next;
if (nextNode == NULL)
{
nextNode = newNode;
newNode->previous = ptr;
cout << "Node Inserted at The End" << endl;
}
else
{
nextNode->previous = newNode;
newNode->next = nextNode;
ptr->next = newNode;
newNode->previous = ptr;
}
}
}
// Deleting Node By Key
void deletion(int newKey)
{
Node *ptr = nodeExists(newKey);
if (ptr == NULL)
{
cout << "No Node Exists with Key " << newKey << endl;
}
else
{
if (head->key == newKey)
{
head = head->next;
cout << "Node Unlinked With Key " << newKey << endl;
}
else
{
Node *prevNode = ptr->previous;
Node *nextNode = ptr->next;
// Deleting Node at The End
if (nextNode == NULL)
{
prevNode->next = NULL;
cout << "Node Deleted at The End with Key:-" << newKey << endl;
}
// Deleting in Between
else
{
prevNode->next = nextNode;
nextNode->previous = prevNode;
cout << "Node Deleted in Between with Key:-" << newKey << endl;
}
}
}
}
void updateNode(int newKey, int newData)
{
Node *ptr = nodeExists(newKey);
if (ptr != NULL)
{
ptr->data = newData;
cout << "Data Updted" << endl;
}
else
{
cout << "No Node Exists with Key " << newKey << endl;
}
}
void printList()
{
if (head == NULL)
{
cout << "No Node in The List" << endl;
}
else
{
Node *temp = head;
cout << "Key and Values in The List" << endl;
while (temp != NULL)
{
cout << "(" << temp->key << ", " << temp->data << ")" << endl;
temp = temp->next;
}
}
}
};
int main()
{
Node *n1 = new Node(1, 20);
Node *n2 = new Node(2, 40);
Node *n3 = new Node(3, 60);
DoublyLinkedList d;
d.append(n1);
d.append(n2);
d.append(n3);
d.deletion(2);
d.updateNode(3, 100);
d.printList();
return 0;
}