Stack & Queue
Stack & Queue
Stack
A stack is a linear data structure that operates on the LIFO (Last In, First Out)
principle, meaning the last element added to the stack is the first one to be
removed.
Applications of Stacks
1. Expression Conversion (Infix to Postfix/Prefix)
Stacks are widely used for converting expressions between infix, prefix, and
postfix notations.
Pop and append all operators from the stack that have higher or
equal precedence.
Pop and append all operators from the stack until an opening
parenthesis is encountered.
Time Complexity:
Time Complexity:
3. Pop(stack): Removes and returns the top element of the stack if it’s not
empty.
1. Push(x):
2. Pop():
1 Algorithm Pop(stack):
2
3 1. If stack.top == -1:
4 Print "Stack Underflow".
5 Return.
6 2. Set x = stack[stack.top].
7 3. Decrement stack.top by 1.
8 4. Return x.
3. Peek():
1 Algorithm Peek(stack):
2
3 1. If stack.top == -1:
4 Print "Stack is Empty".
5 Return.
6 2. Return stack[stack.top].
1. Push(x):
2. Pop():
1 Algorithm Pop(stack):
2
3 1. If stack.top == NULL:
4 Print "Stack Underflow".
5 Return.
6 2. Set x = stack.top.value.
7 3. Set stack.top = stack.top.next.
8 4. Free the memory of the popped node.
9 5. Return x.
Complexity Analysis
Linked List
Operation Array Implementation
Implementation
Types of Queues
1. Simple Queue
Operations
Algorithm:
1 Algorithm Enqueue(queue, x):
2
3 1. If (rear == maxSize - 1):
4 Print "Queue Overflow".
5 Return.
6 2. Increment rear by 1.
7 3. Set queue[rear] = x.
Algorithm:
1 Algorithm Dequeue(queue):
2
3 1. If (front > rear):
4 Print "Queue Underflow".
5 Return.
6 2. Set x = queue[front].
7 3. Increment front by 1.
8 4. Return x.
Drawback:
2. Circular Queue
The rear and front pointers wrap around to the beginning of the array when
they reach the end.
Operations
Algorithm:
1 Algorithm Enqueue(queue, x):
2
3 1. If ((rear + 1) % maxSize == front):
4 Print "Queue Overflow".
5 Return.
6 2. If (front == -1):
7 Set front = 0.
8 3. Set rear = (rear + 1) % maxSize.
9 4. Set queue[rear] = x.
Algorithm:
1 Algorithm Dequeue(queue):
2
3 1. If (front == -1):
4 Print "Queue Underflow".
5 Return.
6 2. Set x = queue[front].
7 3. If (front == rear):
8 Set front = rear = -1. // Reset queue.
9 Else:
10 Set front = (front + 1) % maxSize.
11 4. Return x.
Advantages:
3. Priority Queue
Elements with higher priority are dequeued before elements with lower
priority.
If two elements have the same priority, they are dequeued in FIFO order.
Operations
Algorithm
1 Algorithm Dequeue(queue):
2
3 1. If (queue is empty):
4 Print "Queue Underflow".
5 Return.
6 2. Find the element with the highest priority.
7 3. Remove that element from the array.
8 4. Return the element.
Advantages:
Drawback:
Linear with
Structure Linear Circular
priority
Highest priority
Deletion From the front From the front
element
Efficient for
Wastes space Efficient space
Efficiency priority-based
after dequeue utilization
tasks
O(1) (unsorted),
Complexity
O(1) O(1) O(log n
) (sorted
(Enqueue)
heap)
O(n) (unsorted),
Complexity
O(1) O(1) O(logn) (sorted
(Dequeue)
heap)
Scheduling, CPU
Buffer
Applications resource scheduling,
management
management event handling
Principle Last In, First Out (LIFO). First In, First Out (FIFO).
Implemented using
Implemented using
Implementation arrays, linked lists, or
arrays or linked lists.
circular queues.
Enqueue(10),
Push(10), Push(20), Pop()
Example Enqueue(20), Dequeue()
→ Stack: [10].
→ Queue: [20].