Linear Data Structure 495
int r=6,f*0,n;
clrscr () ;
printf (" In itia l values rear-%d front-% d*,r,f) j
printf ("n How many elements u want to delete : );
scanf ("%d”,6n);
while (f<n)
{
queue[f] «NULL;
f++;
printf ( rear«%d front-% d",r,f) j
}
printf (n Queue elements are : ”);
for ( n«0?n<7;n++)
printf ( %d ,queue[n]);
OUTPUT;
Initial values rear=6 front=*0
How many elements u want to delete: 3
rear=6 front=l
rear=6 front=2
rear=6 front=3
Queue elements are: 0 0 0 4 5 6 7
Explanation The above program is the second part of the first program. We have started exactly
from where we ended the last program. The output of the last program is the starting of this program.
The array is initialized with entered values and rear and front are initialized to 6 and 0, respectively.
The user is asked to enter the number of elements to be deleted. The loop is executed for n times
and 0 to n elements of the array are nullified. At every deletion operation the front is shifted to
inside. It is shifted from 0 to 3 when three elements are deleted. The output of the program is easy to
follow.
b) Dynamic implementation: Dynamic implementation is carried out using pointers. By applying
increment operation on a pointer successive locations of memory can be accessed and an element
can be stored in that location. Thus, the series of element can be continued to any number of
elements. The programmer has to keep the starting address of the pointer in which the first
element is stored. Thus, in the same way the numbers can be viewed or altered. Here is a
simple example.
14.18 SINGLE LINKED LIST
In this type of linked list, two successive nodes of the linked list are linked with each other in a
sequential linear manner. Fig. 14.17 describes a single link list. It is like a train in which two successive
bogies are connected. The train is an example of a single linked list.
496 Programming and Data Structures
Start
5 &
7
& 9
Fig. 14.17 Single linked list
Recall that a single linked list is a dynamic data structure with the ability to expand and shrink
as per the program requirement. The single liked list is an easy and straightforward data structure
as compared to other structures. By changing the link position other types of linked lists such
as circular, doubly linked list can be formed. For creating a linked list the structure is defined as
follows.
struct node
{
int number;
struct node *p;
} ;
The above structure is used to implement the linked list. In the number, variable the entered numbers
are stored. The second member is pointer to the same structure. The pointer *p points to the same
structure. Here, though the declaration of struct node has not been completed, the pointer declaration
of the same structure type is permitted by the complier. However, the variable declaration is not
allowed. This is because the pointers are dynamic in nature, whereas variables are formed by early
binding. The declaration of objects inside the struct leads to the preparation of very complex data
structure. This concept is called object composition and its detailed discussion is out of the scope of
this book.
We are familiar with the array and we know the importance of base address. Once a base address
is obtained, successive elements can be accessed. In the linked list, the list can be created with or
without header node. The head holds the starting address.
14.15 Write a program to create a simple linked list.
# include <stdio.h>
# include <conio.h>
struct lis t
{
int n;
int *pj
} ;
main ()
{
struct lis t itemO,iteml, item2;
item2.n«3;
item2 .p*NULL;
iteml.n=5;
..................Content has been hidden....................

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