-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment5.cpp
More file actions
197 lines (170 loc) · 4.75 KB
/
Copy pathassignment5.cpp
File metadata and controls
197 lines (170 loc) · 4.75 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
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
#include <iostream>
#include <string>
using namespace std;
class route {
public:
string data;
route* next;
route(string d) {
data = d;
next = NULL;
}
};
class navigation {
route* head = NULL;
public:
// Insert a route at the start
void insert_start(string d) {
route* nn = new route(d);
if (head == NULL) {
head = nn;
head->next = head;
} else {
route* temp = head;
while (temp->next != head) {
temp = temp->next;
}
nn->next = head;
temp->next = nn;
head = nn;
}
}
void insert_end(string d) {
route* nn = new route(d);
if (head == NULL) {
head = nn;
head->next = head;
} else {
route* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = nn;
nn->next = head;
}
}
void delete_start() {
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
if (head->next == head)
{
delete head;
head = NULL;
} else {
route* temp = head;
while (temp->next != head) {
temp = temp->next;
}
route* toDelete = head;
temp->next = head->next;
head = head->next;
delete toDelete;
}
}
void delete_end() {
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
if (head->next == head)
{
delete head;
head = NULL;
} else {
route* temp = head;
// Traverse to the second last node
while (temp->next->next != head) {
temp = temp->next;
}
route* toDelete = temp->next; // Last node to be deleted
temp->next = head; // Second last node points to head
delete toDelete; // Delete the last node
}
}
// Print the routes
void print() {
if (head == NULL) {
cout << "List is empty" << endl;
return;
}
route* temp = head;
cout << "Displaying linked list: ";
while (temp->next != head) {
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data << " "; // Print the last node
cout << endl;
}
// Search for a route
void search() {
if (head == NULL) {
cout << "List is empty" << endl;
return;
}
string title;
cout << "Enter the destination: ";
cin >> title;
route* temp = head;
while (temp->next != head) {
if (temp->data == title) {
cout << "Route found: " << temp->data << endl;
return;
}
temp = temp->next;
}
if (temp->data == title) { // Check last node
cout << "Route found: " << temp->data << endl;
} else {
cout << "Route not found." << endl;
}
}
};
int main() {
navigation n;
int choice = 0;
string data;
while (choice != 7) {
cout << "\nNavigation System" << endl;
cout << "1. Add Route at start" << endl;
cout << "2. Add Route at end" << endl;
cout << "3. Delete route at start" << endl;
cout << "4. Display Routes" << endl;
cout << "5. Delete route at end" << endl;
cout << "6. Search Route" << endl;
cout << "7. Exit" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "Enter destination to be added at start: ";
cin >> data;
n.insert_start(data);
break;
case 2:
cout << "Enter destination to be added at end: ";
cin >> data;
n.insert_end(data);
break;
case 3:
n.delete_start();
break;
case 4:
n.print();
break;
case 5:
n.delete_end();
break;
case 6:
n.search();
break;
case 7:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}