Stack and Queue Data Structures Explained
Stack and Queue Data Structures Explained
Introduction
Stack:
In the pushdown stacks only two operations are allowed: push the item into the stack and pop the item
out of the stack. A stack is a limited access data structure - elements can be added and removed from
the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top.
A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add
a new book on the top.
Queue:
An excellent example of a queue is a line of students in the food court of the UC. New additions to a
line made to the back of the queue, while removal (or serving) happens in the front. In the queue only
two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of
the queue, dequeue means removing the front item. The picture demonstrates the FIFO access. The
difference between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.
A real-world stack allows operations at one end only. For example, we can place or remove a card or
plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only.
At any given time, we can only access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which
is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called
PUSH operation and removal operation is called POP operation.
Stack Representation
The following diagram depicts a stack and its operations −
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either
be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack
using arrays, which makes it a fixed size stack implementation.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from
these basic stuffs, a stack is used for the following two primary operations −
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the
stack. When data is PUSHed onto stack.
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the
following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always
represents the top of the stack, hence named top. The top pointer provides top value of the stack
without removing it.
First, we should learn about procedures to support stack functions –
peek()
Algorithm of peek() function −
begin procedure peek
return stack[top]
end procedure
int peek() {
return stack[top];
}
isfull()
Algorithm of isfull() function −
if top equals to
MAXSIZE return true
else
return false
endif
end procedure
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
end procedure
Implementation of isempty() function in C programming language is slightly different. We initialize
top at -1, as the index in array starts from 0. So we check if the top is below zero or
-1 to determine if the stack is empty. Here's the code −
Example
bool isempty() {
if(top == -1)
return true;
else
return false;
}
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.
if stack is
full return
null
endif
top ← top + 1
stack[top] ← data
end procedure
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array
implementation of pop() operation, the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
if stack is
empty return
null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Implementation of this algorithm in C, is as follows −
Example
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
Queue ADT:
Queue is an abstract data structure, somewhat like Stacks. Unlike stacks, a queue is open at both its
ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue).
Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits
first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
Queue Representation
As we now understand that in queue, we access both ends for different reasons. The following
diagram given below tries to explain queue representation as data structure −
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For
the sake of simplicity, we shall implement queues using one-dimensional array.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then completely
erasing it from the memory. Here we shall try to understand the basic operations associated with
queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient. These
are −
peek() − Gets the element at the front of the queue without removing it.
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or
storing) data in the queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek()
This function helps to see the data at the front of the queue. The algorithm of peek() function is as
follows −
Algorithm
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function in C programming language −
Example
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear pointer to
reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular
linked-list, the algorithm will differ. Algorithm of isfull() function −
Algorithm
end procedure
Implementation of isfull() function in C programming language −
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations
are comparatively difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − return success.
Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen
situations.
Algorithm for enqueue operation
procedure enqueue(data)
rear ← rear + 1
queue[rear] ← data
return true
end procedure
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing and
remove the data after access. The following steps are taken to perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
return data;
}
Expression Parsing
The way to write arithmetic expression is known as a notation. An arithmetic expression can be
written in three different but equivalent notations, i.e., without changing the essence or output of an
expression. These notations are −
Infix Notation
As multiplication operation has precedence over addition, b * c will be evaluated first. A table of
operator precedence is provided later.
Associativity
Associativity describes the rule where operators with the same precedence appear in an expression.
For example, in expression a + b − c, both + and – have the same precedence, then which part of the
expression will be evaluated first, is determined by associativity of those operators. Here, both + and
− are left associative, so the expression will be evaluated as (a + b) − c.
Precedence and associativity determine the order of evaluation of an expression. Following is an
operator precedence and associativity table (highest to lowest) −
The above table shows the default behavior of operators. At any point of time in expression
evaluation, the order can be altered by using parenthesis. For example −
In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence over
addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.
The operations are performed based on FIFO (First In First Out) principle. It is also called ‘Ring
Buffer’.
In a normal Queue, we can insert elements until queue becomes full. But once queue
becomes full, we cannot insert the next element even if there is a space in front of queue.
enQueue(value) This function is used to insert an element into the circular queue.
In a circular queue, the new element is always inserted at Rear position.
Applications:
Application of Stack:
Expression Evaluation:
While reading the expression from left to right, push the element in the stack if it is an operand. Pop the
two operands from the stack, if the element is an operator and then evaluate it. Push back the result of
the evaluation. Repeat it till the end of the expression.
Expression Conversion:
One of the applications of Stack is in the conversion of arithmetic expressions in high-level programming
languages into machine readable form. An expression can be represented in infix, postfix and prefix
and stack proves to be useful while converting one form to another.
Syntax Parsing:
Conversion from one form of the expression to another form needs a stack. Many compilers use a stack
for parsing the syntax of expressions, program blocks etc. before translating into low level code.
Parenthesis Checking:
One of the most important applications of stacks is to check if the parentheses are balanced in each
Expression. The compiler generates an error if the parentheses are not matched.
String Reversal:
Reversing string is an operation of Stack by using it we can reverse any string.
Function Call:
The function call stack (often referred to just as the call stack or the stack) is responsible for
maintaining the local variables and parameters during function execution.
Expression Evaluation
Evaluate an expression represented by a String. The expression can contain parentheses, you can assume
parentheses are well-matched. For simplicity, you can assume only binary operations allowed are +, -,
*, and /. Arithmetic Expressions can be written in one of three forms:
Infix Notation: Operators are written between the operands they operate on, e.g., 3 +
4.
Prefix Notation: Operators are written before the operands, e.g., + 3 4.
Postfix Notation: Operators are written after operands.
Infix Expressions are harder for Computers to evaluate because of the additional work needed to
decide precedence. Infix notation is how expressions are written and recognized by
humans and, generally, input to programs. Given that they are harder to evaluate, they are
generally converted to one of the two remaining forms.
This algorithm takes as input an Infix Expression and produces a queue that has this expression
converted to postfix notation. The same algorithm can be modified so that it outputs the
result of the evaluation of expression instead of a queue. The trick is using two stacks instead
of one, one for operands, and one for operators.
Consider the following infix expression a convert into reverse polish notation using stack.
i) Postfix Expression:
Priority Queue
A priority queue is a type of queue that arranges elements based on their priority values. Elements
with higher priority values are typically retrieved before elements with lower priority values.
In a priority queue, each element has a priority value associated with it. When you add an element to
the queue, it is inserted in a position based on its priority value. For example, if you add an element
with a high priority value to a priority queue, it may be inserted near the front of the queue, while
an element with a low priority value may be inserted near the back.
There are several ways to implement a priority queue, including using an array, linked list, heap, or
binary search tree. Each method has its own advantages and disadvantages, and the best choice will
depend on the specific needs of your application.
Priority queues are often used in real-time systems, where the order in which elements are processed
can have significant consequences. They are also used in algorithms to improve their efficiencies,
such as Dijkstra’s algorithm for finding the shortest path in a graph and the A* search algorithm for
pathfinding.
Binary Heap is generally preferred for priority queue implementation because heaps provide
better performance compared to arrays or LinkedList. Considering the properties of a heap, The
entry with the largest key is on the top and can be removed immediately. It will, however, take
time O(log n) to restore the heap property for the remaining keys. However if another entry is to be
inserted immediately, then some of this time may be combined with the O(log n) time needed to insert
the new entry. Thus, the representation of a priority queue as a heap proves advantageous for large n,
since it is represented efficiently in contiguous storage and is guaranteed to require only logarithmic
time for both insertions and deletions. Operations on Binary Heap are as follows:
insert(p): Inserts a new element with priority p.
extractMax(): Extracts an element with maximum priority.
remove(i): Removes an element pointed by an iterator i.
getMax(): Returns an element with maximum priority.
changePriority(i, p): Changes the priority of an element pointed by i to p.
A Self-Balancing Binary Search Tree like AVL Tree, Red-Black Tree, etc. can also be used to
implement a priority queue. Operations like peek(), insert() and delete() can be performed using
BST.