-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopll.cpp
More file actions
223 lines (178 loc) · 3.6 KB
/
opll.cpp
File metadata and controls
223 lines (178 loc) · 3.6 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
//Libraries
#include<bits/stdc++.h>
#include<cstdio>
//Define
#define nl "\n"
//Namespaces
using namespace std;
//global variables
struct node
{
int data;
node * next;
};
struct node * start = NULL;
//Function declarations
node * create_ll(struct node*);
void display_ll(struct node*);
node * insert_beg(struct node*);
node * insert_end(struct node*);
void delete_ll(struct node *);
node * delete_node(struct node*);
node * insert_after(struct node*);
node * insert_before(struct node*);
//Main function
signed main()
{
start = create_ll(start);
display_ll(start);
start = insert_beg(start);
display_ll(start);
start = insert_end(start);
cout<<"\n";
display_ll(start);
start = insert_after(start);
cout<<"\n";
display_ll(start);
start= insert_before(start);
cout<<"\n";
display_ll(start);
start= delete_node(start);
cout<<endl;
display_ll(start);
delete_ll(start);
if (start==NULL)
cout<<"no error";
return 0;
}
//Function Definition
node * create_ll(node * )
{
node * ptr;
node * new_node;
cout<<"Enter the data: \n";
while(true)
{
new_node = new node;
if (new_node==NULL)
cout<<"Memory allocation failed nl"<<endl;
else
cin>>new_node->data;
new_node->next=NULL;
if (new_node->data==-404)
break;
else
if (start==NULL)
{ //cin>>new_node-> data;
//new_node->next= NULL;
start=new_node;}
else
{ ptr =start;
while(ptr->next!=NULL)
ptr= ptr->next;
ptr->next=new_node;
}
}
return start;
}
void display_ll(node *)
{
node * ptr;
ptr= start;
while(ptr!=NULL)
{ cout<<ptr->data<<endl;
// if(ptr->data==100)
//break;
ptr=ptr->next;
}
}
node * insert_beg(node *)
{
node * ptr, *new_node;
cout<<"Enter the data in the new node to be inserted\n";
new_node= new node;
if (new_node == NULL)
cout<<"Memory allocation failed"<<endl;
else
{
cin>>new_node->data;
ptr=start;
new_node->next=ptr;
start=new_node;
}
return start;
}
node * insert_end(node *)
{
node * ptr,* new_node;
cout<<"Enter the data in the new node to be inserted at the end\n";
new_node = new node;
if (new_node == NULL)
cout<<"Memory allocation failed"<<endl;
else
{
cin>>new_node->data;
new_node->next = NULL;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next= new_node;
}
return start;
}
node * insert_after(node *)
{
cout<<endl<<"enter the value after which node is to be inserted"<<endl;
int value;
cin>>value;
node * ptr, *new_node;
ptr = start;
while(ptr->data!=value)
ptr=ptr->next;
new_node=new node;
cout<<"Enter the data in the node"<<endl;
cin>>new_node->data;
new_node->next=ptr->next;
ptr->next=new_node;
return start;
}
void delete_ll(node *)
{
node * ptr,*temp;
ptr = start;
while(ptr!=NULL)
{ temp = ptr->next;
free(ptr);
ptr=temp;
}
//free(temp);
return;
}
node * insert_before(node *)
{ int value;
node * ptr, * new_node;
ptr = start;
cout<<"Enter the value before which node is to be inserted"<<endl;
cin>>value;
while (ptr->next->data!=value)
ptr=ptr->next;
new_node= new node;
cout<<"Enter the data in the new node:\n";
cin>>new_node->data;
new_node->next=ptr->next;
ptr->next=new_node;
return start;
}
node * delete_node(node *)
{
int value;
node * ptr,*preptr;
cout<<"Enter the value to be deleted"<<endl;
cin>>value;
ptr=start;
while(ptr->data!=value)
ptr=ptr->next;
preptr->next=ptr->next;
free(ptr);
return start;
}