0% found this document useful (0 votes)
4 views9 pages

DSA UNIT 2 NOTES

The document provides an overview of data structures, specifically focusing on stacks and queues. It explains the definitions, types, basic operations, advantages, and disadvantages of stacks, as well as the operations and algorithms related to queues. Additionally, it discusses infix and postfix expressions, including an algorithm for converting infix to postfix notation.

Uploaded by

skamalraj2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

DSA UNIT 2 NOTES

The document provides an overview of data structures, specifically focusing on stacks and queues. It explains the definitions, types, basic operations, advantages, and disadvantages of stacks, as well as the operations and algorithms related to queues. Additionally, it discusses infix and postfix expressions, including an algorithm for converting infix to postfix notation.

Uploaded by

skamalraj2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA STRUCTURES AND ALGORITHMS

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.

Basic Operations on Stack:


In order to make manipulations in a stack, there are certain operations provided to us.
● push() to insert an element into the stack
● pop() to remove an element from the stack
● top() Returns the top element of the stack.
● isEmpty() returns true if stack is empty else false.
● isFull() returns true if the stack is full else false.
To implement stack, we need to maintain reference to the top item.

Push Operation on Stack


Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
Algorithm for Push Operation:
● Before pushing the element to the stack, we check if the stack is full .
● If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the
element to the stack.
● Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is inserted
at top position .
● The elements can be pushed into the stack till we reach the capacity of the stack.
Pop Operation in Stack
Removes an item from the stack. The items are popped in the reversed order in which they are
pushed. If the stack is empty, then it is said to be an Underflow condition.
Algorithm for Pop Operation:
● Before popping the element from the stack, we check if the stack is empty .
● If the stack is empty (top == -1), then Stack Underflows and we cannot remove any element
from the stack.
● Otherwise, we store the value at top, decrement the value of top by 1 (top = top – 1) and
return the stored top value.

Top or Peek Operation on Stack


Returns the top element of the stack.
Algorithm for Top Operation:
● Before returning the top element from the stack, we check if the stack is empty.
● If the stack is empty (top == -1), we simply print “Stack is empty”.
● Otherwise, we return the element stored at index = top
Applications of Stacks:
● Function calls: Stacks are used to keep track of the return addresses of function calls,
allowing the program to return to the correct location after a function has finished executing.
● Recursion: Stacks are used to store the local variables and return addresses of recursive
function calls, allowing the program to keep track of the current state of the recursion.
● Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse
Polish Notation).
● Memory management: Stacks are used to allocate and manage memory in some operating
systems and programming languages.
Advantages of Stacks:
● Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable
for a wide range of applications.
● Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)),
providing efficient access to data.
● Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element
added to the stack is the first one removed. This behavior is useful in many scenarios, such as
function calls and expression evaluation.
● Limited memory usage: Stacks only need to store the elements that have been pushed onto
them, making them memory-efficient compared to other data structures.
Disadvantages of Stacks:
● Limited access: Elements in a stack can only be accessed from the top, making it difficult to
retrieve or modify elements in the middle of the stack.
● Potential for overflow: If more elements are pushed onto a stack than it can hold, an
overflow error will occur, resulting in a loss of data.
● Not suitable for random access: Stacks do not allow for random access to elements, making
them unsuitable for applications where elements need to be accessed in a specific order.
● Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of
elements that need to be stored is unknown or highly variable.

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.

Basic Operations on Queue

Some of the basic operations for Queue in Data Structure are:

● enqueue() – Insertion of elements to the queue.

● dequeue() – Removal of elements from the queue.

● 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.

● isFull() – Validates if the queue is full.

● isEmpty() – Checks if the queue is empty.

● size() – This operation returns the size of the queue i.e. the total number of elements it
contains.

Queue Insertion Operation: Enqueue()

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

2. Check if the queue is full.

3. If the queue is full, produce overflow error and exit.

4. If the queue is not full, increment rear pointer to point

the next empty space.

5. Add data element to the queue location, where the rear

is pointing.

6. return success.

7. END

Example:

queue = []

# Enqueue

queue.append('A')

queue.append('B')

queue.append('C')

print("Queue: ", queue)

Queue Deletion Operation: dequeue()

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.

3. If the queue is empty, produce underflow error and exit.

4. If the queue is not empty, access the data where front

is pointing.

5. Increment front pointer to point to the next available

data element.

6. Return success.

7. END

Example

# Dequeue

element = queue.pop(0)

print("Dequeue: ", element)

Queue - The peek() Operation

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

2. Return the element at the front of the queue

3. END

Example:

# Peek

frontElement = queue[0]

print("Peek: ", frontElement)


Queue - The isEmpty() operation

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

2. If the count of queue elements equals zero, return true

3. Otherwise, return false

4. END

Example:

# isEmpty

isEmpty = not bool(queue)

print("isEmpty: ", isEmpty)

Queue - The isFull() Operation

The isFull() operation verifies whether the stack is full.

Algorithm

1. START

2. If the count of queue elements equals the queue size,

return true

3. Otherwise, return false

4. END

Example:

# Size

print("Size: ", len(queue))


What is infix expression?

● The traditional method of writing mathematical expressions is called infix expressions.

● It is of the form <operand><operator><operand>.

● 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.

What is postfix expression?

● The postfix expression as the name suggests has the operator placed right after the two
operands.

● It is of the form <operand><operand><operator>

● E.g. PQ-C/, here – operation is done on P and Q and then / is applied on C and the
previous result.

● A postfix expression is a parenthesis-free expression. For evaluation, we evaluate it from


left to right.

ALGORITHM:

To convert Infix expression to Postfix

1. Scan the infix expression from left to right.

2. If the scanned character is an operand, Print it.

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.

4. If the scanned character is an ‘(‘, push it into the stack.

5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.

6. Repeat steps 2-5 until the entire infix expression is scanned.

7. Print the output.

8. Pop and print the output from the stack until it is not empty.

You might also like