c program for queue using array

c program for queue using array. Queue is a data structure which works as FIFO principle. FIFO means “First in First out”, i.e the element which we have inserted first will be deleted first and the element that we have inserted last will be deleted last. You can have c program to implement queue using array, using stack and using linked list. Two variables are used to implement queue, i.e “rear” and “front”. Insertion will be done at rear side and deletion will be performed at front side. Figure below will show you and will make some concept of queue.

c program for queue using array

With simple queue, you can also have circular queue data structure and dequeue data structure.

c program for queue using array

 

#include<stdio.h>
#include<conio.h>
#define MAX 10

void insert(int);
int del();
int queue[MAX], rear=0, front=0;
void display();
int main()
{
     char ch , a='y';
     int choice, token;
     printf("1.Insert");
     printf("\n2.Delete");
     printf("\n3.show or display");
     do
     {
           printf("\nEnter your choice for the operation: ");
           scanf("%d",&choice);
          switch(choice)
          {
                case 1: insert(token);
                display();
                break;

                case 2:
                token=del();
                printf("\nThe token deleted is %d",token);
                display();
                break;

                case 3:
                display();
                break;

                default:
                printf("Wrong choice");
                break;
          }
          printf("\nDo you want to continue(y/n):");
          ch=getch();
    }
    while(ch=='y'||ch=='Y');
    getch();
}

void display()
{
    int i;
    printf("\nThe queue elements are:");
    for(i=rear;i<front;i++)
    {
        printf("%d ",queue[i]);
    }
}

void insert(int token)
{
    char a;
    if(rear==MAX)
    {
        printf("\nQueue full");
        return;
    }
    do
    {
        printf("\nEnter the token to be inserted:");
        scanf("%d",&token);
        queue[front]=token;
        front=front+1;
        printf("do you want to continue insertion Y/N");
        a=getch();
    }
    while(a=='y');
}

int del()
{
    int t;
    if(front==rear)
    {
        printf("\nQueue empty");
        return 0;
    }
    rear=rear+1;
    t=queue[rear-1];
    return t;
}

Output of c program for queue using array:

c program for queue using array

7 thoughts on “c program for queue using array”

  1. This program needs a bit extension, for more clarity, when queue is full, enqueue operation should be denied.

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>