-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list_ADT.cpp
More file actions
152 lines (137 loc) · 4.28 KB
/
Copy pathsingly_linked_list_ADT.cpp
File metadata and controls
152 lines (137 loc) · 4.28 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
#include <iostream>
template <typename T>
struct Node {
T data;
Node* next;
Node(T value) : data(value), next(nullptr) {}
};
template <typename T>
class SinglyLinkedList {
private:
Node<T>* head;
public:
SinglyLinkedList() : head(nullptr) {}
void insertAtBeginning(T value) {
Node<T>* newNode = new Node<T>(value);
newNode->next = head;
head = newNode;
}
void insertAtPosition(int i, T value) {
if (i <= 0) { return; }
Node<T>* newNode = new Node<T>(value);
newNode->next = (i == 1) ? head : head->next;
if (i != 1) {
Node<T>* current = head;
for (int pos = 1; pos < i - 1 && current != nullptr; ++pos) {
current = current->next;
}
if (current != nullptr) { current->next = newNode; }
}
head = newNode;
}
void removeFromBeginning() {
if (head != nullptr) {
Node<T>* temp = head;
head = head->next;
delete temp;
}
}
void removeFromPosition(int i) {
if (i <= 0 || head == nullptr) { return; }
if (i == 1) {
Node<T>* temp = head;
head = head->next;
delete temp;
return;
}
Node<T>* current = head;
for (int pos = 1; pos < i - 1 && current != nullptr; ++pos) {
current = current->next;
}
if (current != nullptr && current->next != nullptr) {
Node<T>* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
Node<T>* searchElement(T value) {
Node<T>* current = head;
while (current != nullptr && current->data != value) {
current = current->next;
}
return current;
}
void display() {
Node<T>* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
int main() {
SinglyLinkedList<int> myList;
int choice;
do {
std::cout << "\n1. Insert at the beginning\n"
"2. Insert at a specific position\n"
"3. Remove from the beginning\n"
"4. Remove from a specific position\n"
"5. Search for an element\n"
"6. Display the list\n"
"0. Exit\n"
"Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1: {
int value;
std::cout << "Enter the value to insert: ";
std::cin >> value;
myList.insertAtBeginning(value);
break;
}
case 2: {
int position, value;
std::cout << "Enter the position to insert: ";
std::cin >> position;
std::cout << "Enter the value to insert: ";
std::cin >> value;
myList.insertAtPosition(position, value);
break;
}
case 3:
myList.removeFromBeginning();
break;
case 4: {
int position;
std::cout << "Enter the position to remove: ";
std::cin >> position;
myList.removeFromPosition(position);
break;
}
case 5: {
int value;
std::cout << "Enter the value to search: ";
std::cin >> value;
Node<int>* result = myList.searchElement(value);
if (result != nullptr) {
std::cout << "Element " << value << " found at address: " << result << std::endl;
} else {
std::cout << "Element " << value << " not found in the list." << std::endl;
}
break;
}
case 6:
std::cout << "Linked List: ";
myList.display();
break;
case 0:
std::cout << "Exiting program.\n";
break;
default:
std::cout << "Invalid choice. Try again.\n";
}
} while (choice != 0);
return 0;
}