500 Programming and Data Structures
2) rear - >next=node;
The rear pointer keeps track of the last node, the address of current node (node) is assigned to link
field of the previous node.
3) rear=node;
Before creating the next node, the address of the last created node is assigned to pointer rear, which
is used for statement (2). In function main (), using while loop the elements of the linked list are
displayed.
Header: The pointer header is very useful in the formation of a linked list. The address of first
node (1888) is stored in the pointer header. The value of the header remains unchanged until
it turns out to be NULL. The starting location of the list can only be determined by the pointer
header.
while (headerIsNULL)
{
printf ( %d ", header->num);
header-header->next;
>
Traverse a list with header. We are familiar with traverse operation, which involves visiting all the
elements of the queue. The above w h ile () loop and the associated statement are used to traverse
and display the elements of the list.
Linked List without Header
In the last topic we discussed how a linked list can be created using a header. The creation of a
linked list without header is same as that of a linked list with header. The difference in manipulation
is that, in the linked list with header, the pointer header contains the address of the first node. In
the without header list, pointer first itself is the starting of the linked list. Consider the following
urogram.
14.17 Write a program to create a linked list without header.
# include cstdio.h>
# include cconio.h>
# include cmalloc.h>
struct num
{
int num;
struct num *next;
} "fir st ,* re a r ;
main ()
{ void form ( vo id );
form ();
c lrs c r ();
printf ("Linked list elements : ");
Linear Data Structure 501
while (firstI-NULL)
{
printf (■ %d", first->num);
firs t«first-> n e xt;
}
}
void form ( void )
{
struct num *node;
printf ( an Enter number : );
i f (fir s t MULL)
{
firs t*(str u c t num *)malloc(sixsof(struct n ua));
scanf ("%d”,6 first-> n u n );
fir s t-> n e x t * firs t ;
rs a ra fir s t;
}
whiIs (1)
{
node*(struct num*) m alloc(sizeof(struct num));
scanf ( %d,&node->num);
i f (node->nua»0) break;
node- >next«NULL;
rear- >next-node;
rearanode;
}
}
OUTPUT;
Enter number 15987420
Linked list elements: 1 5 9 8 7 4 2
Explanation In this program only two pointers are declared: first and rear. They are global pointers,
hence their contents are NULL. The function form () is invoked and the remaining execution of the
program is the same as the previous program.
Table 14.1 Traverse a list without header
while (first 1=NULL)
{
printf(" %d",first->num);
first=first->next;
}
502 Programming and Data Structures
In the list without header, the lists are traversed with the first node as shown in the above code.
14.18 Write a program to create a single linked list and display the elements.
# include <stdio.h>
# include <conio.h>
struct node
{
int number;
struct node *p;
} *h,
main ()
{
char c ;
c lrs c r();
f=NULL;
do
{
h=(struct node * ) malloc(sizeof(struct node));
printf ("Enter a number : ");
scanf ("%d",&h->number);
i f (fI=NULL)
{
t->p=h;
t*h;
}
else f=t=h;
fflu sh (s tdin );
printf ("Continue (y/n) : " );
scanf ( "%c",&c);
} while (c »* 'y *);
t->p*NULL;
t * f ;
prin tf ("nThe l is t elements are s ");
while ( t !eNULL)
{
printf (" %d " , t->number);
t*t->p;
>
}
OUTPUT:
Enter a number: 5
Continue (y/n): y
..................Content has been hidden....................

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