Stack is a linear data structure that follows Last In, First Out(LIFO) Principle, the last element inserted is the first to be popped out. It means both insertion and deletion operations happen at one end only.While Queue follows the principle of First In, First out (FIFO), where the first element added to the queue is the first one to be removed.
Stack: Follows Last-In-First-Out (LIFO).
Queue: Follows First-In-First-Out (FIFO).
Stack Overview
Key Features of Stack
Top: The only accessible end for insertions/deletions.
Operations:
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
isFull() returns true if the stack is full else false.
Applications of Stack
Function call management (call stack).
Undo/Redo functionality.
Syntax parsing (e.g., matching parentheses).
Queue Overview
Key Features of Queue
Front/Back: Elements enter at the back and exit from the front.
Operations:
Enqueue: Adds an element to the end (rear) of the queue. If the queue is full, an overflow error occurs.
Dequeue: Removes the element from the front of the queue. If the queue is empty, an underflow error occurs.
Peek/Front: Returns the element at the front without removing it.
Size: Returns the number of elements in the queue.
isEmpty: Returns true if the queue is empty, otherwise false.
isFull: Returns true if the queue is full, otherwise false.
Simple Queue: Simple Queue simply follows FIFO Structure. We can only insert the element at the back and remove the element from the front of the queue. A simple queue is efficiently implemented either using a linked list or a circular array.
Double-Ended Queue (Deque): In a double-ended queue the insertion and deletion operations, both can be performed from both ends. They are of two types: Input Restricted Queue: This is a simple queue. In this type of queue, the input can be taken from only one end but deletion can be done from any of the ends. Output Restricted Queue: This is also a simple queue. In this type of queue, the input can be taken from both ends but deletion can be done from only one end.
Priority Queue: A priority queue is a special queue where the elements are accessed based on the priority assigned to them. They are of two types:
Ascending Priority Queue: In Ascending Priority Queue, the elements are arranged in increasing order of their priority values. Element with smallest priority value is popped first.
Descending Priority Queue: In Descending Priority Queue, the elements are arranged in decreasing order of their priority values. Element with largest priority is popped first.
public T Dequeue() { if (IsEmpty()) thrownew InvalidOperationException("Queue is empty."); varvalue = _array[_front % _array.Length]; _front++; _count--; returnvalue; }
public T Peek() => IsEmpty() ? thrownew InvalidOperationException("Queue is empty.") : _array[_front % _array.Length];
public T Dequeue() { if (IsEmpty()) { thrownew InvalidOperationException("Queue is empty"); } MoveInToOut(); // Ensure outStack has elements return _outStack.Pop(); }
public T Peek() { if (IsEmpty()) { thrownew InvalidOperationException("Queue is empty"); } MoveInToOut(); return _outStack.Peek(); }
privatevoidMoveInToOut() { if (_outStack.Count == 0) { while (_inStack.Count > 0) { _outStack.Push(_inStack.Pop()); } } }