C Program to implement Queue using circular linked list

By | 10.04.2017

Queue using circular linked list


Write a C Program to implement queue using circular linked list. Here’s simple Program to implement queue using circular linked list in C Programming Language.


What is Queue ?


Queue is also an abstract data type or a linear data structure, in which the first element is inserted from one end called REAR, and the deletion of existing element takes place from the other end called as FRONT.

This makes queue as FIFO data structure, which means that element inserted first will also be removed first.


Basic Operations : :


  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue.
  • peek() − Gets the element at the front of the queue without removing it.
  • isfull() − Checks if the queue is full.
  • isempty() − Checks if the queue is empty.

Below is the source code for C Program to implement queue using circular linked list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to implement queue using circular linked list*/

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

struct node
{
        int info;
        struct node *link;
}*rear=NULL;

void insert(int item);
int del();
void display();
int isEmpty();
int peek();

int main()
{
        int choice,item;
        while(1)
        {
                printf("\n1.Insert\n");
                printf("2.Delete\n");
                printf("3.Peek\n");
                printf("4.Display\n");
                printf("5.Quit\n");
                printf("\nEnter your choice : ");
                scanf("%d",&choice);

                switch(choice)
                {
                 case 1:
                        printf("\nEnter the element for insertion : ");
                        scanf("%d",&item);
                        insert(item);
                        break;
                 case 2:
                        printf("\nDeleted element is %d\n",del());
                        break;
                 case 3:
                        printf("\nItem at the front of queue is %d\n",peek());
                        break;
                 case 4:
                        display();
                        break;
                 case 5:
                        exit(1);
                 default:
                        printf("\nWrong choice\n");
                }/*End of switch*/
        }/*End of while*/
}/*End of main()*/

void insert(int item)
{
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=item;
        if(tmp==NULL)
        {
                printf("\nMemory not available\n");
                return;
        }

        if( isEmpty() ) /*If queue is empty */
        {
                rear=tmp;
                tmp->link=rear;
        }
        else
        {
                tmp->link=rear->link;
                rear->link=tmp;
                rear=tmp;
        }
}/*End of insert()*/

del()
{
        int item;
        struct node *tmp;
        if( isEmpty() )
        {
                printf("\nQueue underflow\n");
                exit(1);
        }
        if(rear->link==rear)  /*If only one element*/
        {
                tmp=rear;
                rear=NULL;
        }
        else
        {
                tmp=rear->link;
                rear->link=rear->link->link;
        }
        item=tmp->info;
        free(tmp);
        return item;
}/*End of del()*/

int peek()
{
        if( isEmpty() )
        {
                printf("\nQueue underflow\n");
                exit(1);
        }
        return rear->link->info;
}/* End of peek() */

int isEmpty()
{
        if( rear == NULL )
                return 1;
        else
                return 0;
}/*End of isEmpty()*/


void display()
{
        struct node *p;
        if(isEmpty())
        {
                printf("\nQueue is empty\n");
                return;
        }
        printf("\nQueue is :\n");
        p=rear->link;
        do
        {
                printf("%d ",p->info);
                p=p->link;
        }while(p!=rear->link);
        printf("\n");
}/*End of display()*/

OUTPUT : :


1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 1

Enter the element for insertion : 1

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 1

Enter the element for insertion : 2

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 1

Enter the element for insertion : 3

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 4

Queue is :
1 2 3

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 2

Deleted element is 1

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 3

Item at the front of queue is 2

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 2

Deleted element is 2

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 4

Queue is :
3

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 2

Deleted element is 3

1.Insert
2.Delete
3.Peek
4.Display
5.Quit

Enter your choice : 2

Queue underflow

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.2 5 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Swetha Chowdary

nice program structure