Search This Blog

Tuesday 27 October 2015

queue using linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int info;
 struct node *next;
};
struct node *front=NULL;
struct node *rear=NULL;
void insertq(int item)
{
 struct node *p;
 p=(struct node*)malloc(sizeof(struct node));
 p->info=item;
 p->next=NULL;
 if(rear==NULL)
 {
  front=rear=p;
 }
 else
 {
  rear->next=p;
  rear=p;
 }
 printf("\nelement inserted %d",p->info);
}
void deleteq()
{
 struct node *p;
 p=front;
 if(front==NULL)
 printf("\nempty");
 else
 if(front->next==NULL)
 {
  front=rear=NULL;
 }
 else
 {
  front=front->next;
 }
 printf("\nelement deleted %d",p->info);
 free(p);
}
void display()
{
 struct node *p;
 p=front;
 if(p==NULL)
 printf("\nempty");
 {
  while(p!=NULL)
  {
   printf("\n%d",p->info);
   p=p->next;
  }
 }
}
void main()
{
 int item,ch;
 clrscr();
 printf("\nMENU OPTIONS\n1.insert into queue\n2.delete from queue\n3.display\n4.exit\n");
 do
 {
  printf("\nenter your choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("\nenter the element to be inserted");
                  scanf("%d",&item);
                  insertq(item);
                  break;
   case 2:deleteq();
                  break;
   case 3:display();
                  break;
   case 4:exit(0);
   default:printf("invalid choice.........");
  }
 }while(ch!=4);
 getch();

}

No comments:

Post a Comment