Featured Post

Step Wise Project Planning

Planning is the most difficult process in project management. The framework described is called the Stepwise method to help to distinguis...

Write a program of link list implementation of a queue.

Write a program of link list implementation of a queue.

#include <stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int data ;
struct node *link ;
} ;
struct queue
{
struct node *front ;
struct node *rear ;
} ;
void initqueue ( struct queue * ) ;
void addq ( struct queue *, int ) ;
int delq ( struct queue * ) ;
void delqueue ( struct queue * ) ;
main( )
{
struct queue a ;
int i ;
initqueue ( &a ) ;
addq ( &a, 11 ) ;
addq ( &a, -8 ) ;
addq ( &a, 23 ) ;
addq ( &a, 19 ) ;
addq ( &a, 15 ) ;
addq ( &a, 16 ) ;
addq ( &a, 28 ) ;
i = delq ( &a ) ;
printf ( "\nItem extracted: %d", i ) ;
i = delq ( &a ) ;
printf ( "\nItem extracted: %d", i ) ;
i = delq ( &a ) ;
printf ( "\nItem extracted: %d", i ) ;
delqueue ( &a ) ;
}
void initqueue ( struct queue *q )
{
q -> front = q -> rear = NULL ;
}
void addq ( struct queue *q, int item )
{
struct node *temp ;
temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( temp == NULL )
printf ( "\nQueue is full." ) ;
temp -> data = item ;
temp -> link = NULL ;
if ( q -> front == NULL )
{
q -> rear = q -> front = temp ;
return ;
}
q -> rear -> link = temp ;
q -> rear = q -> rear -> link ;
}
int delq ( struct queue * q )
{
struct node *temp ;
int item ;
if ( q -> front == NULL )
{
printf ( "\nQueue is empty." ) ;
return NULL ;
}
item = q -> front  -> data ;
temp = q -> front ;
q -> front = q -> front -> link ;
free ( temp ) ;
return item ;
}
void delqueue ( struct queue *q )
{
struct node *temp ;
if ( q -> front == NULL )
return ;
while ( q -> front != NULL )
{
temp = q -> front ;
q -> front = q -> front -> link ;
free ( temp ) ;
}
}


Output


Previous
Next Post »