Stack Data Structure (1)
Stack Data Structure (1)
Data Structure
2024
1 What is Stack 5 Applications of Stack
Expressions Conversion
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).
Is empty()
Is Full()
Cases to Consider
Stack Overflow: Occurs when pushing into a full stack. (array implementation)
● Initialise a node
● 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
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. 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?
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