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

Stack Data Structure (1)

Stack

Uploaded by

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

Stack Data Structure (1)

Stack

Uploaded by

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

Stack

Data Structure

2024
1 What is Stack 5 Applications of Stack

Expressions Conversion

Outline 2 Stack Operations


Postfix Evaluation

Stack Implementation using


3
Linked List

4 Stack Implemetation
Using Array
What is Stack?
Stack data structure is a linear data structure
that accompanies a principle known as LIFO
(Last In First Out) or FILO (First In Last Out).

A stack is a list with the restriction that insertions


and deletions can be performed in only one
position, namely, the end of the list, called the
top.
Real Life Example

Stack of plates Stack of books Stack of Chairs


Stack Operations
Push operation

● Add an element to the top of the stack


Stack Operations
Stack Operations
Pop operation

● Remove element from the top of the stack


Stack Operations
Stack Operations
Top operation
Top
● returning the top element of a stack.

Is empty()

● Check if the list is empty or not.

● If it’s empty return True else False.

Is Full()

● Check if the list is Full or not.

● If it’s full return True else False.


Stack Operations

Cases to Consider
Stack Overflow: Occurs when pushing into a full stack. (array implementation)

Stack Underflow: Happens when popping from an empty stack.


Stack Implementation
Can be implemented using both
dynamic data structure (Linked List)
static data structure (Array)
Stack Implementation
(Linked List)
Implementing stack using linked list
Stack Implementation
(Linked List)
Push operation

● Initialise a node

● Update the value of that node by data i.e. node.value = data


● Now link this node to the top of the linked list i.e. node.next = head

● And update top pointer to the current node i.e. head = node
Stack Implementation
(Linked List)
Push operation
Stack Implementation
(Linked List)
Pop operation

● First Check whether there is any node present in the linked list or not, if not then return

● Otherwise make pointer let say temp to the top node and move forward the top node by 1
step

● Now free this temp node


Stack Implementation
(Linked List)
Pop operation

Removed node
Stack Implementation
(Linked List)
Top operation

Removed node
Stack Implementation
(Linked List)
Top operation

● Check if there is any node present or not, if not then return. Otherwise return the

● value of top node of the linked list which is the node at Head
void Top(){
struct Node{
if self.isempty() return ;null;
int data; return head->data;
Node* next; }
void push(int data){
Node(int value) {
data = data
if head == null
next = null
head = new Node(data);
} else
}; Node* new_node = new Node(data);
new_node->next = head;
struct Stack{ head = new_node;
}
Node* head; int pop(){
Stack(){
head = null; if self.isempty()
} return ;null;
bool isempty(){ else
if head == null Node* popped_node = head;
return True; head = head->next;
else popped_node->next = null;
return False; int value = popped_node->data;
} delete popped_node;
return value; }}
Stack Implementation
(Array)
The stack is implemented using a fixed-size
array, Stack[n].
top tracks the index of the top element, initialized
to -1 (empty stack).

Operations:
Push: Increment top, add element to Stack[top].
Pop: Remove Stack[top], decrement top.
class Stack {
int pop() {
int stack[100];
if (isEmpty()) {
int top;
cout << "Stack Underflow!;
return -1; //
Stack() {
}
top = -1;
return stack[top--];
}
}

bool isEmpty() {
int peek() {
return top == -1;
if (isEmpty()) {
}
cout << "Stack is empty! ;
bool isFull() {
return -1;
return top == 99;
}
}
return stack[top];
}
void push(int item) {
if (isFull()) {
cout << "Stack Overflow!;
int size() {
return;
return top + 1;
}
}
stack[++top] = item;
};
cout << "Pushed";
}
Applications of Stack
Applications of Stack
Used in a wide range of applications:

1. Converting infix expressions to postfix and prefix forms.


2. evaluating postfix expressions.
3. Parenthesis Checking
4. Reversing strings
5. Managing function calls and simulating recursions
6. Computing decimal to binary or other base conversions.
7. Analyzing the structure of code during compilation (Parsing)
and more
A1. Conversion between Expressions
Expression Types

1. Infix : ((A + B) × C) / D
2. Postfix : A B + C × D /
3. Prefix: / × + A B C D
A1. Conversion between Expressions
Why is Expression Conversion Needed?

Problem with Infix Notation:


Complex Evaluation: Requires handling:
Operator priority (e.g., x over +)
Ambiguity: Needs parentheses to clarify order.
Not suitable for a machine
A1. Conversion between Expressions
How Prefix & Postfix Help:

Simplified Evaluation:
No Parentheses required.
Priority of operators becomes irrelevant.
Operator position determines the sequence of evaluation.
Simplifies compilation into machine instructions.
A1. Conversion between Expressions
E = A/B ^ C + D \ E - A \ C
Manual Conversion

Steps:
E = (((A/(B ^ C)) + (D \ E)) - (A \ C))
1. Add parentheses based on operator
precedence and associativity.
2. move all operators so that they replace their
corresponding right/left parenthesis.
3. Eliminate all parentheses to get the
postfix/prefix expression.
E(postfix) = ABC ^/ DE x+ AC x -
A1. Conversion between Expressions
Infix to Postfix using Stack
A1. Conversion between Expressions
Infix to Postfix using Stack

Convert the given infix Expression into Postfix Using Stack:


(A * (B + C)) / D)
show all necessary steps in pictorically
A1. Conversion between Expressions
Infix to Prefix using Stack
A1. Conversion between Expressions
Infix to Prefix using Stack

Convert the given infix Expression into Prefix Using Stack:


(A * (B + C)) / D)
show all necessary steps in pictorically
A2. Expression Evaluation
Postfix Evaluation
A2. Expression Evaluation
Postfix Evaluation

Evaluate the following Postfix Experession Using Stack:


E = AB + Cx. .......... (A = 8)(B = 3)(C = -4)
show all necessary steps in pictorically
Thank You!

You might also like