My Project - Video Waker Alarm
Get Video alarm on Google Play

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

16 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.

  2. please tell me how to create a c programm to create n numbers of queues in an array of given size .?

  3. the program seems wrong to me….in queue you delete from the beginning not the end…..you have done otherwise.

  4. Queue inserts at REAR and exerts (pops, reads, deletes) at FRONT! You have everything the other way round. I’d prefer it the way it’s done in everyone else’s implementation for consitency with C++ and others! Also you can make this a Ring, such that you can keep adding, after deletion of the last element!

    #define QUEUE_CAPACITY 5
    typedef struct Queue Queue;
    struct Queue {
    int size;
    int front;
    int rear;
    int elements[QUEUE_CAPACITY];
    };

    // insert at the rear of the queue
    void push (Queue* q, int number) {
    if (q->size==QUEUE_CAPACITY) {
    mexPrintf(“queue overfilled”);
    return;
    }
    q->elements[q->rear] = number;
    q->size++;
    q->rear++;
    if (q->rear==QUEUE_CAPACITY) {
    q->rear = 0;
    }
    // mexPrintf(“rear %d \n”,q->rear);
    return;
    }

    // read/remove at the front of the queue
    int pop (Queue* q) {
    int value = q->elements[q->front];
    q->size–;
    q->front++;
    if (q->front==QUEUE_CAPACITY) {
    q->front = 0;
    }
    // debug
    mexPrintf(“popped element: %d\n”, \
    value);
    return value;
    }

    // for debuging only
    void displayQueue(Queue* q) {
    int i;
    mexPrintf(“\nfront: %d rear: %d \n”,q->front,q->rear);
    mexPrintf(“The queue elements are: “);
    for(i=0;isize;i++)
    {
    int el = (q->front+i)%QUEUE_CAPACITY;
    mexPrintf(“%d “,q->elements[el]);
    }
    mexPrintf(“\n”);
    }

    and:

    Queue que = {0};
    Queue* q = &que;
    displayQueue(q);
    push(q,3);
    push(q,1);
    push(q,7);
    push(q,2);
    displayQueue(q);
    push(q,19);
    push(q,24);
    displayQueue(q);
    pop(q);
    displayQueue(q);
    push(q,25);
    displayQueue(q);
    pop(q);
    pop(q);
    pop(q);
    displayQueue(q);
    push(q,8);
    push(q,0);
    push(q,33);
    displayQueue(q);

    Just replace mexPrintf with printf.

  5. I think this program is wrong
    Bcoz if(front== rear) then their is any data or item in queue after underflow condition if this condition hold true for front==rear [only for front=rest=-1]but after using return we come out from loop and then we can use front== rear and by using front=-1 and rear=-1 we can delete item

  6. THIS MUST BE ADDED IN THIS EXAMPLE>> OTHERWISE THERE WILL BE ERROR WHILE DELETING ELEMENT

    if(front>MAX){
    break;
    }
    else {
    printf(“\nEnter the token to be inserted:”);
    scanf(“%d”,&token);
    queue[front]=token;
    front=front+1;
    }

Comments are closed.