C program for implementation of linear queue using linked list

Posted: April 3, 2012 in Lab 8
/*Linear Queue implementation using linked list
Written by Shivam Rana
Date- 2-4-12*/
#include<stdio.h>
#include<stdlib.h>
#define que struct queue
#define pf printf
#define sf scanf
struct queue{
int info;
struct queue *link;
};
 que *front=NULL,*rear=NULL;
int count=0;
void push(int n)
{
que *temp,*newnode;
temp=front;
newnode=(struct queue*)malloc(sizeof(struct queue));
newnode->info=n;
newnode->link=NULL;
if(count==0)
front=newnode;
else
{
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
}
count++;
}
int pop(void)
{
int n;
que *temp,*newnode;
if(count==0)
return NULL;
count--;
temp=front;
n=temp->info;
front=temp->link;
free(temp);
return n;
}
void display()
{
que *temp;
int i;
if(count==0)
pf("Empty");
else
{
temp=front;
for(i=0;i<count;i++)
{
pf("%d ",temp->info);
temp=temp->link;
}
}
pf("\n");
}
int size(void)
{
return count;
}
int main()
{
int n,ch=10;
while(ch!=0)
{
pf("\n       What do you want to do??\n");
pf("1.Push\n");
pf("2.Pop\n");
pf("3.SizeOfQueue\n");
pf("4.Display\n");
pf("0.EXIT\n");
sf("%d",&ch);
switch(ch)
{
case 1:
{
pf("What no. do you want to push in queue\n");
sf("%d",&n);
push(n);
break;
}
case 2:
{
n=pop();
if(n==NULL)
pf("Queue is empty\n");
else
pf("Number poped from queue is %d\n",n);
break;
}
case 3:
{
n=size();
pf("Size of queue is %d\n",n);
break;
}
case 4:
{
pf("Queue is -->> ");
display();
}
case 0:
break;
default:
pf("Wrong Choice\n");
break;
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s