Queue implementation using Linked List in C Language





#include<stdio.h>
#include<conio.h>
struct Node
{
   int data;
   struct Node *next;
}*front = NULL,*rear = NULL;

void enQueue(int);
void deQueue();
void search(int);
void display();
void main()
{
   int value, choice;
   clrscr();
   while(1)
   {
      printf("\n\n***** MENU *****");
      printf("\n---------------------------");
      printf("\n 1. Insertion");
      printf("\n 2. Deletion");
      printf("\n 3. Search");
      printf("\n 4. Display");
      printf("\n 5. Exit");
      printf("\nEnter your choice: ");
      scanf("%d",&choice);
      switch(choice)
      {
case 1: 
                 printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: 
                 deQueue();
break;
case 3: 
 printf("Enter the value to be searched: ");
scanf("%d",&value);
search(value);
 
  case 4: 
display();
break;
case 5:
exit(1);

default: printf("\nWrong selection!!! Try again!!!");
      }
   }
}
void enQueue(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode -> next = NULL;
   if(front == NULL)
      front = rear = newNode;
   else
   {
      rear -> next = newNode;
      rear = newNode;
   }
   printf("\nInsertion is Success!!!\n");
   getch();
}
void deQueue()
{
   struct Node *temp;
   if(front == NULL)
      printf("\nQueue is Empty!!!deletion is not possible");
   else if(front==rear)
   {
      temp = front;
      front = NULL, rear=NULL;
    }
   else
   {
      temp = front;
      front = front -> next;
   }
   printf("\n Deleted element: %d", temp->data);
   free(temp);
   getch();
}
void search(int n)
{
   int found=0;
   if(front == NULL)
   {
      printf("\nQueue is Empty!!! search is not possible!!!");
      return;
   }
   else
   {
      struct Node *temp = front;
      while(temp!= NULL)
      {
       if(temp->data==n)
       {
found=1;
break;
       }
       temp=temp->next;
      }
    if(found==1)
     printf("\n %d found in the queue ",n);
    else
     printf("\n %d not found in the queue",n);
   }
   if(found==1)
     printf("\n %d found in the queue ",n);
   else
     printf("\n %d not found in the queue",n);  
   getch();
 }
}
void display()
{
   printf("\n Queue status..:");
   if(front == NULL)
      printf("\nQueue is Empty!!!\n");
   else
   {
      struct Node *temp = front;
      while(temp->next != NULL)
      {
printf("%d--->",temp->data);
temp = temp -> next;
      }
      printf("%d--->NULL",temp->data);
   }
   getch();
}

Post a Comment

0 Comments