0% found this document useful (0 votes)
10 views

Chapter 4

Chapter 4 covers stacks, queues, and recursion as fundamental data structures and concepts in programming. Stacks operate on a LIFO principle, while queues follow a FIFO principle, with both having specific operations for adding and removing elements. Recursion is a method where functions call themselves to solve problems, and it is widely used in applications like tree traversal, sorting algorithms, and backtracking.

Uploaded by

lap use
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Chapter 4

Chapter 4 covers stacks, queues, and recursion as fundamental data structures and concepts in programming. Stacks operate on a LIFO principle, while queues follow a FIFO principle, with both having specific operations for adding and removing elements. Recursion is a method where functions call themselves to solve problems, and it is widely used in applications like tree traversal, sorting algorithms, and backtracking.

Uploaded by

lap use
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 4 (Stacks, Queues & Recursion)

Stacks
• 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.

Queue Data Structure


• A queue is a linear data structure where elements are stored in the FIFO (First In First
Out) principle where the first element inserted would be the first element to be
accessed. 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.
Recursion
• Recursion is the process in which a function calls itself again and again. It entails
decomposing a challenging issue into more manageable issues and then solving each
one again. There must be a terminating condition to stop such recursive calls.
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).
• Syntax parsing: Stacks are used to check the validity of syntax in programming
languages and other formal languages.
• Memory management: Stacks are used to allocate and manage memory in some
operating systems and programming languages
Representation of Stacks

To implement the stack, it is required to maintain


the pointer to the top of the stack , which is the last
element to be inserted because we can access the
elements only on the top of the stack.
This strategy states that the element that is inserted last
will come out first. You can take a pile of plates kept on
top of each other as a real-life example. The plate which
we put last is on the top and since we remove the plate
that is at the top, we can say that the plate that was put
last comes out first.

PUSH Operation in Stack Structure


Push is the term used to insert an element into a stack.
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
PUSH(STACK, TOP, MAXTK, ITEM)
1. [Stack already full?] If TOP = MAXTK, then: print: OVERFLOW and Return.
2. Set TOP := TOP + 1. [Increases TOP by 1]
3. Set STACK[TOP] := ITEM. [Inserts ITEM in new TOP position]
4. Return.

POP Operation in Stack Structure


• POP operation is used to remove 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.
• This procedure deletes the top element of STACK and assigns it to the variable ITEM.
• POP(STACK, TOP, ITEM)
• 1. [Stack has an item to be removed?] If TOP = 0, then, Print: UNDERFLOW and
Return.
Set ITEM := STACK[TOP]. [Assigns TOP element to ITEM.]
• 3. set TOP := TOP-1. [Decrease TOP by 1]
• 4. Return.
Arithmetic Expression of Stack
• An Arithmetic Expression is a combination of operands and Arithmetic operators,
such as addition, subtraction, and so on. These combinations of operands and
operators should be mathematically meaningful, otherwise, they can not be considered
as an Arithmetic expression in C.
• The stack organization is very effective in evaluating arithmetic expressions.
Expressions are usually represented in what is known as Infix notation, in which each
operator is written between two operands (i.e., A + B). With this notation, we must
distinguish between ( A + B )*C and A + ( B * C ) by using either parentheses or some
operator-precedence convention.
1.Polish notation (prefix notation) –
It refers to the notation in which the operator is placed before its two operands. Here no
parentheses are required, i.e., +AB.
2. Reverse Polish notation(postfix notation) –
It refers to the analogous notation in which the operator is placed after its two operands.
Again, no parentheses is required in Reverse Polish notation, i.e., AB+.
The procedure for getting the result is:
1. Convert the expression in Reverse Polish notation( post-fix notation).
2. Push the operands into the stack in the order they appear.
3. When any operator encounters then pop two topmost operands for executing the
operation.
4. After execution push the result obtained into the stack.
5. After the complete execution of expression, the final result remains on the top of
the stack.
For example:
Infix notation: (2+4) * (4+6)
notation: 2 4 + 4 6 + *
Result: 60

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.

Insertion of Elements to the Queue


• This procedure inserts an element ITEM into a queue.
• QINSERT(QUEUE, N, FRONT, REAR, ITEM)
• 1. [Queue already filled?] If FRONT = 1 and REAR = N or if FRONT = REAR+1, then
Write := OVERFLOW and Return.
• 2. [Final new value is REAR]
If FRONT := NULL then [Queue initially empty] Set FRONT := 1 and REAR := 1.
Else if REAR = N then set REAR := 1
Else set RAER := REAR + 1 [End of If structure]
3. Set QUEUE[RAER] := ITEM. [This inserts new element]
4. Return.
Deletion of Elements to the Queue
• This procedure deletes an element from a queue and assigns it to the variable ITEM.
• QDELETE(QUEUE, N, FRONT, REAR, ITEM)
• 1. [Queue already empty?] If FRONT := NULL, then, Write UNDEDRFLOW and
Return.
• 2. Set ITEM := QUEUE[FRONT]
• 3. [Find new value of FRONT]
If FRONT = REAR then [queue has only one element to start]
Set FRONT := NULL and REAR := NULL
Else If FRONT = N then Set FRONT := 1
Else Set FRONT := FRONT + 1
[End of If Structure]
5. Return.

Priority Queues
• A priority queue is a collection of elements such that each element has been assigned a
priority and such that the order in which elements are deleted and processed comes from
the following rules.
Need of Recursion
• Performing the same operations multiple times with different inputs.
• In every step, we try smaller inputs to make the problem smaller.
• Base condition is needed to stop the recursion otherwise infinite loop will occur.
Applications of Recursion
• Tree and graph traversal: Recursion is frequently used for traversing and searching
data structures such as trees and graphs. Recursive algorithms can be used to explore all
the nodes or vertices of a tree or graph in a systematic way.
• Sorting algorithms: Recursive algorithms are also used in sorting algorithms such as
quicksort and merge sort. These algorithms use recursion to divide the data into smaller
subarrays or sublists, sort them, and then merge them back together.
• Divide-and-conquer algorithms: Many algorithms that use a divide-and-conquer
approach, such as the binary search algorithm, use recursion to break down the problem
into smaller subproblems.
• Fractal generation: Fractal shapes and patterns can be generated using recursive
algorithms. For example, the Mandelbrot set is generated by repeatedly applying a
recursive formula to complex numbers.
• Backtracking algorithms: Backtracking algorithms are used to solve problems that
involve making a sequence of decisions, where each decision depends on the previous
ones. These algorithms can be implemented using recursion to explore all possible paths
and backtrack when a solution is not found.
• Memoization: Memoization is a technique that involves storing the results of expensive
function calls and returning the cached result when the same inputs occur again.
Memoization can be implemented using recursive functions to compute and cache the
results of subproblems.

You might also like