Showing posts with label Queue Operations using Single Linked List. Show all posts
Showing posts with label Queue Operations using Single Linked List. Show all posts

Thursday, 17 October 2013

Queue Operations using Single Linked List

Queue Operations using Single Linked List

#include<stdio.h>
#include<conio.h>
#include <stdlib.h>    

struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;

void insert()
{
int item;
struct node *temp,*tempe;

printf("\nEnter the Number : ");
scanf("%d",&item);
temp=(struct node *) malloc(sizeof(struct node));

temp->data=item;
temp->next=NULL;

if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}



void del()
{


if(front==NULL)
printf("\Queue is Empty ");
else if(rear==front)
front=rear=NULL;
else
{
printf("%d is Deleted ",front->data);
front=front->next;
}
}

void display()
{
struct node *temp;

if(front==NULL && rear==NULL)
printf("\nQueue is Empty");
else
{
temp=front;
printf("\n Elements are : ");
while(temp->next!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}

void main()
{
int ch;
while(1)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:  insert();break;
case 2:  del();break;
case 3:  display();break;
case 4: exit(0);break;
            default: printf("\nYour Choice is Woring");
}
}

}