Thursday, October 19, 2017

Program to Implement Queue Using Linked List in C

/*==========================================================================================
 **
 **  File Name     : queue_linkedlist.c
 **  Creation Date : Fri 13 Oct 2017 11:47:03 PM IST
 **  Last Modified : Thu 19 Oct 2017 07:50:35 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *next;
};

struct node *front=NULL, *rear=NULL;

void enqueue(int item);
int dequeue();
void display();
int peek();
int isEmpty();
int main()
{
    int choice,item;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Queue Using Linked List in C = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");     
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Display\n");
        printf("4. Peek: Print the front element\n");
        printf("5. Quit\n");
        printf("\nEnter your choice : ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Enter the number to be added in queue : ");
                scanf("%d",&item);
                enqueue(item);
                printf("Number %d das been added to the queue\n",item);
                break;
            case 2:
                item=dequeue();
                printf("Front element %d has been deleted\n",item);
                break;
            case 3:
                display();
                break;
            case 4:
                printf("Front element in the Queue is: %d\n",peek());
                break;
            case 5:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
                return;
            default :
                printf("Oops.. Wrong choice!!\n");
        }
    }
}

void enqueue(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
        printf("Memory not available\n");
        return;
    }
    tmp->info = item;
    tmp->next=NULL;
    if(front==NULL)
        front=tmp;
    else
        rear->next=tmp;
    rear=tmp;
}

int dequeue()
{
    struct node *tmp;
    int item;
    if(isEmpty( ))
    {
        printf("Queue Underflow\n");
        return;
    }
    tmp=front;
    item=tmp->info;
    front=front->next;
    free(tmp);
    return item;
}

int peek()
{
    if( isEmpty( ) )
    {
        printf("Queue Underflow\n");
        return;
    }
    return front->info;
}

int isEmpty()
{
    if(front==NULL)
        return 1;
    else
        return 0;

}

void display()
{
    struct node *ptr;
    ptr=front;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue elements are:\n\n");
    while(ptr!=NULL)
    {
        printf("%d\t",ptr->info);
        ptr=ptr->next;
    }
    printf("\n\n");
}

OutPut:

manoj@ubuntu:~/ds/queue$ gcc queue_linkedlist.c -o queue_linkedlist
manoj@ubuntu:~/ds/queue$ ./queue_linkedlist
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Queue Using Linked List in C = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 10
Number 10 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 20
Number 20 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 30
Number 30 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 1
Enter the number to be added in queue : 40
Number 40 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 3
Queue elements are:

10    20    30    40   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 2
Front element 10 has been deleted
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 4
Front element in the Queue is: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 3
Queue elements are:

20    30    40   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek: Print the front element
5. Quit

Enter your choice : 5
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~/ds/queue$

No comments:

Post a Comment