-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTwoWayLinkedList.cpp
More file actions
228 lines (189 loc) · 6.88 KB
/
TwoWayLinkedList.cpp
File metadata and controls
228 lines (189 loc) · 6.88 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
219
220
221
222
223
224
225
226
227
228
/**
* Developer: Minhas Kamal (BSSE-0509, IIT, DU)
* Date: 13.Sep.2013
**/
#include<iostream>
#include<list>
using namespace std;
list<int> creatList(); //creates the list
void traversing(list<int> myList, int choice); //prints all the integers in the list
int searching(list<int> myList, int searchingElement); //searches for selected element
list<int> deleting(list<int> myList, int deletingElement, int selection); //deleting selected element
list<int> insertingAtBeginning(list<int> myList, int insertingElement); //inserting at beginning
list<int> insertingAtEnd(list<int> myList, int insertingElement); //inserting at end
list<int> inserting(list<int> myList, int destinationElement, int insertingElement, int a); //inserts certain element in definite place
list<int> destroy(list<int> myList); //destroying the list
int main()
{
list<int> myList;
myList = creatList();
while(true)
{
int selection; //user action
cout << "\n##What do you want to do? \npress- 1 for traversing\n"; //user prompt
cout << " 2 for searching\n 3 for deleting\n 4 for inserting\n";
cout << " 5 for exit\nSelection: ";
cin >> selection;
if(selection==1){
int choice;
cout << "How do you want to traverse?\npress- 1 for traversing first to last";
cout << "\n 2 for traversing last to first\nSelection: ";
cin >> choice;
traversing(myList, choice);
}
else if(selection==2)
{
int searchingElement; //the element to be searched
cout << "Enter the element you want to search for: ";
cin >> searchingElement;
cout << "\n";
cout << "* " << searchingElement << " is found " << searching(myList, searchingElement) << " times.\n";
}
else if(selection==3)
{
int deletingElement; //the element to be deleted
cout << "Enter the element: ";
cin >> deletingElement;
int sel=0; //determines the correct element
cout << "Press 1 for deleting " << deletingElement;
cout << "\n 2 for deleting the element after " << deletingElement;
cout << "\n 3 for deleting the element before " << deletingElement << "\nSelection- ";
cin >> sel;
myList = deleting(myList, deletingElement, sel);
}
else if(selection==4)
{
int insertingElement, destinationElement, sel; //the element to be deleted
cout << "Enter the element: ";
cin >> insertingElement;
cout << "Where do you want to insert? \nPress 1 for beginning";
cout << "\n 2 for end\n 3 for chosing destination\nSelection: ";
cin >> sel;
if(sel==1) myList = insertingAtBeginning(myList, insertingElement);
else if(sel==2) myList = insertingAtEnd(myList, insertingElement);
else{
cout << "Enter the destination element: ";
cin >> destinationElement;
int a=0; //determines the correct element
cout << "Press 1 for inserting after " << destinationElement;
cout << "\n 2 for inserting before " << destinationElement << "\nSelection- ";
cin >> a;
myList = inserting(myList, destinationElement, insertingElement, a);
}
}
else if(selection==5) break;
else cout << "#Invalid input!\n";
}
myList = destroy(myList);
}
list<int> creatList() //creates the list
{
list<int> myList; //declaring the list
int intNumber, //the number of integers
input; //the input that the user gives
cin >> intNumber;
for(int i=0; i<intNumber; i++)
{
cin >> input;
myList.push_back(input);
}
return myList;
}
void traversing(list<int> myList, int choice) //prints the List
{
list<int>:: iterator it; //declaring a iterator
cout << endl;
if(choice==1)
for(it=myList.begin(); it!=myList.end(); it++)
cout << *it << " ";
else
for(it=myList.end(); it!=myList.begin(); ){
it--;
cout << *it << " ";
}
cout << endl << endl ;
}
int searching(list<int> myList, int searchingElement)
{
int found=0; //number of the searching element
list<int>:: iterator it, //declaring iterator
old=myList.begin(); //this will store old position
for(it=myList.begin(); it!=myList.end(); it++){
if(searchingElement==*it){
it++;
if(old!=myList.begin() && it!=myList.end()){
cout << "*between " << *old << " and " << *it << endl;
}
else if(old==myList.begin()) cout << "*at beginning\n";
else cout << "*at end\n";
it--;
found++;
}
old=it;
}
return found;
}
list<int> deleting(list<int> myList, int deletingElement, int choice)
{
int flag=1;
if(choice==1) myList.remove(deletingElement);
else if(choice==2) {
list<int>:: iterator it; //declaring iterator
flag=0;
for(it=myList.begin(); it!=myList.end(); it++){
if(deletingElement==*it){
it++;
it=myList.erase(it);
flag=1;
if(it==myList.end()) break;
}
}
}
else {
list<int>:: iterator it; //declaring iterator
flag=0;
for(it=myList.begin(); it!=myList.end(); it++){
if(deletingElement==*it){
it--;
it=myList.erase(it);
flag=1;
if(it==myList.end()) break;
}
}
}
if(flag) cout << "\n* Deleted!\n";
return myList;
}
list<int> insertingAtBeginning(list<int> myList, int insertingElement)
{
myList.insert(myList.begin(), insertingElement);
cout << "\n* Inserted!!\n";
return myList;
}
list<int> insertingAtEnd(list<int> myList, int insertingElement)
{
myList.insert(myList.end(), insertingElement);
cout << "\n* Inserted!!\n";
return myList;
}
list<int> inserting(list<int> myList, int destinationElement, int insertingElement, int a)
{
int flag=0;
list<int>:: iterator it; //declaring iterator
for(it=myList.begin(); it!=myList.end(); it++){
if(destinationElement==*it) {
flag=1;
break;
}
}
if(a==1) it++;
myList.insert(it, insertingElement);
if(flag) cout << "\n* Inserted!!\n";
else cout << "Destination not found.\n";
return myList;
}
list<int> destroy(list<int> myList)
{
myList.clear();
return myList;
}