0% found this document useful (0 votes)
39 views37 pages

Stacks

This document discusses stacks and their implementations. It defines stacks as linear data structures that follow LIFO ordering and support push and pop operations. Stacks have various applications and can be implemented using arrays or linked lists. Array implementation involves maintaining a top pointer, while linked list implementation uses nodes with next pointers. The document also discusses recursion and provides an example of a recursive factorial function.

Uploaded by

Shikhar Ashish
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)
39 views37 pages

Stacks

This document discusses stacks and their implementations. It defines stacks as linear data structures that follow LIFO ordering and support push and pop operations. Stacks have various applications and can be implemented using arrays or linked lists. Array implementation involves maintaining a top pointer, while linked list implementation uses nodes with next pointers. The document also discusses recursion and provides an example of a recursive factorial function.

Uploaded by

Shikhar Ashish
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/ 37

Stacks

Presented by:
Dr. Ishleen Kaur
Table of Contents

• Introduction to Stacks
• Array Implementation of Stacks
• Operations on Stacks
• Applications of Stacks
• Linked List Implementation of Stacks
Stacks

• It is defined as a linear structure which supports LIFO/FILO property.


• Only topmost element is accessible.
• Operations:
• Push() – to insert an element
• Pop() – to delete an element
Stack as an ADT

• When we define a stack as an ADT (or Abstract Data Type), then we


are only interested in knowing the stack operations from the user
point of view.
• Users are not interested in knowing the implementation details-
only in knowing the type of operations that can be performed.
Stacks
Array Implementation of Stacks

# define MAX 100


Struct Stack
{
int top;
int a[MAX];
}

typedef Struct Stack STK


STK *s;
Stacks

• To check if a stack is empty:


if(top == -1)

• To check if a stack is full:


if(top == MAX-1)
Push operation

void push (STK *s, int item)


{
if(is_full(s) )
{
printf(“Overflow”);
exit();
}
top++;
a[top] = item;
}
Push operation

Push 11
void push (STK *s, int item)
{
if(is_full(s) )
{
printf(“Overflow”);
exit();
}
top++;
a[top] = item;
}
Push operation

Push 6
void push (STK *s, int item)
{
if(is_full(s) )
{
printf(“Overflow”);
exit();
}
top++;
a[top] = item;
}
Pop operation

int pop (STK *s)


{
if (is_empty(s) )
{
printf(“Underflow”);
exit();
}
x= a[top]
top--;
return (x);
}
Pop operation

Pop
int pop (STK *s)
{
if (is_empty(s) )
{
printf(“Underflow”);
exit();
}
x= a[top]
top--;
return (x);
}
Copy the elements from stack S1 to stack S2 using intermediate
stack.

void stack_copy (STK *S1, STK *S2)


{
STK *temp;
int x;
create_empty_stack (temp);
while (! is_empty(S1) )
{
x=pop(S1); 3 3
push(temp, x);
} 2 2
while (! is_empty(temp)) 1 1
{
x=pop(temp); S1 S2
push (S1, x);
push (S2, x);
}
}
Initially
S1 temp Pop(S1) Push(temp,3) Pop(S1) Push(temp,2) Pop(S1) Push(temp,1) S1 temp

3 1 1
2 2 2 2 2
1 1 3 1 3 3 3
Pop(temp) Push (S2,1) Pop(temp) Push(S2,2) Pop(temp) Push(S2,3) temp S2

3 3
2 2 2 2
3 1 3 1 1 1
Applications of Stack

Expression

Prefix or Postfix or
Infix
Polish Reverse Polish
a*b
*ab ab*
Convert 3+4*5-6/3+(2-3) into postfix expression.

3+4*5-6/3+(23-)
3+(45*) - 6/3+ (23-)
3+(45*) - (63/) + (23-)
(345*+) - (63/) + (23-)
(345*+ 63/-) + (23-)
345*+63/-23-+
7 – 3 + 6 * 2 + ( 5+2) / 2
7-3+6*2+(52+)/2
7-3+(62*)+(52+)/2
7-3+(62*)+(52+2/)
(73-)+(62*)+(52+2/)
(73-62*+)+(52+2/)
73-62*+52+2/+
Convert 3+4*5-6/3+(2-3) into prefix expression.

3+4*5-6/3+(-23)
3 + (*45) – (/63) + (-23)
(+3*45) – (/63) + (-23)
(-+3*45 /63) + (-23)
+ -+3*45 /63 -23
7 – 3 + 6 * 2 + ( 5+2) / 2
7-3+6*2+(+52)/2
7-3+(*62)+(+52)/2
7-3+(*62)+(/+522)
-73+(*62)+(/+522)
(+-73*62)+(/+522)
++-73*62/+522
Evaluate 2*3+(4+5) using stack

Operator Stack
*
*

5 +
3 4 4 9 +(
2 2 6 6 6 6 15 +(+
+
Operator stack
+
( (
* * + + + +
Question: Evaluate 2*5*(3+6) using stack
Evaluate 2*5*(3+6) using stack

Operator Stack
*
*

6 *
5 3 3 9 *(
2 2 10 10 10 10 90 *(+
*
Operator stack
+
( (
* * * * * *
Evaluate 25*36+* using stack

6
5 3 3 9
2 2 10 10 10 10 90

Operator stack

* + *
Question: Evaluate 5+3*(6/2) using stack

Hint: Postfix expression: 5 3 6 2 / * +


Linked List Implementation of Stack
struct Node {
int data;
struct Node *next;
};

struct Node *top = NULL;


Push operation
void push(int value) {
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value; // assign value to the node
if (top == NULL)
newNode -> next = NULL;
else
newNode -> next = top; // Make the node as top
top = newNode; // top always points to the newly created node
}
Pop operation
int pop() {
if (top == NULL)
printf("\nStack Underflow\n");
else {
struct Node *temp = top;
int temp_data = top -> data;
top = top -> next;
free(temp);
return temp_data;
}
}
void display()
{
if (top == NULL)
printf("\nStack Underflow\n");
else {
printf("The stack is \n");
struct Node *temp;
temp = top;
while (temp != NULL)
{
printf("%d--->", temp->data);
temp = temp->next;
}
}
Recursion

• Any function which calls itself is called recursive.


• A recursive method solves a problem by calling a copy of itself to work on a smaller
problem. This is called the recursion step.
• The recursion step can result in many more such recursive calls.

Format for a recursive function:


if (test for the base case)
return some base case value
else if (test for another base case)
return some other base case value
//the recursive case
else
return (some work and then a recursive call)
Factorial function using recursion
Factorial function using recursion

int fact (int n)


{
if(n==1)
return 1;
else if(n==0)
return 1;
else
return n*fact(n-1);
}
References

1. “Data Structures: A Pseudocode Approach with C”, Richard F. Gilberg &


Behrouz A. Forouzan, 2nd edition, 2005.
2. “Data Structures and Algorithms Made Easy”, Narasimha Karumanchi, 5th
edition, 2017.

You might also like