Linear Data Structure 515
14.21 DELETION
Element to
be deleted
(a) Deletion from the linked list
(b)
Linked list after deletion of element 3
Fig. 14.22
In Fig. 14.22(a) linked list elements are shown. The element 3 is to be deleted. After deletion the
linked list would be as shown in Fig. 14.22(b).
Deleting a node from the list has the following three cases.
1) Deleting the first node
2) Deleting the last node
3) Deleting the specific node
While deleting the node, the node which is to be deleted is searched first. If the first node is to be
deleted, then the second node is assigned to the header. If the last node is to be deleted, the last but
one node is accessed and its link field is assigned a N U LL value. If the node which is to be deleted
is in between the linked list, then the predecessor and successor nodes of the specified node are
accessed and linked.
14.23 Write a program to remove elements from the beginning of the linked list.
# include <stdio.h>
# include <conio.h>
# include <malloc.h>
struct num
{ int num;
struct num *next;
} *head er,*first,*rear,*k ;
main ()
516 Programming and Data Structures
{
void rem(void);
void form ( void );
void show (vo id );
c lrs c r ();
prin tf ( n Operation creation :);
form ();
show () ;
printf ( n After deletion : " );
remove();
show () ;
rem ();
show () ;
rem () ;
show () ;
}
void form ( void )
{
struct num *node;
prin tf ( n Enter number : ");
i f (header--NULL)
{
first*(stru c t num*)malloc(sizeof(struct num));
scanf (%d,6first->num );
first->next*header;
header-first;
re ar*first;
}
while (1)
{
node*(struct num*) malloc(sizeof(struct num));
scanf ("%d"#&node->num);
i f (node->num**0) break;
node- >next■NULL;
rear- >next-node;
rear-node;
>
}
void rem()
{
struct num *s;
s*header;
free ( s ) ;
header* first->n ext;
first*first->nex t;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.194