0% found this document useful (0 votes)
13 views23 pages

Stack

Uploaded by

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

Stack

Uploaded by

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

Stack

• A stack is a linear data structure that follows the Last In,


First Out (LIFO) principle. This means that the last
element added to the stack will be the first one to be
removed.
Stack
• Key Characteristics of a Stack:
• LIFO (Last In, First Out): The most recently added item is the first to
be removed.
• Operations:
• Push: Add an element to the top of the stack.
• Pop: Remove and return the element from the top of the stack.
• Peek (Top): Return the top element without removing it.
• isEmpty: Check if the stack is empty.
• isFull: Check if the stack is full (in case of an array-based stack with fixed size).
Stack
• Applications of Stack:
• Expression Evaluation: Stacks are used to evaluate expressions
(postfix or prefix).
• Backtracking: Helps in algorithms like Depth-First Search (DFS)
for traversing graphs or trees.
• Function Call Management: The call stack in programming is a
stack data structure.
• Undo/Redo Operations: Used in applications where you need to
undo and redo actions.
Stack
• Stack Operations:
1.Push Operation:
1.Objective: Insert an element at the top of the stack.
2.Algorithm:
1.Check if the stack is full. If it is, return an overflow message.
2.If not, increment the top pointer and add the new element at the
position of top
Stack
Push(stack, top, maxSize, element) {
if (top == maxSize - 1) {
print "Stack Overflow";
} else {
top = top + 1;
stack[top] = element;
}
}
Stack
• Pop Operation:
• Objective: Remove and return the top element of the
stack.
• Algorithm:
1. Check if the stack is empty. If it is, return an underflow message.
2. If not, return the element at the top and decrement the top
pointer
Stack
• Pop(stack, top) {
• if (top == -1) {
• print "Stack Underflow";
• } else {
• element = stack[top];
• top = top - 1;
• return element;
• }
• }
Stack
• Peek Operation:
• Objective: Return the top element without removing it
from the stack.
• Algorithm:
• If the stack is empty, return an underflow message.
• Otherwise, return the element at the position top
Stack
Peek(stack, top) {
if (top == -1) {
print "Stack is empty";
} else {
return stack[top];
}
}
Stack
• isEmpty Operation:
• Objective: Check if the stack is empty.
• Algorithm
1. If top == -1, return true (stack is empty).
2. Otherwise, return false.
Stack
isEmpty(top) {
if (top == -1) {
return true;
} else {
return false;
}
}
Stack
• isFull Operation (Array-based stack):
• Objective: Check if the stack is full.
• Algorithm:
1. If top == maxSize - 1, return true (stack is full).
2. Otherwise, return false
Recursion
• Recursion is a technique where a function calls itself in order to
solve a problem. It is particularly useful for problems that can be
broken down into smaller, similar subproblems. The main
components of a recursive function are:
1.Base Case: This is the condition under which the recursion ends. It
prevents infinite recursion and is essential for the function to
eventually stop calling itself.
2.Recursive Case: This is the part of the function where the recursive
call is made. The function performs a task and then calls itself with a
modified argument.
Recursion
• How Recursion Works
When a recursive function is called, a new instance of that
function is created in memory. Each instance has its own
variables and execution context. The function continues to
call itself until it reaches the base case, at which point it
returns a value and unwinds back through the chain of calls,
eventually returning a final result.
Recursion
• How Recursion Works
When a recursive function is called, a new instance of that
function is created in memory. Each instance has its own
variables and execution context. The function continues to
call itself until it reaches the base case, at which point it
returns a value and unwinds back through the chain of calls,
eventually returning a final result.
Recursion
• Key Concepts of Recursion
1.Function Calls: Each recursive call creates a new frame in the call stack.
2.Stack Overflow: If the recursion goes too deep (too many nested calls), it
can lead to a stack overflow error.
3.Efficiency: Some recursive solutions may be less efficient than their
iterative counterparts, especially if they involve repeated calculations (e.g.,
Fibonacci numbers).
4.Memoization: This technique involves storing the results of expensive
function calls and reusing them when the same inputs occur again, which
can improve efficiency
Recursion
• Recursion can be classified into
several types based on the structure of
the recursive calls. Here are the main
types of recursion in C++.
Recursion
1. Direct Recursion
A function calls itself directly.
Example:
void func() {
// Base condition
if (condition) return;
func(); // Directly calls itself
}
Recursion
• 2. Indirect Recursion
• Two or more functions call each other in a cycle.
• Example:
void funcA() {
// Base condition
if (condition) return;
funcB(); // funcA calls funcB
}

void funcB() {
// Base condition
if (condition) return;
funcA(); // funcB calls funcA
}
Recursion
• 3. Tail Recursion
• The recursive call is the last operation in the function. After the
call, nothing is done.
• Example:
void tailRecursion(int n) {
if (n == 0) return;
tailRecursion(n - 1); // Recursive call is the last statement
}
Recursion
• 4. Non-Tail Recursion
• The recursive call is not the last operation, and some computation
happens after the recursive call.
• Example
int nonTailRecursion(int n) {
if (n == 0) return 1;
return n * nonTailRecursion(n - 1); // Further computation
after the recursive call
}
Recursion
• 5. Tree Recursion
• The function makes more than one recursive call in its body, leading
to a tree-like structure of function calls.
• Example:
• int treeRecursion(int n) {
• if (n <= 1) return n;
• return treeRecursion(n - 1) + treeRecursion(n - 2); // Multiple
recursive calls
• }
Recursion
• 6. Head Recursion
• The recursive call is the first statement in the function, and any
computation happens after the recursive call.
• Example:
void headRecursion(int n) {
if (n == 0) return;
headRecursion(n - 1); // Recursive call is the first statement
// Some computation after the recursive call
}

You might also like