Role of Stacks and Queues in Problem Solving
By: Zaid Shaikh
Tags: Coding, Datastrucutre, Problem Solving, Coding Interviews, Algorithms

Introduction to Stacks and Queues

Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. Queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle.

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.

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.

Analogy of Stack and Queue in Real Life

Stack :

Queue :

FIFO & LILO and LIFO & FILO Principles

Queue: First In First Out (FIFO): The first object into a queue is the first object to leave the queue, used by a queue.

Stack: Last In First Out (LIFO): The last object into a stack is the first object to leave the stack, used by a stack

OR

Stack: Last In First Out (FILO): The First object or item in a stack is the last object or item to leave the stack.

Queue: Last In First Out (LILO): The last object or item in a queue is the last object or item to leave the queue.

How Stack Solves Real World Problems ?

Expression Evaluation

Stack is used to evaluate prefix, postfix and infix expressions.

Expression Conversion

An expression can be represented in prefix, postfix or infix notation. Stack can be used to convert one form of expression to another.

Syntax Parsing

Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code.

Parenthesis Checking

Stack is used to check the proper opening and closing of parenthesis.

Backtracking

Suppose we are finding a path for solving maze problem. We choose a path and after following it we realize that it is wrong. Now we need to go back to the beginning of the path to start with new path. This can be done with the help of stack.

String Reversal

Stack is used to reverse a string. We push the characters of string one by one into stack and then pop character from stack.

Function Call

Stack is used to keep information about the active functions or subroutines in an operating system.

How Queue Solves Real World Problems ?

Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the first one coming in, also gets out first while the others wait for their turn, like in the following scenarios

Operating System

Serving requests on a single shared resource, like a printer, CPU task scheduling etc.

Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive i.e First come first served.

Call Centers

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

Demonstration of Stack Problem

Infix to Postfix ( Expression Conversion Problem) :

The complex arithmetic operations can be converted into polish notation using stacks which then can be executed in two operands and an operator form.

Infix Expression

It follows the scheme of <operand><operator><operand> i.e. an <operator> is preceded and succeeded by an <operand>. Such an expression is termed infix expression. E.g., A+B

Postfix Expression

It follows the scheme of <operand><operand><operator> i.e. an <operator> is succeeded by both the <operand>. E.g., AB+

Algorithm :

Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.

1. Push “(“onto Stack, and add “)” to the end of X.

2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.

3. If an operand is encountered, add it to Y.

4. If a left parenthesis is encountered, push it onto Stack.

5. If an operator is encountered ,then:

a. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same precedence as or higher precedence than operator.

b. Add operator to Stack.

6. If a right parenthesis is encountered ,then:

a. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is encountered.

b. Remove the left Parenthesis.

7. END.

Advantage of Postfix Expression over Infix Expression

An infix expression is difficult for the machine to know and keep track of precedence of operators. On the other hand, a postfix expression itself determines the precedence of operators (as the placement of operators in a postfix expression depends upon its precedence).Therefore, for the machine it is easier to carry out a postfix expression than an infix expression.

Let’s take an example to better understand the algorithm

Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator

Resultant Postfix Expression: ABC*DEF^/G*-H*+

Demonstration of Queue Problem

Round Robin Scheduling (CPU Task Scheduling)

Round Robin Scheduling is a scheduling algorithm used by the system to schedule CPU utilization. This is a preemptive algorithm. There exist a fixed time slice associated with each request called the quantum. The job scheduler saves the progress of the job that is being executed currently and moves to the next job present in the queue when a particular process is executed for a given time quantum.

Algorithm

· We first have a queue where the processes are arranged in first come first serve order.

· A quantum value is allocated to execute each process.

· The first process is executed until the end of the quantum value. After this, an interrupt is generated and the state is saved.

· The CPU then moves to the next process and the same method is followed.

· Same steps are repeated till all the processes are over.