Search This Blog

Saturday, March 8, 2014

Queue using Linked List


Queue Using Linked List

#include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 struct Node
 {
        int Data;
        struct Node* next;
 }*rear, *front;
rear=NULL;
front=NULL:
void delQueue()
{
       struct Node *r;
      int ele;
if(front==NULL)
{
printf("empty");
}
else
{
     r=front;
    ele=r->Data ;
      front=front->next;
     free(r);
    printf("%d",ele);
}
    
}

void push(int value)
{
     struct Node *temp;
     temp=(struct Node *)malloc(sizeof(struct Node));
     temp->Data=value;
     temp->next=NULL;
     if (front == NULL)
     {
           front=temp;
           rear=front;
     }
     else
     {
           rear->next=temp;
           rear=temp;

     }
}

void display()
{
     struct Node *r=front;
if(front==NULL)
{
     printf("\nQueue is Empty");
}
else
{

     while(r!=NULL)
     {
           printf("\nElements are as:  ");
           printf("\t%d",r->Data);
                r=r->next;
           }
     printf("\n");
     }
   
}

int main()
{
     int i=0;
     front=NULL;
     printf(" \n1. Push to Queue");
     printf(" \n2. Pop from Queue");
     printf(" \n3. Display Data of Queue");
     printf(" \n4. Exit\n");
     while(1)
     {
          printf(" \nChoose Option: ");
          scanf("%d",&i);
          switch(i)
          {
                case 1:
                {
                     int value;
                     printf("\nEnter a value to push into Queue: ");
                     scanf("%d",&value);
                     push(value);
                     display();
                     break;
                }
                case 2:
                {
                     delQueue();
                     display();
                     break;
                }
                case 3:
                {
                     display();
                     break;
                }
                case 4:
                {
                     exit(0);
                }
                default:
                {
                     printf("\nwrong choice for operation");
                }
          }
     }
}

No comments:

Post a Comment