In this implementation, the Top of the stack is always represented by the Head of the linked list. This allows for O(1) Push and Pop operations without worrying about array size limits.
Templates allow the stack to hold any data type (integers, floats, or objects) while using a single logic definition.
top == MAX-1 before pushing and top == -1 before popping.
A Circular Queue solves the limitation of a simple array queue where space at the front cannot be reused.
rear = (rear + 1) % SIZE; front = (front + 1) % SIZE; A De-queue allows insertion and deletion from both the Front and the Rear. Using a Doubly Linked List is often preferred here as it provides O(1) access to the previous node at the Rear for deletion.
| Operation | Description |
|---|---|
| insertFront() | Adds element to the start of the list. |
| insertRear() | Adds element to the end of the list. |
| deleteFront() | Removes the first element. |
| deleteRear() | Removes the last element. |
Q1: Why is a Linked List implementation of a Stack considered "dynamic"?
Unlike the array implementation, it does not require a predefined size; memory is allocated only when a new element is pushed.
Q2: When is a Circular Queue "Full"?
In a typical implementation, it is full when (rear + 1) % SIZE == front.
Q3: What are the two types of restricted De-queues?
1. Input-Restricted (insertion at one end, deletion at both) and 2. Output-Restricted (insertion at both ends, deletion at one).
template <typename T> void Stack<T>::push(T val).