C Program to implement linked queue

This program implements linked queue in c. A linked queue uses linked list to insert and delete elements. A queue is a data structure where new items are entered on the rear and are removed form from the front. The advantage of linked queue is that the items need no be in contiguous memory locations. This program implements enqueue, dequeue and display operations of a linked queue. The terms enqueue and dequeue represents insertion and deletion operations respectively.


Program


//Demonstration of linked queue

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

}*f=NULL,*r=NULL,*p;

void main()

{

int ch,item;

printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit ");

scanf("%d",&ch);

while(ch!=4)

{

switch(ch)

{

case 1:

printf("\nEnter the data to be inseted ");

scanf("%d",&item);

p=(struct node *)malloc(sizeof(struct node));

if(r==NULL)

{

f=p;

r=p;

p->data=item;

p->next=NULL;

break;

}

p->data=item;

p->next=NULL;

r->next=p;

r=p;

break;

case 2:

if(r==NULL&&f==NULL)

{

printf("\nQueue is empty\n");

break;

}

if(f->next==NULL&&r->next==NULL)

{

free(f);

f=NULL;

r=NULL;

}

p=f;

f=f->next;

free(p);

break;

case 3:

if(f==NULL&&r==NULL)

{

printf("\nQueue is Empty\n");

break;

}

p=f;

while(p!=NULL)

{

printf("%d->",p->data);

p=p->next;

}

break;

}

printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit ");

scanf("%d",&ch);

}

}


No comments: