-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly linked list.cpp
225 lines (225 loc) · 4.85 KB
/
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* singly linked list
*
* Created on: 13-Jul-2018
* Author: indrajeet
*/
#include <iostream>
#include <string>
using namespace std;
template<class T>
struct node{
T data;
node *nextnodeptr;
};
template<class T>
class list{
private:
node<T> *headptr=nullptr;
public:
list(node<T> *headptr=nullptr):headptr(headptr){}
node<T> *createNode(T,node<T> *);
void prepend(T);
void append(T);
void addafter(T,T);
void addbefore(T,T);
T findPosition(T,bool);
void sortlist(bool);
void delbeg();
void delnode(T);
void reverse();
void print();
};
template<class T>
inline node<T>* list<T>::createNode(T data,node<T>* nextptr){
node<T> *newNode = new(node<T>);
newNode-> data=data;
newNode-> nextnodeptr=nextptr;
return newNode;
}
template<class T>
void list<T>::prepend(T data){
node<T> *newNode = createNode(data,headptr);
headptr=newNode;
}
template<class T>
void list<T>::append(T data){
node<T> *iter = headptr;
while(iter!=nullptr && iter->nextnodeptr!=nullptr)
iter = iter->nextnodeptr;
node<T> *newNode = createNode(data,nullptr);
if(iter == nullptr)
headptr = newNode;
else
iter->nextnodeptr=newNode;
}
template<class T>
void list<T>::addafter(T positionValue,T data){
node<T> *iter = headptr;
while(iter!=nullptr && iter->data!=positionValue)
iter = iter->nextnodeptr;
if(iter==nullptr)
cout << "Value not found" <<endl;
else{
node<T> *newNode = createNode(data,iter->nextnodeptr);
iter->nextnodeptr = newNode;
}
}
template<class T>
void list<T>::addbefore(T positionValue,T data){
node<T> *iter = headptr,*prev=nullptr;
while(iter!=nullptr && iter->data!=positionValue){
prev=iter;
iter = iter->nextnodeptr;
}
if(iter==nullptr)
cout << "Value not found" <<endl;
else if(prev==nullptr)
prepend(data);
else{
node<T> *newNode = createNode(data,iter);
prev->nextnodeptr = newNode;
}
}
template<class T>
T list<T>::findPosition(T data,bool ascending){
node<T> *iter=headptr;
while(iter!=nullptr)
if(ascending==true && data > iter->data)
iter = iter->nextnodeptr;
else if(ascending==false && data < iter->data)
iter = iter->nextnodeptr;
else
return iter->data;
}
template<class T>
void list<T>::sortlist(bool ascending){
node<T> *iter = headptr;
while(iter!=nullptr && iter->nextnodeptr!=nullptr){
if(ascending==true && iter->data < iter->nextnodeptr->data)
iter=iter->nextnodeptr;
else if(ascending==false && iter->data > iter->nextnodeptr->data)
iter=iter->nextnodeptr;
else{
T temp =iter->nextnodeptr->data;
delnode(temp);
addbefore(findPosition(temp,ascending),temp);
}
}
}
template<class T>
void list<T>::delbeg(){
if(headptr == nullptr){
cout << "list is empty" <<endl;
return ;
}
node<T> *temp=headptr;
headptr=headptr->nextnodeptr;
delete temp;
}
template<class T>
void list<T>::delnode(T data){
node<T> *iter = headptr,*prev=nullptr;
while(iter!=nullptr && data != iter->data){
prev = iter;
iter=iter->nextnodeptr;
}
if(iter == nullptr)
cout << "Value not found" <<endl;
else{
prev->nextnodeptr = iter->nextnodeptr;
delete iter;
}
}
template<class T>
void list<T>::reverse(){
node<T> *iter=headptr;
while(iter!=nullptr && iter->nextnodeptr!=nullptr){
T temp = iter->nextnodeptr->data;
delnode(temp);
prepend(temp);
}
}
template<class T>
void list<T>::print(){
node<T> *iter = headptr;
while(iter!=nullptr){
cout << iter->data <<" " ;
iter = iter->nextnodeptr;
}
}
int main(void) {
list<char> l;
string str ="Enter choice :\n"
"0:Exit\n"
"1:Add in beginning\n"
"2:Add in last\n"
"3:Add after\n"
"4:Add before\n"
"5:Reverse\n"
"6:Sort list\n"
"7:Delete first\n"
"8:Delete node\n"
"9:Print list";
int choice=0;
do{
char input;
int positionValue;
bool order;
cout << str << endl;
cin >> choice;
switch(choice){
case 0:
cout << "terminating the program..."<<endl;
cout << "program terminated."<<endl;
break;
case 1:
cout <<"insert value"<<endl;
cin >> input;
l.prepend(input);
break;
case 2:
cout <<"insert value"<<endl;
cin >> input;
l.append(input);
break;
case 3:
cout <<"insert value"<<endl;
cin >> input;
cout <<"Enter value after which you want to add new node" <<endl;
cin >> positionValue;
l.addafter(positionValue,input);
break;
case 4:
cout <<"insert value"<<endl;
cin >> input;
cout <<"Enter value before which you want to add new node" <<endl;
cin >> positionValue;
l.addbefore(positionValue,input);
break;
case 5:
l.reverse();
break;
case 6:
cout <<"Enter choice:\n1:Ascending\n2:descending"<<endl;
cin >> input;
order = (input==1 ? true : false);
l.sortlist(order);
break;
case 7:
l.delbeg();
break;
case 8:
cout << "Enter value to be deleted" <<endl;
cin >> input;
l.delnode(input);
break;
case 9:
l.print();
break;
default :
cout << "Wrong choice entered" << endl;
}
}while(choice!=0);
return 0;
}