Stack vs Queue
Stack is an ordered list in which insertion and deletion of list items can be done only in one end called the top. Due to this reason, stack is considered as a Last in First out (LIFO) data structure. Queue is also an ordered list in which insertion of list items are done in one end called the rear, and the deletion of items are done in the other end called the front. This insertion and deletion mechanism makes the queue a First in First out (FIFO) data structure.
What is Stack?
As mentioned earlier, stack is a data structure in which elements are added and removed from only one end called the top. Stacks allow only two fundamental operations called push and pop. The push operation adds a new element to the top of the stack. The pop operation removes an element from the top of the stack. If the stack is already full, when a push operation is performed, it is considered as a stack overflow. If a pop operation is performed on an already empty stack, it is considered as a stack underflow. Due to the small number of operations that could be performed on a stack, it is considered as a restricted data structure. Additionally, according to the way that the push and pop operations are defined, it is clear that elements that were added last in to the stack go out of the stack first. Therefore stack is considered as a LIFO data structure.
What is Queue?
In a queue, elements are added from the rear of the queue and removed from the front of the queue. Since the elements that are added first will be removed from the queue first, it maintains the FIFO order. Due to this order of adding and removing elements, queue represents the idea of a checkout line. General operations supported by a queue are en-queue and de-queue operations. En-queue operation will add an element at the rear of the queue, while the de-queue operation removes an element from the front of the queue. In general, queues do not have a limit on the number of elements that can be added to the queue besides the memory constraints.
What is the difference between Stack and Queue?
Even though both the stacks and queues are kinds of ordered lists, they have some important differences. In stacks, adding or deleting items can be done only from one end called the top, while in queues adding items is done from one end called the rear and deleting items is done from the other end called the front. In a stack, items that are added last to the stack will be removed first from the stack. Therefore stack is considered as a LIFO data structure. In queues, items that are added first will be removed from the queue first. Therefore queue is considered as a FIFO data structure.
Related Link:
Difference Between Stack and Heap
Leave a Reply