Stack and Queue Implementation
2.1 Stack (LIFO – Last In, First Out)
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last element added is the first one to be removed.
Operations on Stack:
- Push(x): Adds element x at the top.
- Pop(): Removes the top element.
- Peek(): Returns the top element without removing it.
- isEmpty(): Checks if the stack is empty.
- isFull(): Checks if the stack is full (only for arrays).
Stack Implementation Methods:
- Using Arrays: Fixed-size implementation, simple but can overflow.
- Using Linked Lists: Dynamic size, but uses extra memory for pointers.
2.2 Queue (FIFO – First In, First Out)
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The first element added is the first one to be removed.
Operations on Queue:
- Enqueue(x): Adds element x to the rear.
- Dequeue(): Removes the front element.
- Front(): Returns the front element without removing it.
- isEmpty(): Checks if the queue is empty.
- isFull(): Checks if the queue is full (only for arrays).
Queue Implementation Methods:
- Using Arrays: Fixed size, simple but inefficient after deletions.
- Using Linked Lists: Dynamic size, but requires extra memory for pointers.