Linear Data Structure 497
iteml.p=item2.n;
itemO.n«7;
itemO .p*iteml.n;
clrscrO ;
printf ( n Linked li s t elements are: );
printf ( %d " , itemO.n);
printf (" %d " , itemO.p);
printf (" %d " , iteml.p);
printf ( %d ,item2.p);
}
QUTTUT;
Linked list elements are: 7 5 3 0
Explanation In the above program the structure list is declared with two elements int n and *p.
The pointer *p recursively points to the same structure. The struct itemO, iteml and item2 are three
variables of type list Consider the initialization given below.
item2.n=3;
item2.p=NULL;
The item2 is the third (last) node of the list. The pointer p is initialized with NULL. This is because
next to this no node is present and thus there is no need to point to any address.
iteml.n=5;
iteml .p=item2.n;
In this node, n is assigned a value of 5. The pointer points to the data field of the next node, that is,
item2.n (7).
item0.n=7;
item0.p=iteml.n;
14.19 LIN K ED L IS T W ITH AN D W IT H O U T HE A D E R
Linked List w ith H eader
The following steps are used to create a linked list with header.
1) Three pointers—header, first and rearare declared. The header pointer is initially initialized
with NULL. For example, header=NULL, where header is pointer to structure. If it remains NULL
it implies that the list has no element. Such a list is known as a NULL list or an empty list.
NULL
2) In the second step, memory is allocated for the first node of the linked list. For example, let the
address of the first node be 1888. An integer, say 8, is stored in the variable num and the value of
header is assigned to pointer next.
Header
498 Programming and Data Structures
Header
First first - >next
1888 8 NULL
Both the header and rear are initialized the address of the first node.
The statement would be
header=first;
rear=first;
3) The address of pointer first is assigned to pointers header and rear. The rear pointer is used to
identify the end of the list and to detect the NULL pointer.
4) Again, create a memory location, suppose 1890, for the successive node,
node node->next
node node - >next
1890
H 10
NULL
5) Join the element of 1890 by assigning the value of node rear->next. Move the rear pointer to the
last node.
1888
8
10
i
L
The following program explains the above concept.
14.16 Write a program to create a linked list with header.
# include <stdio*h>
# include <conio.h>
# include <malloc.h>
struct num
{
int num;
struct num *next;
} *h eader,*first, *rear;
main ()
{
void form { v o id );
f orm O i
c lrs c r () ;
prin tf ( n Linked l is t elements are : " ) ;
while (header1«NULL)
{
printf (■ %d ", header->num);
header^header- >next;
}
Linear Data Structure 499
void form ( void )
{
struct num *node;
printf ("n Enter number : );
i f (header-«NULL)
{
firs ts(stru c t num*)malloc(sizeof(struct num));
scanf ( %d,&first->num);
first->next-header ;
header-first;
re a r-first;
>
while (1)
{
node-(struct num*) m alloc(sizeof(struct num));
scanf ( %d",&node->num);
i f (node->num--0) break;
node- >next-NULL;
rear- >next-node;
rear-node;
}
}
OUTPUT:
Enter number. 13 4 8 7 9 0
Linked list elements are: 1 3 4 8 7 9
Explanation The above program is an example of a linked list with header. Three structure pointers
*header, *first and *rear are declared after structure declaration. Initially, these pointers are NULL
because they are declared as global. The form function is used to create linked list nodes. Inside the
function form () another structure pointer*node is defined and its scope is local to the same function.
The procedure for creating first and later successive nodes is different. The i f () statement checks
the value of header. The value of header is NULL. The m a llo c () function allocates memory to
pointer first and the entered number is stored in variable num of the node. In the same i f () block,
both the pointers header and rear are assigned the value of first. Once the first node is created, next
time the execution of i f () block is not required. The while loop is iterated continuously
and successive nodes are created by allocating memory to pointer node. Consider the following
statements.
1) node - >next=NULL;
The above statement assigns NULL to the pointer next of current node. If the user does not want to
create more nodes, the linked list can be closed here.
..................Content has been hidden....................

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