-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorted_LinkedList.cpp
More file actions
253 lines (232 loc) · 4.58 KB
/
Copy pathSorted_LinkedList.cpp
File metadata and controls
253 lines (232 loc) · 4.58 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Name : Annie Bhalla
Roll No : 19HCS4009
Course : Bsc (H) Computer Science
Semester : 3
Title : Sorted Linked List
*/
#include<iostream>
using namespace std;
class node
{
private :
int data;
node *next;
public:
node()
{
data=0;
next=NULL;
}
node(const int x, node *n=NULL)
{
data=x;
next=n;
}
friend class LinkedList;
};
class LinkedList
{
node *head,*tail;
protected:
void display(node *p);
public:
LinkedList(): head(NULL), tail(NULL){ } // (a) constructor
~LinkedList(); // (b) destructor
void insert(const int x); // (c)
void insert_front(const int x); // (c.1)
void insert_end(const int x); // (c.2)
void insert_middle(const int x, node *first, node* second); // (c.3)
void remove(const int x); // (d)
void remove_front(const int x); // (d.1)
void remove_end(const int x); // (d.2)
void remove_middle(node *f); // (d.3)
void display() // (e) to diplay contents
{
if(head==NULL)
throw " Function display() : Empty List ";
display(head);
}
};
// (b) Destructor
LinkedList::~LinkedList()
{
node *p=head;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
tail=NULL;
}
// (c) to insert x in Linked List in sorted order
void LinkedList::insert(const int x)
{
if(head==NULL)
{
insert_front(x);
return;
}
if( x < head->data)
{
insert_front(x);
return;
}
node *ptr= head;
node *ctr= head->next;
while(ctr!=NULL)
{
if(x >ptr->data && x< ctr->data)
{
insert_middle(x,ptr,ctr);
return;
}
ptr=ctr;
ctr=ctr->next;
}
if(ctr==NULL && x > ptr->data)
{
insert_end(x);
return;
}
else
throw " Function insert() : Insertion of Duplicate elements ";
}
// (c.1) to insert at front
void LinkedList::insert_front(const int x)
{
node *n=new node(x);
n->next=head;
head=n;
if(tail==NULL)
tail=n;
}
// (c.2)to insert at end
void LinkedList::insert_end(const int x)
{
node *ptr,*arrow;
ptr= new node;
ptr->data=x;
arrow=head;
while(arrow->next!=NULL)
{
arrow=arrow->next;
}
arrow->next=ptr;
tail=ptr;
}
// (c.3) to insert in between two nodes
void LinkedList::insert_middle(const int x , node *first, node *second)
{
node *newnode= new node(x);
newnode->next= second;
first->next=newnode;
}
// (d) to remove x from linked List
void LinkedList::remove(const int x)
{
node *ptr=head;
if(ptr==NULL)
throw " Function Delete() : Deletion from an Empty Linked List ";
if( x == head->data)
{
remove_front(x);
return;
}
while(ptr!=NULL && ptr->data !=x)
{
ptr=ptr->next;
}
if(ptr==NULL)
throw " Function Delete() : Deletion of Non Existing Element ";
if(ptr->next==NULL)
{
remove_end(x);
return;
}
else
{
remove_middle(ptr);
return;
}
}
// (d.1) to remove element from front
void LinkedList::remove_front(const int x)
{
node *old=head;
head=old->next;
delete old;
if(head==NULL)
tail==NULL;
}
// (d.2) to remove element from end
void LinkedList::remove_end(const int x)
{
node *ptr,*arrow;
ptr=head;
arrow=head->next;
while(arrow->next!=NULL)
{
ptr=ptr->next;
arrow=arrow->next;
}
ptr->next=NULL;
tail=ptr;
delete arrow;
}
// (d.3) to remove element from middle
void LinkedList::remove_middle(node *f)
{
node *ptr= head;
while(ptr->next!=f)
ptr=ptr->next;
ptr->next=f->next;
delete f;
}
// (e) to display the linked list recursively
void LinkedList::display(node* ptr)
{
if(ptr==NULL)
return;
cout<<ptr->data<<" ";
display(ptr->next);
}
// driver fucntion
int main()
{
try
{
LinkedList L;
int n;
int element;
cout<<"\n How many numbers do you wish to store in Linked List ";
cin>>n;
cout<<"\n Enter the elements : "<<endl;
for(int i=0;i<n;i++)
{
cin>>element;
L.insert(element);
}
cout<<endl;
cout<<"\n <---------- Contents of Linked List -------------> "<<endl;
L.display();
cout<<endl;
char ch;
do
{
cout<<"\n Enter the element you want to delete : ";
cin>>element;
L.remove(element);
cout<<"\n Element Removed Successfully : "<<endl;
L.display();
cout<<endl;
cout<<"\n Do you wish to continue ? (y/n) : ";
cin>>ch;
}while(ch=='Y' || ch=='y');
}
catch(const char *str)
{
cout<<"\n Exception from : "<<str;
}
return 0;
}