Queues

mayukh datta



Queue is a linear data structure like stack and linked list. It follows FIFO (First-in, First-out) or LILO (Last-in, Last-out) policy. It has two main operations - enqueue (insert element; can only be inserted at the rear of a queue) and dequeue (remove element; can only be removed from the front of a queue).

queue-data structure

I have four USB storage devices each containing some data that I need to copy to hard drive. As an USB port can read data from a single device at a time, so I have organized them in a queue such that each device is served one after the other. Now as queue basically follows "First Come, First Serve" rule, therefore the device with index 0 being the first device to be placed in the queue must be served first and the last placed device with index 3 must be served at the last. This is how queue follows FIFO policy.

Like stack, queue can also be implemented by array and linked list.

Code for simple array implementation of queue:-

We have used array in simple and usual manner to implement queue but it might not be the efficient. We can't insert elements into rooms of array where deletion operation took place earlier, therefore memory gets wasted. To learn in details, watch this. So, it would be better to use array in a circular manner where first and last elements are contiguous.

Code for circular array implementation of queue:-

I'm still working on the code for linked list implementation of queue. I'll update this post with it as soon as I complete it. Double-ended queue and priority queue will be discussed later on.

Comments

  1. U made it so interesting and easy👍

    ReplyDelete
    Replies
    1. Thanks, mam. I'm glad that you read maximum of my blogs every time.😌💚

      Delete

Post a Comment

Popular Posts

How to dive into programming?

Inside Data Structures and Algorithms

Stacks

How to dual-boot Kali Linux and Windows 10 in a UEFI System