Stack
Stack
Stack
C top
B top B B top
A A A A top
A top
Stack Operations
• In stack data structure mainly performs two operations
• If the stack is empty, then print error of underflow and exit the
program.
• If the stack is not empty, then print the element at the top and
decrement the top.
Insertion of any item –push().
• Top is incremented by 1
Function for push() operation:
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
Deletion of any item-pop()
• Top is decremented by 1
Function for pop() operation
if(top==-1)
printf("Stack Underflow:");
else
{
item=stack[top];
} top=top-1;
Department of ISE
printf("\nThe poped element: %d\t",item);
}
function to check if the stack is full
printf(“Stack is full\n”);
}
else
{
printf(“Stack is not full\n”);
}
}
function to check if the stack is empty
printf(“Stack is empty\n”);
}
else
{
printf(“Stack is not empty\n”);
}
}
function for peek()
int peek()
{
if (top == -1)
{
printf("Underflow");
return 0;
}
else
{ In the above code, firstly we are checking whether the stack is empty
return stack [top]; or not. If the stack is empty, the program will print underflow and will
return zero. Otherwise, it will return the top element of the stack
}}
Function for display the elements in stack
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("Stack is...");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
IMPLEMENTATION OF STACK USING ARRAYS //lab pgm :8
switch(choice)
#include<stdlib.h> {
#include<stdio.h> case 1:
#include<string.h> push();
#define max_size 5 break;
int stack[max_size],top = -1; case 2:
void push(); pop();
void pop(); break;
void display(); case 3:
void peek(); peek();
break;
int main() case 4:
{ display();
int choice; break;
while(choice) case 5:
{ exit(0);
printf("\n\n--------STACK OPERATIONS-----------\n"); break;
printf("1.Push\n"); default:
printf("2.Pop\n"); printf("\nInvalid choice:\n");
printf("3.Peek\n"); break;
printf("4.Display\n"); }
printf("5.Exit\n"); }
printf("\nEnter your choice:\t"); return 0;
scanf("%d",&choice); }
void push() //Inserting element into the stack
{
int item,n; void pop() //deleting an element from the stack
if(top==(max_size-1)) {
{ int item;
printf("\nStack Overflow:"); if(top==-1)
} {
else printf("Stack Underflow:");
{ }
printf("Enter the element to be inserted:\t"); else
scanf("%d",&item); {
top=top+1; item=stack[top];
stack[top]=item; top=top-1;
} printf("\nThe poped element: %d\t",item);
}
}
}
int peek()
{
int item; void display()
if(top==-1) {
{ int i;
printf("Stack Underflow:"); if(top==-1)
} {
else printf("\nStack is Empty:");
{ }
return stack[top]; else
{
} printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
} {
printf("%d\n",stack[i]);
}
}
}
Applications of Stack:
❖ Factorial
Recursion :
❖ Recursion is implemented using stack because activation records are to be stored in
LIFO order i.e. last in first out. An activation record of a function call contains three
parts: first is arguments, return address and local variables of the function.
❖ Recursion has many, many applications. In this module, we'll see how to use recursion
to compute the factorial function, to determine whether a word is a palindrome, to
compute powers of a number, to draw a type of fractal, and to solve the ancient
Towers of Hanoi problem
How recursion works?
void recurse()
... .. ...
recurse();
... .. ...
}
int main()
{
.. .. ...
recurse();
... .. ...
}
Advantages and Disadvantages of
Recursion
❖ Fibonacci search divides the array into two parts that have
sizes that are consecutive Fibonacci numbers. On average,
this leads to about 4% more comparisons to be executed,
but it has the advantage that one only needs addition and
subtraction to calculate the indices of the accessed array
elements, while classical binary search needs bit-shift
division or multiplication, operations that were less common
at the time Fibonacci search was first published. Fibonacci
search has an average- and worst-case complexity of O(log
n).
#include <stdio.h>
if(n == 0) {
{ int n = 5;
return 0; int i;
}
else if(n == 1) printf("Fibbonacci of %d: " , n);
{
return 1; for(i = 0;i<n;i++) {
} printf("%d ",fibbonacci(i));
Else }
{ }
#include <stdio.h>
int main()
int factorial(int n) {
{
//base case
int n = 5;
if(n == 0) {
int i;
return 1;
} else {
printf("Factorial of %d: %d\n" , n , factorial(n));
return n * factorial(n-1);
}
}
}
}
Output
Factorial of 5: 120
Example :3 Tower of Hanoi
Algorithm:
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
IMPORTANT QUESTIONS MODULE -4