Professional Documents
Culture Documents
Ahmed Rafiullah
Enrolment Number: 01-134132-016
Class: BS(CS), 4B
Course: Data Structures and Algorithms
khattak.ahmed@yahoo.com
April 22, 2015
The insert any function first checks for the value to be searched and then checks
if the node at which the value is found is the last ,first or any node other than
first or last and inserts the new node after the searched node. The delete any
function uses the same checks as the insert any function and then deletes the
given node.
Only difficulty that was faced was with the template uploaded it was giving
windows api errors i could not reslove the problem in time and decided to give
the lab work in the standard template.
3
1
11
13
15
# i n c l u d e <i o s t r e a m >
# i n c l u d e <c o n i o . h>
u s i n g namespace s t d ;
c l a s s node {
public :
i n t data ;
node n e x t ;
node p r e v ;
};
class link {
node head ;
node t a i l ;
public :
link ()
{
17
19
21
23
25
27
29
31
33
head = NULL;
t a i l = NULL;
}
void display forward ( )
{
node x ;
f o r ( x = head ; x != NULL; x = x>n e x t )
{
c o u t << x>data << e n d l ;
}
}
void display backward ( )
{
node x ;
f o r ( x = t a i l ; x != NULL; x = x>p r e v )
{
c o u t << x>data << e n d l ;
}
35
37
39
41
43
45
}
void delete head ( )
{
i f ( head != NULL)
{
node temp ;
temp = head ;
head = head>n e x t ;
head>p r e v = NULL;
d e l e t e temp ;
}
47
49
51
53
55
57
}
void d e l e t e t a i l ( )
{
i f ( t a i l != NULL)
{
node temp ;
temp = t a i l ;
t a i l = t a i l >p r e v ;
t a i l >n e x t = NULL;
d e l e t e temp ;
}
59
61
63
65
67
69
71
}
void i n s e r t l a s t ( i n t value )
{
node temp ;
temp = new node ;
temp>n e x t = NULL;
temp>p r e v = NULL;
temp>data = v a l u e ;
i f ( head==NULL)
{
head = t a i l = temp ;
}
else
73
75
77
79
81
83
85
87
89
91
93
95
{
temp>p r e v = t a i l ;
t a i l >n e x t = temp ;
}
t a i l = temp ;
}
void i n s e r t f i r s t ( i n t value )
{
node temp ;
temp = new node ;
temp>n e x t = NULL;
temp>p r e v = NULL;
temp>data = v a l u e ;
i f ( head == NULL)
{
head = t a i l = temp ;
}
else
{
temp>n e x t = head ;
head>p r e v = temp ;
}
head= temp ;
97
99
101
103
105
107
109
111
113
115
117
119
121
123
125
}
void delete any ( i n t index )
{
node temp1 ;
node temp2 ;
node remove ;
node x ;
f o r ( x = head ; x != NULL; x = x>n e x t )
{
i f ( x>data == i n d e x && x>n e x t != NULL && x>p r e v != NULL)
// f o r any node o t h e r than l a s t o r f i r s t node
{
remove = x ;
temp1 = x>p r e v ;
temp2 = x>n e x t ;
temp1>n e x t = temp2 ;
temp2>p r e v = temp1 ;
d e l e t e remove ;
b r e a k ; // b r e a k s t h e l o o p s i n c e we want o n l y one c a l l o f t h i s
f u c n t i o n t o d e l e t e one node
}
e l s e i f ( x>data== i n d e x && x>p r e v == NULL) // f o r head
node
{
head = x>n e x t ;
head>p r e v = NULL;
remove= x ;
d e l e t e remove ;
break ;
}
e l s e i f ( x>data == i n d e x && x>n e x t == NULL)
// f o r t a i l
node
{
127
t a i l = x>p r e v ;
t a i l >n e x t = NULL;
remove = x ;
d e l e t e remove ;
break ;
129
131
133
135
}
137
139
141
143
145
147
149
151
}
v o i d i n s e r t a n y ( i n t index , i n t data )
{
node temp ;
node temp1 ;
temp = new node ;
node x ;
temp>data = data ;
temp>n e x t = NULL;
temp>p r e v = NULL;
f o r ( x = head ; x != NULL; x = x>n e x t )
{
i f ( x>data== i n d e x && x>n e x t != NULL && x>p r e v != NULL) //
f o r any node o t h e r than f i r s t o r l a s t
{
153
155
157
159
161
163
165
167
169
171
temp1 = x>n e x t ;
temp1>p r e v = temp ;
temp>n e x t = temp1 ;
temp>p r e v = x ;
x>n e x t = temp ;
break ;
// b r e a k t h e l o o p
}
e l s e i f ( x>data == i n d e x && x>p r e v == NULL)
head node
{
// i n s e r t f o r
173
175
}
e l s e i f ( x>data== i n d e x && x>n e x t == NULL)
l a s t node
{
177
179
// i n s e r t a f t e r
temp>n e x t = NULL;
x>n e x t = temp ;
temp>p r e v = x ;
t a i l = temp ;
break ;
181
183
185
}
}
187
}
189
191
193
195
197
199
201
203
205
207
209
211
213
215
217
219
221
223
225
227
229
231
233
};
v o i d main ( )
{
link l ;
char c h o i c e ;
int x ;
int y ;
do {
system ( c l s ) ;
c o u t << \ n1 . i n s e r t f r o n t \ n2 . i n s e r t r e a r \ n3 . i n s e r t a f t e r any
node \ n4 . d e l e t e f r o n t \ n5 . d e l e t e r e a r \ n6 . d e l e t e any node \ n7 . show
backwards \ n8 . show f o r w a r d s ;
c o u t << e n d l << Enter c h o i c e : ;
c i n >> c h o i c e ;
switch ( choice ) {
case 1 :
c o u t << e n t e r an i n t e g e r v a l u e : ;
c i n >> x ;
l . i n s e r t f i r s t (x) ;
break ;
case 2 :
c o u t << e n t e r an i n t e g e r v a l u e : ;
c i n >> x ;
l . i n s e r t l a s t (x) ;
break ;
case 3 :
c o u t << e n t e r an i n t e g e r v a l u e t o s e a r c h f o r : ;
c i n >> x ;
c o u t << e n t e r an i n t e g e r v a l u e : ;
c i n >> y ;
l . insert any (x , y) ;
break ;
case 4 :
l . delete head () ;
break ;
case 5 :
l . d e l e t e t a i l () ;
break ;
case 6 :
c o u t << e n t e r an i n t e g e r v a l u e t o d e l e t e
:;
c i n >> x ;
l . delete any (x) ;
break ;
case 7 :
l . display backward () ;
getch () ;
break ;
case 8 :
l . display forward () ;
getch () ;
break ;
}
} w h i l e ( c h o i c e != n ) ;
235
237
239
241
243
245
getche () ;
247