forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.cpp
More file actions
232 lines (217 loc) · 4.55 KB
/
SinglyLinkedList.cpp
File metadata and controls
232 lines (217 loc) · 4.55 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
#include<iostream>
using namespace std;
struct node{
int info;
struct node *next;
}*start;
class sll{
public:
node *create_node(int);
void insert_begin();
void insert_pos();
void insert_end();
void delete_pos();
void display();
sll()
{
start = NULL;
}
};
int main()
{
int choice, nodes, element, position, i;
sll sl;
start = NULL;
while(1)
{
cout<<endl<<"--------------------------------";
cout<<endl<<"Operations on Singly Linked list";
cout<<endl<<"--------------------------------";
cout<<endl<<"1-Insert Node at beginning";
cout<<endl<<"2-Insert Node at last";
cout<<endl<<"3-Insert Node at position";
cout<<endl<<"4-Delete particular Node";
cout<<endl<<"5-Display Linked List";
cout<<endl<<"6-Exit"<<endl;
cout<<endl<<"Enter Your Choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"Inserting Node at beginning:"<<endl;
sl.insert_begin();
break;
case 2: cout<<"Inserting Node at last:"<<endl;
sl.insert_end();
break;
case 3: cout<<"Inserting Node at Position:"<<endl;
sl.insert_pos();
break;
case 4: cout<<"Delete a particular Node:"<<endl;
sl.delete_pos();
break;
case 5: cout<<"Display elements:"<<endl;
sl.display();
break;
case 6: cout<<"Exiting....";
exit(1);
break;
default: cout<<"Wrong choice";
}
}
}
node *sll::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if(temp == NULL)
{
cout<<"Memory not allocated"<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
void sll::insert_begin()
{
int value;
cout<<"Enter the value to be inserted:";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if(start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
temp->next = p;
}
cout<<"Element inserted at beginning";
}
void sll::insert_end()
{
int value;
cout<<"Enter value to be inserted:";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while(s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element inserted at last";
}
void sll::insert_pos()
{
int value, pos, counter=0;
cout<<"Enter value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the position at which node to be inserted:";
cin>>pos;
int i;
s = start;
while(s != NULL)
{
s = s->next;
counter++;
}
if(pos == 1)
{
if(start = NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if(pos>1 && pos<=counter)
{
s = start;
for(i = 1;i<pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Position out of Range";
}
}
void sll::delete_pos()
{
int pos, i, counter = 0;
if(start==NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value you want to delete:";
cin>>pos;
struct node *s, *ptr;
s = start;
if(pos==1)
{
start=s->next;
}
else
{
while(s!=NULL)
{
s=s->next;
counter++;
}
if(pos>0 && pos<=counter)
{
s = start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element deleted"<<endl;
}
}
void sll::display()
{
struct node *temp;
if(start==NULL)
{
cout<<"List is empty"<<endl;
return;
}
temp=start;
cout<<"Elements of list are:"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}