STACKS AND QUEUES
STACKS AND QUEUES
Stack: In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.
Queue: An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. The picture demonstrates the FIFO access. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Difference between Stack and Queue Data Structures
Stack:- A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into stack is called push operation, and deletion of an element from the stack is called pop operation. In stack we always keep track of the last element present in the list with a pointer called top.
The diagrammatic representation of stack is given below:
Queue:- A queue is a linear data structure in which elements can be inserted only from one side of the list called rear, and the elements can be deleted only from the other side called the front. The queue data structure follows the FIFO (First In First Out) principle, i.e. the element inserted at first in the list, is the first element to be removed from the list. The insertion of an element in a queue is called an enqueue operation and the deletion of an element is called a dequeue operation. In queue we always maintain two pointers, one pointing to the element which was inserted at the first and still present in the list with the front pointer and the second pointer pointing to the element inserted at the last with the rear pointer.
The diagrammatic representation of queue is given below:
Difference between
Stack and Queue
Stack |
Queue |
The stack is based on LIFO(Last In First
Out) principle |
The queue is based on FIFO(First In First
Out) principle. |
Insertion Operation is called Push Operation |
Insertion Operation is called Enqueue
Operation |
Deletion Operation is called Pop Operation |
Deletion Operation is called Dequeue
Operation |
Push and Pop Operation takes place from one
end of the stack |
Enqueue and Dequeue Operation takes place
from a different end of the queue |
The most accessible element is called Top
and the least accessible is called the Bottom of the stack |
The insertion end is called Rear End
and the deletion end is called the Front End. |
Simple Implementation |
Complex implementation in comparison to
stack |
Only one pointer is used for performing
operations |
Two pointers are used to perform operations |
Empty condition is checked using Top== -1 |
Empty condition is checked using Front== -1 || Front==Rear+1 |
Full condition is checked using Top== Max-1 |
Full condition is checked using Rear== Max-1 |
There are no variants available for stack |
There are three types of variants i.e circular queue, double-ended queue and priority queue |
Can be considered as a vertical collection
visual |
Can be considered as a horizontal
collection visual |
Used to solve the recursive type problems |
Used to solve the problem having sequential
processing |