-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularDoublyLinkedList.cpp
188 lines (178 loc) · 3.16 KB
/
CircularDoublyLinkedList.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
/*
Implement a Circular Doubly Linked list class having one private data member Node pointer
‘head’.
1. Implement a nested Node class in the linked list class, having three attributes data, next,
previous.
2. Now implement the following operations for the circular doubly linked list class:
2. Insert void insertAtHead(int value);
3. Print Forward void print() const;
4. Search a value bool Search(T value) const;
5. Erase a value void erase(T value);
6. Print backward void printback() const;
7. Destructor
8. Insert void insertafter(int key,int value);
9. Design a manu driven main function which will ask the user to perform any of the
above function. Ask for -1 to exit program.
*/
#include<iostream>
using namespace std;
class CircularDLL;
class Node
{
int data;
Node* next, * prev;
friend class CircularDLL;
};
class CircularDLL
{
Node* head;
public:
CircularDLL()
{
head = nullptr;
head = 0;
}
void print()
{
if (head == nullptr)
{
cout << "list is empty";
}
else
{
Node* curr = head;
while (curr != 0)
{
cout << curr->data << " ";
curr = curr->next;
}
}
}
void insertAtHead(int value)
{
Node* temp = new Node;
temp->next = head;
temp->data = value;
if (head != 0)
{
temp->prev = head->prev;
head->prev = temp;
}
else
{
temp->next = head;
temp->prev = head;
}
head = temp;
}
void printback()
{
if (head == nullptr)
{
cout << "\nlist is empty";
}
else
{
Node* curr = head;
curr = curr->next;
curr = curr->next;
while (curr != 0)
{
cout << curr->data << " ";
curr = curr->prev;
}
}
}
Node* Search(int value)
{
if (head == nullptr)
{
cout << "\nlist is empty";
return 0;
}
else
{
Node* curr = head;
while (curr != nullptr)
{
if (curr->data == value)
{
cout << "\nfound";
return curr;
}
else
{
cout << "\nnot found";
return 0;
}
curr = curr->next;
}
return 0;
}
}
bool deleteSpecificValue(int valToDelete)
{
if (head == 0)
{
cout << "\nthe list is empty";
return 0;
}
else
{
Node* curr = head;
Node* pre = nullptr;
if (curr->data == valToDelete)
{
head = curr->next;
if (head != nullptr)
{
head->prev = nullptr;
}
delete curr;
return true;
}
while (curr != nullptr)
{
if (curr->data == valToDelete)
{
pre->next = curr->next;
if (curr->next != nullptr)
{
curr->next->prev = pre;
}
delete curr;
return true;
}
pre = curr;
curr = curr->next;
}
return false;
}
}
~CircularDLL()
{
Node* curr = head;
while (curr != nullptr)
{
Node* nex = curr->next;
delete curr;
curr = nex;
}
cout << "\ndestructor called";
}
};
void main()
{
CircularDLL d;
d.insertAtHead(2);
d.insertAtHead(3);
d.insertAtHead(4);
d.print();
cout << "\nBack print: ";
d.printback();
Node* a = d.Search(5);
d.deleteSpecificValue(2);
cout << "\nAfter deleting : ";
d.print();
system("pause>nul");
}