C Program to Implement Queue using Linked List

Queue operations using Linked List


     As we know implementation of queue using array has some limitations i.e predefined storage space therefore we can't add elements more than specified array size, In queue we can add unlimited number of elements, so now are going to see queue implementation using Linked List. In queue elements are stored in FIFO manner which mean First In First Out, In short elements will be removed in order of insertion. In program given below we are going to perform all major operations like enqueue, dequeue, display, queue size, and exit. To understand the working of program we have added comments explaining the working after every important statements.

Also Read: C Program to perform Stack Using linked List 

Program:


 /* C Program to Implement Queue Data Structure using Linked List by SlashMyCode.com
 */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0; //initially queue don't have any element so count=0

void main()
{
    int no, ch, e;

    printf("\n 1 - Enque (ENTER)");
    printf("\n 2 - Deque (DELETE)");
    printf("\n 3 - Front element");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Display");
    printf("\n 7 - Queue size");
    create();
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            enq(no);
            break;
        case 2:
            deq();
            break;
        case 3:
            e = frontelement();
            if (e != 0)
                printf("Front element : %d", e);
            else
                printf("\n No front element in Queue as queue is empty");
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            queuesize();
            break;
        default:
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}

/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\n Queue size : %d", count);
}

/* Entering data in the queue */
void enq(int data)
{
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        rear->info = data;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        temp->info = data;
        temp->ptr = NULL;

        rear = temp;
    }
    count++;   //since element is added 
}

/* Displaying the queue elements */
void display()
{
    front1 = front;

    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
    while (front1 != rear)
    {
        printf("%d ", front1->info);
        front1 = front1->ptr;
    }
    if (front1 == rear)
        printf("%d", front1->info);
}

/* Deleting element from the queue */
void deq()
{
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: queue is Empty");
        return;
    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Deleted value : %d", front->info);
            free(front);
            front = front1;
        }
        else
        {
            printf("\n Deleted value : %d", front->info);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--; //since element is removed
}

/* Returns the front element of queue */
int frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->info);
    else
        return 0;
}
  /* C Program to Implement Queue Data Structure using Linked List by SlashMyCode.com */
/* Display if queue is empty or not */
void empty()
{
     if ((front == NULL) && (rear == NULL))
        printf("\n Queue empty");
    else
       printf("Queue not empty");

}


Output:

this is the output if we perform all the operations in a queue
------------------------------------------------------
 1 - Enque (ENTER)
 2 - Deque (DELETE)
 3 - Front element
 4 - Empty
 5 - Exit
 6 - Display
 7 - Queue size
 Enter choice : 1
Enter data : 12

 Enter choice : 1
Enter data : 14

 Enter choice : 1
Enter data : 9

 Enter choice : 3
Front element : 12
 Enter choice : 7

 Queue size : 3
 Enter choice : 6
12 14 9
 Enter choice : 2

 Deleted value : 12
 Enter choice : 5
------------------------------------------------------







No comments:

Post a Comment

Feel free to ask us any question regarding this post