DSA UNIT 2 NOTES
DSA UNIT 2 NOTES
UNIT 2
STACK
A Stack is a linear data structure that follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies
that the element that is inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.
It behaves like a stack of plates, where the last plate added is the first one to be removed. Think
of it this way:
● Pushing an element onto the stack is like adding a new plate on top.
● Popping an element removes the top plate from the stack.
Types of Stack:
● Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size and cannot grow
or shrink dynamically. If the stack is full and an attempt is made to add an element to it, an
overflow error occurs. If the stack is empty and an attempt is made to remove an element
from it, an underflow error occurs.
● Dynamic Size Stack : A dynamic size stack can grow or shrink dynamically. When the stack
is full, it automatically increases its size to accommodate the new element, and when the
stack is empty, it decreases its size. This type of stack is implemented using a linked list, as it
allows for easy resizing of the stack.
QUEUE
Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first
element inserted is the first to be popped out. A queue is an Abstract Data Type (ADT) similar to
stack, the thing that makes queue different from stack is that a queue is open at both its ends. The
data is inserted into the queue through one end and deleted from it using the other end. Queue is
very frequently used in most programming languages.
● getFront()- Acquires the data element available at the front node of the queue without
deleting it.
● getRear() – This operation returns the element at the rear end without removing it.
● size() – This operation returns the size of the queue i.e. the total number of elements it
contains.
The enqueue() is a data manipulation operation that is used to insert elements into the stack. The
following algorithm describes the enqueue() operation in a simpler way.
Algorithm
1. START
is pointing.
6. return success.
7. END
Example:
queue = []
# Enqueue
queue.append('A')
queue.append('B')
queue.append('C')
The dequeue() is a data manipulation operation that is used to remove elements from the stack.
The following algorithm describes the dequeue() operation in a simpler way.
Algorithm
1. START
2. Check if the queue is empty.
is pointing.
data element.
6. Return success.
7. END
Example
# Dequeue
element = queue.pop(0)
The peek() is an operation which is used to retrieve the frontmost element in the queue, without
deleting it. This operation is used to check the status of the queue with the help of the pointer.
Algorithm
1. START
3. END
Example:
# Peek
frontElement = queue[0]
The isEmpty() operation verifies whether the stack is empty. This operation is used to check the
status of the stack with the help of top pointer.
Algorithm
1. START
4. END
Example:
# isEmpty
Algorithm
1. START
return true
4. END
Example:
# Size
● As the name suggests, here the operator is fixed inside between the operands. e.g. A+B
here the plus operator is placed inside between the two operators, (A*B)/Q.
● The postfix expression as the name suggests has the operator placed right after the two
operands.
● E.g. PQ-C/, here – operation is done on P and Q and then / is applied on C and the
previous result.
ALGORITHM:
3. Else,
● If the precedence of the scanned operator is greater than the precedence of the operator in
the stack or the stack is empty or the stack contains a ‘(‘, push the character into the
stack.
● Else, Pop all the operators from the stack which are greater than or equal to in precedence
than that of the scanned operator. After doing that Push the scanned operator to the stack.
5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.
8. Pop and print the output from the stack until it is not empty.