Queue Fundamentals Part-1

Queue Fundamentals Part-1

You might have come across the queue in the shopping mall billing center. So, the queue in the data structure is exactly the same kind of stuff. queu inShopping Mall.jpg

NOTE: As the concept of the queue is lengthier. I have done some blogs in parts.

Well, the Definition of the queue goes likes this:

Queue is a linear data structure that follows a particular order in which both ends are open.

As we all know came to know that person who is standing at the front of the queue will get served first. Similarly, the last served last. Therefore it is said that the queue obeys the First In First Out(FIFO) principle.

Same in the stack here there are operations namely,insertion(enqueue),deletion(dequeue),and display.

queue.pngHere insertion and deletion take place at different ends.

Rear: is the place where insertion is done.

Front: is the place where deletion is done.

Let us jump to the programming section by implementing queue using arrays.

So the very first we need to do is to create a queue...

To create the queue:

createQueue(){
    #define SIZE 10
    int rear, front,queue(SIZE),i;
}

Here is the simple create function in which we firstly define the maximum number of elements queue can contain, then we declare pointer variables like rear(to point the rear end of the queue), front(to point the front end of the queue), the queue is the array name, i variable is used for iteration.

To insert an element in the queue:

void insert(){
    int item,rear=-1;
    if(rear==SIZE-1){
        printf("QUEUE IS FULL");
        return 0;
    }
    printf("Enter the item to be inserted ");
    scanf("%d",&item);
    rear++;
    queue[rear]=item;
}

In the code above we check whether the space is available or not inside the queue. If it's available we insert the item inside the queue by incrementing rear(as rear increments it points to ner location). If the space is unavailable it prints the message "QUEUE IS FULL".

To delete an item from the stack:

void delete(){
    front=0;
    if(front>rear){
        printf("QUEUE IS EMPTY\n");
        return 0;
    }
    front++;
    printf("Element deleted= %d\n",queue[front]);
}

The above code depicts the delete function, first, it checks whether there are elements present to delete or not. If it is present it increases the front value by one. then deletes it. If there are no elements found in the queue it prints the message "QUEUE IS EMPTY".

To display the contents of the queue:

void display(){
    if(front>rear){
        printf("QUEUE IS EMPTY\n");
        return 0;
    }
    printf("Contents of the queue are:\n");
    for(i=front;i<=rear;i++)
    printf("%d\n",queue[i]);
}

Here also first it checks whether there are elements present to delete or not. If present it displays the queue using for loop. If there are no elements found in the queue it prints the message "QUEUE IS EMPTY".

Applications:

  • in system software development

  • keyboard buffer

  • In the call centers phone system

Refer to the link for the complete program %[Link]

This was about the part-1 of the queue data structure. meet you in the next part...

THANK YOU, READERS:)

-Preksha