-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsingly_linkedlist.cpp
230 lines (227 loc) · 5.28 KB
/
singly_linkedlist.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
226
227
228
229
230
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class singlelist
{
private:
node *tail,*head;
public:
singlelist()
{ head=NULL;
tail=NULL;
}
void addtotail(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
} else
{
tail->next = tmp;
tail = tail->next;
}
cout<<"succesfully added to tail";
}
void addtohead(int val)
{ node *new_node = new node;
new_node->data = val;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
} else
{
new_node->next = head;
head= new_node;
}
cout<<"succesfully added to head";
}
void randominsert(int itemn)
{
node *ptr = new node;
struct node *temp;
int loc;
cout<<"Enter the location";
cin>>loc;
ptr->data = itemn;
temp=head;
for(int i=0; i<loc; i++)
{ temp = temp->next;
if(temp == NULL)
{
cout<<"\ncan't insert\n";
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
cout<<"\nNode inserted at desired position";
}
void begdelete()
{
node *ptr;
if(head == NULL)
{
cout<<"\nList is empty";
}
else
{
ptr = head;
head = ptr->next;
delete ptr;
cout<<"\n Node deleted from the begining ...";
}
}
void end_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
cout<<"\nlist is empty";
}
else if(head -> next == NULL)
{
head = NULL;
delete head;
cout<<"\nOnly node of the list deleted ...";
}
else
{ ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
delete ptr;
cout<<"\n Deleted Node from the last ...";
}
}
void search(int key)
{
struct node *search;
search=head;
while (search->next!= NULL)
{
if (search->data == key)
{
cout<<"element is found in list\n";
return;
}
search = search->next;
}
cout<<"item not found in list\n";
}
void Delete(int anypos)
{ node* temp1 = head;
if(anypos == 1) {
head = temp1->next;
delete temp1;
cout<<"succesfully deleted from desired positon in list\n"<<endl;
}
else
{
for(int i=0; i<anypos-2; i++)
{
temp1 = temp1->next;
}
node* temp2 = temp1->next;
temp1->next = temp2->next;
delete temp2;
cout<<"succesfully deleted from desired positon in list\n"<<endl;
}
}
void reverse() {
node *curNode = head,*nxtNode;
head = NULL;
while (curNode != NULL) {
nxtNode = curNode->next;
curNode->next = head;
head = curNode;
curNode = nxtNode;
}
}
void display()
{
node *tmp;
tmp = head;
while (tmp != NULL)
{
cout << tmp->data <<"->"<<" ";
tmp = tmp->next;
}
}
};
int main()
{ singlelist l;
int n;
while(1)
{ cout<<endl;
cout<<"enter your choice"<<endl;
cout<<"1. add to tail"<<endl;
cout<<"2. add to head"<<endl;
cout<<"3. add to desired position"<<endl;
cout<<"4. delete from front/begin"<<endl;
cout<<"5. delete from end/last "<<endl;
cout<<"6. search in list "<<endl;
cout<<"7. delete from desired position "<<endl;
cout<<"8. display"<<endl;
cout<<"9. reverse"<<endl;
cin>>n;
switch(n)
{
case 1:
int item;
cout<<"enter item to tail"<<endl;
cin>>item;
l.addtotail(item);
break;
case 2:
int item1;
cout<<"enter item to tail"<<endl;
cin>>item1;
l.addtohead(item1);
break;
case 3:
int iteminsert;
cout<<"enter item to desired position"<<endl;
cin>>iteminsert;
l.randominsert(iteminsert);
break;
case 4:
l.begdelete();
break;
case 5:
l.end_delete();
break;
case 6:
int value;
cout<<"enter item you want to search"<<endl;
cin>>value;
l.search(value);
break;
case 7:
int delpos;
cout<<"enter position you want to delete"<<endl;
cin>>delpos;
l.Delete(delpos);
break;
case 8:
l.display();
break;
case 9:
l.reverse();
break;
default:
cout<<"nothing"<<endl;
}
}
}