assignment_06.cpp
Problem Statement
assignment_06.cpp
Implement Circular Linked List. Include functions for insertion, deletion and search of a number, reverse the list.
Source Code
cpp
#include <iostream>
using namespace std;
struct node {
int data;
node *next = nullptr;
};
class CircularLinkedList {
private:
node *head = nullptr;
public:
void insertAtBegin(int val) {
node *newNode = new node{val};
if (head == nullptr) { head = newNode; newNode->next = head; return; }
node *temp = head;
while (temp->next != head) temp = temp->next;
newNode->next = head;
temp->next = newNode;
head = newNode;
}
void insertAtEnd(int val) {
node *newNode = new node{val};
if (head == nullptr) { head = newNode; newNode->next = head; return; }
node *temp = head;
while (temp->next != head) temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
void insertAtPos(int pos, int val) {
if (pos < 1) { cout << "Invalid Position!\n"; return; }
if (pos == 1) { insertAtBegin(val); return; }
if (head == nullptr) { cout << "Out of range!\n"; return; }
node *temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
if (temp == head) { cout << "Out of range!\n"; return; }
}
node *newNode = new node{val};
newNode->next = temp->next;
temp->next = newNode;
}
void deleteAtBegin() {
if (head == nullptr) { cout << "List is empty!\n"; return; }
if (head->next == head) { delete head; head = nullptr; return; }
node *temp = head, *last = head;
while (last->next != head) last = last->next;
head = head->next;
last->next = head;
delete temp;
}
void deleteAtEnd() {
if (head == nullptr) { cout << "List is empty!\n"; return; }
if (head->next == head) { delete head; head = nullptr; return; }
node *temp = head;
while (temp->next->next != head) temp = temp->next;
node *delNode = temp->next;
temp->next = head;
delete delNode;
}
void deleteAtPos(int pos) {
if (head == nullptr || pos < 1) { cout << "Unable to process!\n"; return; }
if (pos == 1) { deleteAtBegin(); return; }
node *temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
if (temp == head) { cout << "Out of range!\n"; return; }
}
if (temp->next == head) { cout << "Out of range!\n"; return; }
node *delNode = temp->next;
temp->next = delNode->next;
delete delNode;
}
int search(int key) {
if (head == nullptr) return -1;
node *temp = head;
int pos = 1;
do {
if (temp->data == key) return pos;
temp = temp->next;
pos++;
} while (temp != head);
return -1;
}
void reverse() {
if (head == nullptr || head->next == head) return;
node *prev = nullptr, *curr = head, *nxt = nullptr, *oldHead = head;
do {
nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
} while (curr != head);
oldHead->next = prev;
head = prev;
}
void display() {
if (head == nullptr) { cout << "List is empty!\n"; return; }
node *temp = head;
cout << "List: ";
do { cout << temp->data << " -> "; temp = temp->next; } while (temp != head);
cout << "(head)\n";
}
void printMenu() {
cout << "1.insertAtBegin 2.insertAtEnd 3.insertAtPos 4.deleteAtBegin 5.deleteAtEnd \n"
<< "6.deleteAtPos 7.search 8.reverse 0.Display -1.Exit\n";
}
~CircularLinkedList() {
if (head == nullptr) return;
node *temp = head->next;
while (temp != head) { node *next = temp->next; delete temp; temp = next; }
delete head;
}
};
int main() {
CircularLinkedList list;
int choice, val, pos;
while (true) {
list.printMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter value: "; cin >> val; list.insertAtBegin(val); break;
case 2: cout << "Enter value: "; cin >> val; list.insertAtEnd(val); break;
case 3:
cout << "Enter position: "; cin >> pos;
cout << "Enter value: "; cin >> val;
list.insertAtPos(pos, val);
break;
case 4: list.deleteAtBegin(); break;
case 5: list.deleteAtEnd(); break;
case 6: cout << "Enter position: "; cin >> pos; list.deleteAtPos(pos); break;
case 7: {
cout << "Enter value to search: "; cin >> val;
int res = list.search(val);
if (res != -1) cout << "Found at position " << res << ".\n";
else cout << "Not found.\n";
break;
}
case 8: list.reverse(); break;
case 0: list.display(); break;
case -1: cout << "Exiting...\n"; return 0;
default: cout << "Wrong choice. Try again.\n";
}
}
}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
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