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

 




"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in c program and tagged , . Bookmark the permalink.

6 Responses to Implement queue using linked list in c code

  1. Kurosaki says:

    this is awesome because i can understand it easy!!!

  2. Sun says:

    Bagaimana membuat ADT Queue menggunakan Linked List??

  3. Sun says:

    How to make ADT Queue using Linked List??

  4. Programmer says:

    It’s not working when you push number and then pop it – qeueue will be empty forever

  5. Mucahit says:

    thanks thanks thanks thanks thanks…….. :D

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>