-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
258 lines (224 loc) · 4.37 KB
/
Copy pathLinkedList.cpp
File metadata and controls
258 lines (224 loc) · 4.37 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
254
255
256
257
258
// Linked List Implementation
class StringNode
{
private:
string elem;
StringNode *next;
public:
friend class LinkedList;
};
class LinkedList
{
StringNode *head,*tail;
public:
LinkedList(): head(NULL) : tail(NULL){};
~LinkedList();
bool empty() const; // done
const string& front() const; // done
void insert_front(const string& str); // done
void remove_front(); // done
void insert_end(const string& str); // done
void remove_end(); // done
void insert_middle(int position, const string& str); // done
void display(); // done
void remove_middle(int position); // done
void search(const string& str); // dpne
int count(); // done
void concatenation(const LinkedList& obj); // done
};
// function to determine whether the list in empty or not
bool LinkedList::empty()
{
if(head->next==NULL)
return 0;
else
return 1;
}
//function that returns the element at the head of list
string& LinkedList::front()
{
if(head==NULL) throw" Empty List";
return head->elem;
}
// function to insert an element in the beginning of the list
void LinkedList::insert_front(const string& str)
{
StringNode *p= new StringNode;
p->elem=str;
p->next=head; // head points to p
if(empty())
tail=p;
head=p; // p follows head
}
// function to remove the head element or beginning element in the list
void LinkedList::remove_front()
{
StringNode *old=head;
head=old->next;
delete old;
}
// function to insert an elemet at the end of the list
void LinkedList::insert_end(const string& str)
{
StringNode *ptr,*arrow;
ptr= new StringNode;
ptr->elem=str;
arrow=head;
while(arrow!=NULL)
{
arrow=arrow->next;
}
arrow=p;
tail=p;
}
// function to remove an elemenet from the end of list
void LinkedList::remove_end()
{
if(empty())
{
cout<<"\n Empty list";
return;
}
if(count()==1)
{
StringNode *i=head;
head= NULL;
tail= NULL;
delete i;
return;
}
StringNode *ptr,*arrow;
ptr=head;
arrow=head->next;
while(arrow->next!=NULL)
{
ptr=ptr->next;
arrow=arrow->next;
}
ptr->next=NULL:
tail=ptr;
delete arrow;
}
// function to display all the elements in the linked list
void LinkedList::display()
{
if(head==NULL) cout<<"\n Empty List";
else
{
StringNode *pt= head;
while(pt!=NULL)
{
cout<<pt->elem<<" ";
pt=pt->next;
}
}
}
// functiont to insert an element at a given position in the list
void LinkedList::insert_middle(int position,const String& str)
{
if(position>count())
{
StringNode *x;
x->elem=str;
x->next=NULL;
tail->next=x;
x=tail;
return;
}
if(position<1)
{
cout<<"\n Invalid position";
return;
}
if(position==1)
{
insert_front(str);
return;
}
int count=1;
StringNode *ptr,*arrow;
arrow=head;
while(arrow!=NULL)
{
if(count==position-1)
{
ptr=new StringNode;
ptr->elem=str;
ptr->next=arrow->next;
arrow->next=ptr;
}
else
{
arrow=arrow->next;
count++;
}
}
}
// function to remove an element from given position in the list
void LinkedList::remove_middle(int position)
{
if(position>count())
{
cout<<"\n Position "<<position<<" does not exist for the linked list ";
return;
}
if(position==1)
{
StringNode *ptr=head;
head=ptr->next;
delete ptr;
return;
}
int count=1;
StringNode *ptr,*arrow;
arrow=head;
while(arrow->next!=NULL)
{
if(count==(position-1))
{
ptr=arrow->next;
arrow->next=ptr->next;
delete ptr;
}
else
{
arrow=arrow->next;
count++;
}
}
}
// function that returns the number of nodes in a list
int count()
{
int ctr=1;
StringNode *ptr=head;
while(ptr->!=NULL)
{
ptr=ptr->next;
ctr++;
}
return ctr;
}
// fuction to search the element in the list
StringNode* search(const string& x)
{
StringNode *ptr=head;
while(ptr!=NULL)
{
if(ptr->elem==x)
return ptr;
}
return ptr;
}
// function to concatenate two singly linked lists
void concatenation(const LinkedList& obj)
{
StringNode *ptr,*ptr2;
ptr= tail;
ptr2=obj.head;
ptr->next= ptr2;
}
// Driver Code
int main()
{
}