Repository navigation
Expand file tree
/
Copy path21.cpp
More file actions
218 lines (194 loc) · 5.71 KB
/
Copy path21.cpp
File metadata and controls
218 lines (194 loc) · 5.71 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Menu driven operations on LL
#include <iostream>
using namespace std;
class Linkedlist {
private:
struct Node {
int data;
Node *next;
} *start = nullptr;
public:
// --- INSERTION OPERATIONS ---
// Insert at beginning
void insert_beg(int item) {
Node* new_node = new Node;
new_node->data = item;
new_node->next = start;
start = new_node;
cout << "Inserted " << item << " at the beginning.\n";
}
// Insert at end
void insert_end(int item) {
Node* new_node = new Node;
new_node->data = item;
new_node->next = nullptr;
if (start == nullptr) {
start = new_node;
} else {
Node* temp = start;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new_node;
}
cout << "Inserted " << item << " at the end.\n";
}
// Insert at a specific position (1-based index)
void insert_pos(int item, int pos) {
if (pos < 1) {
cout << "Invalid position!\n";
return;
}
if (pos == 1) {
insert_beg(item);
return;
}
Node* temp = start;
// Traverse to the (pos - 1)-th node
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Position out of bounds!\n";
return;
}
Node* new_node = new Node;
new_node->data = item;
new_node->next = temp->next;
temp->next = new_node;
cout << "Inserted " << item << " at position " << pos << ".\n";
}
// --- DELETION OPERATIONS ---
// Delete from beginning
void delete_beg() {
if (start == nullptr) {
cout << "List is empty! Nothing to delete.\n";
return;
}
Node* temp = start;
start = start->next;
cout << "Deleted element: " << temp->data << " from the beginning.\n";
delete temp;
}
// Delete from end
void delete_end() {
if (start == nullptr) {
cout << "List is empty! Nothing to delete.\n";
return;
}
// If there's only one node
if (start->next == nullptr) {
cout << "Deleted element: " << start->data << " from the end.\n";
delete start;
start = nullptr;
return;
}
// Traverse to the second-to-last node
Node* temp = start;
while (temp->next->next != nullptr) {
temp = temp->next;
}
Node* last = temp->next;
cout << "Deleted element: " << last->data << " from the end.\n";
temp->next = nullptr;
delete last;
}
// Delete from a specific position (1-based index)
void delete_pos(int pos) {
if (pos < 1) {
cout << "Invalid position!\n";
return;
}
if (start == nullptr) {
cout << "List is empty! Nothing to delete.\n";
return;
}
if (pos == 1) {
delete_beg();
return;
}
Node* temp = start;
// Traverse to the (pos - 1)th node
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr || temp->next == nullptr) {
cout << "Position out of bounds!\n";
return;
}
Node* target = temp->next;
temp->next = target->next;
cout << "Deleted element: " << target->data << " from position " << pos << ".\n";
delete target;
}
// Display function
void display() {
Node* temp = start;
cout << "[";
while (temp != nullptr) {
cout << temp->data;
if (temp->next != nullptr) {
cout << ", ";
}
temp = temp->next;
}
cout << "]\n";
}
};
int main() {
Linkedlist list;
int choice, value, pos;
while (true) {
cout << "\n--- Linked List Operations ---\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Specific Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Specific Position\n";
cout << "7. Display List\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> value;
list.insert_beg(value);
break;
case 2:
cout << "Enter value: ";
cin >> value;
list.insert_end(value);
break;
case 3:
cout << "Enter value: ";
cin >> value;
cout << "Enter position (1-based): ";
cin >> pos;
list.insert_pos(value, pos);
break;
case 4:
list.delete_beg();
break;
case 5:
list.delete_end();
break;
case 6:
cout << "Enter position (1-based) to delete: ";
cin >> pos;
list.delete_pos(pos);
break;
case 7:
cout << "Current Linked List: ";
list.display();
break;
case 8:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}