-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement Circular linked list..cpp
More file actions
159 lines (147 loc) · 3.89 KB
/
Copy pathimplement Circular linked list..cpp
File metadata and controls
159 lines (147 loc) · 3.89 KB
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
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
class CircularLinkedList {
private:
Node *head, *tail;
public:
CircularLinkedList() {
head = tail = NULL;
}
void insertAtBegin(int value) {
Node *temp = new Node;
temp->data = value;
temp->next = head;
if (head == NULL) {
head = tail = temp;
temp->next = head;
} else {
tail->next = temp;
head = temp;
}
}
void insertAtEnd(int value) {
Node *temp = new Node;
temp->data = value;
temp->next = head;
if (head == NULL) {
head = tail = temp;
} else {
tail->next = temp;
tail = temp;
}
}
void deleteFromBegin() {
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
if (head == tail) {
delete head;
head = tail = NULL;
return;
}
Node *temp = head;
head = head->next;
tail->next = head;
delete temp;
}
void deleteFromEnd() {
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
if (head == tail) {
delete head;
head = tail = NULL;
return;
}
Node *temp = head;
while (temp->next != tail) {
temp = temp->next;
}
delete tail;
tail = temp;
tail->next = head;
}
void searchElement(int value) {
Node *temp = head;
int index = 0;
while (temp->next != head) {
index++;
if (temp->data == value) {
cout << "Element found at index: " << index << endl;
return;
}
temp = temp->next;
}
if (temp->data == value) {
cout << "Element found at index: " << index + 1 << endl;
return;
}
cout << "Element not found." << endl;
}
void displayList() {
Node *temp = head;
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
while (temp->next != head) {
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data << endl;
}
};
int main() {
int choice, value;
CircularLinkedList cll;
cout << "1.Insert at Beginning" << endl;
cout << "2.Insert at the End" << endl;
cout << "3.Delete from Beginning" << endl;
cout << "4.Delete from last" << endl;
cout << "5.Search an element" << endl;
cout << "6.Display" << endl;
cout << "7.Exit" << endl;
do{
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value: ";
cin >> value;
cll.insertAtBegin(value);
break;
case 2:
cout << "Enter the value: ";
cin >> value;
cll.insertAtEnd(value);
break;
case 3:
cll.deleteFromBegin();
break;
case 4:
cll.deleteFromEnd();
break;
case 5:
cout << "Enter the value: ";
cin >> value;
cll.searchElement(value);
break;
case 6:
cll.displayList();
break;
case 7:
exit(0);
break;
default:
cout << "Invalid choice." << endl;
break;
}
}while(choice != 7);
return 0;
}