Implement queue using linked list in c code

Implement queue using linked list in c program. Queue is a data structure, that uses First in First out(FIFO) principle. Queue can be implemented by stack, array and linked list. Also see c program to implement queue using array
Here is a program :

queue using linked list in c

 #include<stdio.h> 
 #include<conio.h>
 #include<stdlib.h>
 struct Node
 {
        int Data;
        struct Node* next;
 }*rear, *front;

void delQueue()
{
       struct Node *temp, *var=rear;
      if(var==rear)
      {
             rear = rear->next;
             free(var);
      }
      else
      printf("\nQueue Empty");
}

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

void display()
{
     struct Node *var=rear;
     if(var!=NULL)
     {
           printf("\nElements are as:  ");
           while(var!=NULL)
           {
                printf("\t%d",var->Data);
                var=var->next;
           }
     printf("\n");
     } 
     else
     printf("\nQueue is Empty");
}

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 valueber 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");
                }
          }
     }
}

Output :

queue using linked list in c

 


7 thoughts on “Implement queue using linked list in c code”

  1. There is a mistake in code. In Del function it should be front = front->next. Similarly in else part of push function it should be rear not front. While pushing rear should be incremented and while deleting front should be incremented.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>