Stacks and Queues

Stacks and Queues are linear data structures. A data structure is said to be linear if its elements are stored in a sequential manner. Therefore, in this data structure, a linear relationship is formatted between elements with the help of sequential memory locations.

Stacks

A stack is an ordered collection of an item in which new items may be inserted and the item may be deleted at one end, called the top of the stack. A stack is also known as LIFO structure because the element which is inserted last is the first element to be deleted.

By the definition of the stack, there is no upper limit on the number of items that can be kept in a stack. Pushing element results in a larger collection of items on stack and popping an element leads to a lesser collection of elements.

Representation of Stack

Primitive Operations on Stack

There are two primitive operations performed on the stack.

Insertion Operation on Stack

The method of inserting an element into a stack is called the “Push” operation. It adds a new item to the top of the stack. The top is incremented by 1, each time when an element is inserted into the stack. A stack is also called a last-in-first-out structure where the element inserted at the last is the first to be deleted. Push operation is nothing but writing values into the stack. When the stack is full, no element can be inserted and this situation is called stack overflow.

Algorithm for Push Operation

Procedure push(S,top,MAX,item)

Deletion Operations on Stack

The method of deleting an element from a stack is called “Pop” operation. It deletes the topmost elements present on the stack. The top is decremented by 1, each time when an element is deleted from the stack. A pop operation is nothing but deleting values from the stack. When the stack is empty, no element can be deleted and this situation is called stack underflow.

Algorithm for Pop Operation:

Procedure pop(S,top)

Applications of Stacks

Queue

A queue is an ordered collection of items in which items may be inserted at one end known as rear end and items may be inserted at one end known as front end. A queue is also known as FIFO (first in first out) structure because the element which is inserted first will be the first element to be deleted. Diagrammatically it is shown as,

Primitive Operations

Two basics operations performed on queues are

Queues are implemented sequentially in C using an array and two variables ‘front’ and ‘rear’ to hold the position of first and last elements in queue respectively. A queue can have 0 to MAX-1 elements where MAX is the size of an array. Initially when the queue is empty, front=rear=-1

Insertion Operation

Insertion operation performs the following actions, If rear==MAX-1 i.e., the queue is full, then prints a message that ‘Queue is full’ and no new element can be added to a queue. Otherwise, increments rear by 1 and insert a new element at rear position.

Algorithm:

Inserting elements into a queue procedure Qinsert(Q,front,rear,MAX,item)

Deletion Operation:

Deletion operations perform the following actions. If the queue is empty which is given by front =-1, then prints a message that “no deletion is possible”. Otherwise, retrieves the front element and then increments front by 1.

Algorithm

Deletion from a Queue procedure Qdelete(Q,front,rear)

Applications of Queue

Circular Queue

Circular Queue is a queue in which elements are stored in a circular manner. Circular queues are implemented using arrays. Let us consider an array CQ that contains ‘K’ elements in which CQ[1] comes after CQ[K] in the array. A liner queue can be transformed into a circular queue when the last room comes just before the first room.

In circular queue if an element is inserted when rear=K, then this element is assigned CQ[1] i.e., inserted of incrementing the rear value to K+1, the rear is reset to 1. If there is only a single element then front=rear and if that element is deleted than front=NULL and rear=NULL which indicates that queue is empty.

It is very easy to perform the insertion and deletion operation on a linear queue. At the same time, there are many drawbacks to implementing a simple queue. If an element is deleted, that empty memory location cannot be used again. Whereas in a circular queue it can be used.

It is possible to insert an element even at the beginning of the queue if that location is empty. Because of this limitation, disk space is wasted. In order to overcome these problems, a circular queue is implemented.

Priority Queue

A priority queue is a data structure similar to stacks and queue .the difference between them is that in stack and queue insertion and deletion operation are performed based on the order in which elements are inserted, but in a priority queue, the operation is performed are based on priority.

 Rules required for maintaining a priority queue

Priority queues are generally used while scheduling jobs and in simulation systems. There are two types of the priority queues.

  1. Maximum priority queue
  2. Minimum priority queue

Max Priority Queue

in a max priority queue, elements can be inserted regardless of the order, but while performing deletion only the largest element is removed.

Min Priority Queue

a min-priority queue is the same as max priority queue but the only difference is that in the main priority queue only the smallest element is removed. Operations that can be applied to max priority queue and min priority queue are,