-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_Singly_Linked_List.cpp
201 lines (165 loc) · 3.5 KB
/
01_Singly_Linked_List.cpp
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
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head;
void insertAtHead(int value){
Node* newNode = new Node(value);
newNode -> data = value;
newNode -> next = head;
head = newNode;
}
Node* traverse(int n){
Node* temp = head;
for(int i = 1 ; i < n && temp != nullptr; i++){
temp = temp->next;
}
return temp;
}
void insertInBetween(int value, int i){
Node* temp = traverse(i);
if(i<=0){
cout << "Invalid input" << endl;
}
else if(temp == nullptr){
cout <<"Position out of bound"<< endl;
}
else if(i == 1){
insertAtHead(value);
}
else {
Node* newNode = new Node();
newNode -> data = value;
newNode -> next = temp -> next;
temp -> next = newNode;
}
}
void deleteHead(){
Node* temp = head;
head = head->next;
delete temp;
}
void deleteInBetween(int i){
if(i == 1){
deleteHead();
}
else if(head == nullptr){
cout << "Linked List is empty" << endl;
}
else {
Node* temp = traverse(i);
if(temp == nullptr || i == 0){
cout << "Position " << i << " does not exist" << endl;
}
else{
traverse(i-1)->next = temp->next;
delete(temp);
}
}
}
void display(){
Node* temp = head;
while(temp != nullptr){
cout << temp->data;
if(temp->next !=nullptr){
cout << " -> ";
}
temp = temp->next;
}
cout << endl;
}
void defaultList(){
head = NULL;
insertAtHead(5);
insertAtHead(4);
insertAtHead(3);
insertAtHead(2);
insertAtHead(1);
cout<< "Default Singly linked list: " << endl;
display();
}
Node* search(int n){
Node* temp = head;
while( temp != nullptr){
if(temp->data == n){
cout << "Pointer Of '" << n << "' is " << temp <<endl;
return temp;
}
else{
temp = temp->next;
}
}
cout << "No Match Found" << endl;
return nullptr;
}
void reverse(){
Node* current , *prev, *next;
current = head;
prev = nullptr;
while (current != nullptr){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void choice(){
int n;
cout<< "Chose on of the Following Option" << endl;
cout<< "1. Insert at Head" << endl;
cout<< "2. Insert at ith position" <<endl;
cout<< "3. Delete at Head" << endl;
cout<< "4. Delete at ith position" <<endl;
cout<< "5. Search for an element x in the singly linked list and return its pointer" << endl;
cout<< "6. Reverse the Linked List" <<endl;
cin>> n;
if (n == 1){
int x;
cout << "Enter the element you want to insert at Head"<< endl;
cin >> x;
insertAtHead(x);
}
if (n == 2){
int x, i;
cout << "Enter the element you want to insert"<< endl;
cin >> x;
cout << "Enter the Position"<< endl;
cin >> i;
insertInBetween(x,i);
}
if (n == 3){
deleteHead();
cout << "Element at head deleted"<< endl;
}
if (n == 4){
int i;
cout << "Enter the Position you want to delete"<< endl;
cin >> i;
deleteInBetween(i);
}
if (n == 5){
int x;
cout << "Enter the element you want to search"<< endl;
cin >> x;
search(x);
}
if (n == 6){
reverse();
}
else if(n>6 || n<1){
cout << "Invalid Option" << endl;
}
}
int main(){
cout << "**********************************************************************************" << endl;
defaultList();
cout << "**********************************************************************************" << endl;
choice();
cout<< "Singly linked list: ";
display();
cout << endl;
return 0;
}