Stacks and Queues
Stacks and Queues
1. Stack Abstract Data Type (ADT)
Definition: A stack is a Last-In-First-Out (LIFO) data structure. Items are inserted (pushed) and removed (popped) from the top.
Key Operations:
All of these structures are O(1)
Push: Insert an item at the top of the stack.
Pop: Remove and return the item from the top.
Peek: Return the top item without removing it.
IsEmpty: Returns true if the stack has no items.
GetLength: Returns the number of items in the stack.
Implementation:
A stack can be implemented using an array, a linked list, or a vector.
When implemented using a linked list, the head of the list represents the top of the stack.
Common Applications:
Reversing data (such as reversing a word).
Implementing undo functionality in text editors.
Expression evaluation and syntax parsing.
2. Stack Operations with Examples
Push and Pop Example:
Operations executed: Push(7), Push(14), Push(9), Push(5)
Resulting Stack Configuration: Stack = 5, 9, 14, 7 (from top to bottom).
Pop Operations:
The first Pop() returns 5.
The second Pop() returns 9.
Common Stack Errors:
Applying Pop or Peek on an empty stack leads to undefined behavior.
3. Stack Implementation Using Linked Lists
Definition: A stack implemented using a linked list uses the head as the top of the stack.
Operations:
StackPush(stack, item):
Allocates a new node and inserts it at the top (the head of the linked list).
StackPop(stack):
Removes the head node from the stack and returns its data.
Implementation Steps:
Allocate a new node.
Insert it as the new head of the list.
For popping, remove the head node and return its data.
4. Queue Abstract Data Type (ADT)
Definition: A queue is a First-In-First-Out (FIFO) data structure. Items are inserted at the rear (end) and removed from the front.
Key Operations:
Enqueue: Insert an item at the rear of the queue.
Dequeue: Remove and return the item from the front of the queue.
Peek: Return the front item without removing it.
IsEmpty: Returns true if the queue is empty.
GetLength: Returns the number of items in the queue.
Implementation: A queue can be implemented using arrays or linked lists, where the head represents the front, and the tail represents the rear.
Common Applications:
Job scheduling in operating systems.
Print job queues.
Breadth-First Search (BFS) in graph algorithms.
5. Queue Operations with Examples
Enqueue and Dequeue Example:
Operations executed: Enqueue(7), Enqueue(14), Enqueue(9), Dequeue()
Resulting Queue Configuration: Queue = 14, 9 (from front to back).
Dequeue Operation:
Dequeue() returns 7.
Common Queue Errors:
Applying Dequeue or Peek on an empty queue leads to undefined behavior.
6. Queue Implementation Using Linked Lists
Definition: A queue implemented using a linked list uses the head for the front and the tail for the rear.
Operations:
QueueEnqueue(queue, item):
Allocates a new node and appends it to the tail of the list.
QueueDequeue(queue):
Removes the head node from the queue and returns its data.
Implementation Steps:
Allocate a new node.
Append the node to the end of the list (update tail).
For dequeueing, remove the head and return its data.
7. Double-Ended Queue (Deque) ADT
Definition: A deque (pronounced "deck") is a data structure where items can be inserted and removed from both the front and rear. Items can’t be removed or added ate the beginning
Key Operations:
PushFront: Insert an item at the front.
PushBack: Insert an item at the rear.
PopFront: Remove and return the item from the front.
PopBack: Remove and return the item from the rear.
PeekFront: Return the front item without removing it.
PeekBack: Return the rear item without removing it.
IsEmpty: Returns true if the deque is empty.
GetLength: Returns the number of items in the deque.
Common Applications:
Sliding window algorithms in data processing.
Storing undo/redo history in applications.
8. Deque Operations with Examples
PushFront and PushBack Example:
Operations executed: PushBack(7), PushFront(14), PushFront(9), PushBack(5)
Resulting Deque Configuration: Deque = 9, 14, 7, 5 (from front to back).
Pop Operations:
PopBack() returns 5.
PopFront() returns 9.
9. Deque Implementation Using Linked Lists
Definition: A deque implemented using a linked list can insert and remove items from both the head and tail.
Operations:
DequePushFront(deque, item): Inserts an item at the head of the list.
DequePushBack(deque, item): Inserts an item at the tail of the list.
DequePopFront(deque): Removes and returns the head item of the deque.
DequePopBack(deque): Removes and returns the tail item of the deque.
Additional Notes
Edge Cases:
Always check if the stack, queue, or deque is empty before performing Pop or Dequeue operations to avoid undefined behavior.
Efficiency:
Consider the time complexity of each operation when choosing between array and linked list implementations.