Stack and Queues�
By
Mr. Abhijit T. Somnathe
Data Structure
What is Linear Data Structure?
What is STACK?
Examples of STACK?
Basic Stack Operations
Basic Stack Operations
Applications of Stack
Array Implementation
int stack[10]
int top = -1
Array Implementation
Array Implementation
class Stack
{
int arr[]
int capacity
int top
}
Array Implementation
Stack(int cap)
{
capacity = cap
top = -1
}
PUSH Operation
What changes are made to the stack when a new element is pushed?
PUSH Operation
What if the stack is filled to its capacity?
PUSH Operation
void push(int item)
{
if ( top == capacity - 1 )
print( "Stack overflow!" )
else
{
arr[top+1] = item
top = top + 1
}
}
POP Operation
What changes are made to the stack internally for a pop operation?
POP Operation
void pop()
{
if ( isEmpty() == True )
print( "Stack is empty!" )
else
top = top - 1
}
Balanced parentheses using Stack
Balanced parentheses using Stack
Balanced parentheses using Stack
Balanced parentheses using Stack
Balanced parentheses using Stack
Infix to Postfix Conversion
Infix to Postfix Conversion
Infix to Postfix Conversion
Infix to Postfix Conversion
Current Symbol | Action Performed | Stack Status | Postfix Expression |
2 | PUSH C | C | 2 |
2 | | | 2 |
+ | PUSH + | (+ | 2 |
( | PUSH ( | (+( | 24 |
4 | | | 24 |
- | PUSH - | (+(- | 241 |
1 | POP | | 241- |
Infix to Postfix Conversion
Current Symbol | Action Performed | Stack Status | Postfix Expression |
) | | (+ | 241- |
* | PUSH * | (+* | 241- |
3 | | | 241-3 |
| POP * | | 241-3* |
| POP + | | 241-3*+ |
) | | | |
Postfix Expression Evaluation
Postfix Expression Evaluation
Postfix Expression Evaluation
Postfix Expression Evaluation
Postfix Expression Evaluation
Reading Symbol | Stack Operation | Evaluated Part of Expression |
Initial | Stack is Empty | Nothing |
5 | Push (5) | Nothing |
3 | Push (3) | Nothing |
+ | Value1 = pop() Value2 = pop() Result = Value2+Value1 Push(Result) | Value1 = pop(); //3 Value2 = pop(); //5 Result = 5+3; //8 Push(8) |
8 | Push(8) | (5+3) |
2 | Push(2) | (5+3) |
Postfix Expression Evaluation
Reading Symbol | Stack Operation | Evaluated Part of Expression |
- | Value1 = pop() Value2 = pop() Result = Value2 – Value1 Push(Result) | Value1 = pop(); //2 Value2 = pop(); //8 Result = 8 – 2; //6 Push(6) |
* | Value1 = pop() Value2 = pop() Result = Value2 * Value1 Push(Result) | Value1 = pop();//6 Value2 = pop(); //8 Result = 8*6; //48 Push(48) |
$ End of Expression | Result = pop() | Display Result 48 As Final Result |
Queue
Queue
Operations on Queue
Operations on Queue
Operations on Queue
Operations on Queue
Enqueue Operation
Enqueue Operation
Enqueue Operation
Dequeue Operation
Dequeue Operation
Dequeue Operation
Types of Queue
Simple Queue
Simple Queue
Circular Queue
Circular Queue
Priority Queue
Priority Queue
Double-Ended Queue (Deque)
Double-Ended Queue (Deque)
Double-Ended Queue (Deque)
Double-Ended Queue (Deque)
Double-Ended Queue (Deque)
Round Robin Algorithm
Characteristics of Round-Robin
Characteristics of Round-Robin
Round Robin Algorithm
Process Queue | Burst Time |
P1 | 4 |
P2 | 3 |
P3 | 5 |
Round Robin Algorithm
Round Robin Algorithm
Round Robin Algorithm
Round Robin Algorithm
Round Robin Algorithm
Round Robin Algorithm
Advantages of Round-Robin
Advantages of Round-Robin
Disadvantages of Round-Robin
Disadvantages of Round-Robin
ANY DOUBTS?