Queues

in #technology6 years ago

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

This makes queue as FIFO(First in First Out) data structure (that element inserted first will be removed first).

The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.

ENQUEUE:

  1. Check if the queue is full or not.
  2. If the queue is full, then print overflow error and exit the program.
  3. If the queue is not full, then increment the tail and add the element.

DEQUEUE:

  1. Check if the queue is empty or not.
  2. If the queue is empty, then print underflow error and exit the program.
  3. If the queue is not empty, then print the element at the head and increment the head.

peek( ) function is oftenly used to return the value of first element without dequeuing it.

NOTE:Like stack, queue is also an ordered list of elements of similar data types.

2000px-Data_Queue.svg.png

Applications of Queue:

Serving requests on a single shared resource, such as a printer, and CPU task scheduling .

cpu-1137501_960_720.jpg

In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free.

Handling of interrupts in real-time systems (first come first served).

Implementation of Queue Data Structure:

Queue can be implemented using an Array, Stack or Linked List.

Array:

The easiest way of implementing a queue is by using an array to make a policy decision that element zero of the array (array[0]) is initially the head and the tail of the queue.

As we add elements to the queue, the tail keeps on moving ahead, always pointing to the position where the next element will be inserted, while the head remains at the first index.

queue_enqueue_diagram.jpg

When we remove an element from Queue, we can follow two possible approaches.

In the first approach, we remove the element at head position, and then one by one shift all the other elements in forward position.

Queue+After+Removing+an+Element.jpg
In the second approach we remove the element from head position and then move head to the next position.

queueOps.png

References:
https://www.studytonight.com/data-structures/queue-data-structure

https://www.sqa.org.uk/e-learning/ArrayDS02CD/page_15.htm

https://www.google.am/search?as_st=y&hl=en&tbm=isch&sa=1&ei=r2PbWuT4CsaTsgGiuo9I&q=add+to+queue+data+structure&oq=add+to+queue+data+structure&gs_l=psy-ab.3...3313.6676.0.6812.17.16.1.0.0.0.169.1410.2j11.13.0....0...1c.1.64.psy-ab..3.0.0....0.9WIhvaeyiwY#imgrc=1p5OHZnx2rLawM:

https://www.google.am/search?biw=1299&bih=626&tbm=isch&sa=1&ei=OGXbWsuqG8ersAHT3IyoDA&q=remove+element+from+queue+with+shifting+head&oq=remove+element+from+queue+with+shifting+head&gs_l=psy-ab.3...5703.7345.0.7590.5.4.1.0.0.0.116.449.0j4.4.0....0...1c.1.64.psy-ab..0.0.0....0.oeREFSBs8zY#imgrc=WjEIv_vBoXYJOM:

Sort:  

Awesome article on queues, mate! This deserves a resteem. Keep it up!

thank you for encouragement.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 57711.87
ETH 3013.92
USDT 1.00
SBD 2.35