Unit 2
Unit 2
Venkata Ramana S
Assistant Professor
CSE,MRECW
UNIT-II Stacks and Queues using C& Python: ADT Stack and its operations:
Algorithms and their complexity analysis, Applications of Stacks: Expression
Conversion and evaluation – corresponding algorithms and complexity analysis.
ADT Queue: Types of Queue: Simple Queue, Circular Queue, Priority Queue.
Double ended Queue and Operations on each types of Queues and Algorithms.
Applications of queues.
What is a Stack ?
A Stack is a linear data structure that follows the LIFO (Last-
In-First-Out) principle. Stack has one end, whereas the Queue
has two ends (front and rear).
It contains only one pointer top pointer pointing to the
topmost element of the stack.
Whenever an element is added in the stack, it is added on the
top of the stack, and the element can be deleted only from the
stack.
In other words, a stack can be defined as a container in
which insertion and deletion can be done from the one
end known as the top of the stack.
4
Applications of Stacks
Some Applications of Stacks:
Balancing of symbols: Stack is used for balancing a symbol.
String reversal: Stack is also used for reversing a string. For example, we
want to reverse a “RamSita" string, so we can achieve this with the help of a
stack.
First, we push all the characters of the string in a stack until we reach the
null character.
After pushing all the characters, we start taking out the character one by
one until we reach the bottom of the stack.
UNDO/REDO: One of the most common uses of Stack in programming is to
implement undo and redo functionality in text editors and other applications.
When the user types text, each keystroke is pushed onto a stack. When the
user hits undo, the most recently pushed text is popped off the Stack and
removed from display. Redo pops the item back onto the Stack. As Stack
maintains LIFO order, the text removed last is undone first. Visual editors
like Photoshop also use it for undo/redo image edits.
Recursion: The recursion means that the function is calling itself again. To
maintain the previous states, the compiler creates a system stack in which
all the previous records of the function are maintained.
8
Applications of Stacks
Backtracking: Suppose we have to create a path to solve a maze problem. If we
are moving in a particular path, and we realize that we come on the wrong way.
In order to come at the beginning of the path to create a new path, we have to
use the stack data structure.
Expression conversion: Stack can also be used for expression conversion. This
is one of the most important applications of stack. The list of the expression
conversion is given below:
I. Infix to prefix
II. Infix to postfix
III.Prefix to infix
IV. Prefix to postfix
V. Postfix to infix
Memory management: The stack manages the memory. The memory is assigned
in the contiguous memory blocks. The memory is known as stack memory as all
the variables are assigned in a function call stack memory. The memory size
assigned to the program is known to the compiler. When the function is
created, all its variables are assigned in the stack memory. When the function
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below
figure there are five memory blocks in the stack; therefore, the size
of the stack is 5.
Suppose we want to store the elements in a stack and let's assume
that stack is empty. We have taken the stack of size 5 as shown
below in which we are pushing the elements one by one until the
Since our stack is full as the size of the stack is
stack becomes full.
5. In the above cases, we can observe that it
goes from the top to the bottom when we were
entering the new element in the stack. The
stack gets filled up from the bottom to the top.
Applications of Queue
There are various applications of queues discussed as below.
1. Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg. pipes,
file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
5. Queues are used in operating systems for handling interrupts.
Operations associated with queues
The main operations associated with
queues are:
o Enqueue: Add an element to the back of the queue.
o Dequeue: Remove an element from the front of the
queue.
o IsEmpty: Check if the queue is empty.
o IsFull: Check if the queue is full.
o Peek: Get the value of the front element without
removing it.
Algorithms:
Queues are based on the FIFO algorithm. The steps are:
o Enqueue: Add an item to the back of the queue, increase the rear
pointer
o Dequeue: Remove the item from the front of the queue, increase
the front pointer
o Check if the rear pointer has reached the end of the queue
capacity
o Check if the front pointer equals the rear (empty queue)
Data Structures & Algorithms
Advantages of Queues Disadvantages of Queues
1. Maintains Order: 1. Limited Access:
o You can only access the front and rear elements, not
o Elements are processed in the order
the middle ones.
they are added (First In, First Out). 2. Memory Usage:
2. Fairness: o Dynamic queues (like linked lists) use extra memory
o Ensures every element gets processed for pointers.
Time Complexity
Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Data Structures & Algorithms
Data Structures & Algorithms
The major drawback of using a linear Queue is that insertion is done only from
the rear end.
If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue. In this case, the
linear Queue shows the overflow condition as the rear is pointing to the last
element of the Queue.
Data Structures & Algorithms
Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the
linear Queue except that the last element of the queue is connected to the
first element.
It is also known as Ring Buffer, as all the ends are connected to another end.
The representation of circular queue is shown in the below image -
The drawback that occurs in a linear queue is overcome by using the circular
queue.
If the empty space is available in a circular queue, the new element can be
added in an empty space by simply incrementing the value of rear.
The main advantage of using the circular queue is better memory utilization.
Data Structures & Algorithms
Just like Stack, in case of a Queue too, we know exactly, on which position new element will be
added and from where an element will be removed, hence both these operations requires a
single step.
Enqueue: O(1)
Dequeue: O(1)
Size: O(1)