Difference Between Stack and Queue

Main Difference – Stack vs Queue

In Computer Science, stack and queue are two abstract data types that are simple data structures that use pointers to represent dynamic sets. However, a difference can be noted between them based on their implementations. Basic operations of inserting and deleting elements are supported by both stack and queue. The main difference between Stack and Queue is that a stack implements Last In First Out or LIFO policy, whereas a queue implements First In First Out or FIFO policy.

What is Stack

A stack is a linear data structure that serves as a collection of elements. Only one end of the structure is accessible to perform operations on elements, and it is commonly referred to as the top. Two principal operations can be performed on a stack; push and pop. An ‘insert’ operation performed on a stack is called push and a ‘delete’ operation performed on a stack is called pop.

The push operation adds an element to the top of the collection. Performing a pop operation removes an element that is at the top of the collection. Since the elements that are removed from the stack are in reverse order to the order of their addition, the structure is known to follow Last In First Out or an LIFO approach. Given this implementation, the lowest elements have been on the stack for the longest.

A stack is considered as a restricted data structure due to the small number of operations that can be performed on a stack. Additionally, a peek operation can be implemented to return the value of the top element without modifying the element. Also, implementations of a stack often have an IsEmpty function to check whether the stack is empty. In environments that rely heavily on stacks, functions like delete, swap/exchange and rotate may also be provided. But these are not essential for the basic functionality of a stack.

A stack has a bounded capacity. If the stack is full, it goes into an overflow state, which means there is not enough space for more elements to be pushed onto the stack. If the stack is empty, and there are no elements to pop, the stack is in an underflow state.

A stack can be easily implemented using arrays or linked lists in most high-level programming languages.

Stacks are applicable in areas such as evaluating arithmetic expressions, run-time memory management, tree traversal, syntax parsing, etc.Difference Between Stack and Queue

What is Queue

A queue is a linear data structure that also serves as a collection of elements. Both ends of a queue are accessible for performing operations on elements and are typically called head and tail. Two principal operations can be performed on a queue; enqueue and dequeue. Enqueue is the insertion operation while dequeue is the deletion operation performed on a queue.

When an element is enqueued, it is added to the tail of the queue. Performing a dequeue operation will remove an element from the head of the queue. Since the enqueued elements are always dequeued in the same order as they were enqueued, the structure is said to implement a First In First Out or FIFO approach.

Similar to a stack, a queue is also a restricted data structure given the small number of operations that can be carried out. In addition, a peek operation can be implemented in a queue, which will return the value of the element at the head of the queue without dequeuing it. Other primitive operations on a queue may include IsEmpty, IsFull, and display. The IsEmpty function checks whether the queue is empty and IsFull check if the queue is full. The display function can be used to present the content of the queue. But again, these functions are not critical for the implementation of a queue.

 Unlike a stack, queues can be implemented to have a bounded capacity or with no specific capacity. An overflow state of a queue occurs when an element is enqueued to a full queue, and an underflow state occurs when an element is dequeued, but the queue is empty.    

The type of queue may differ on how enqueuing and dequeuing operations are performed on elements. Circular queue, priority queue, and double-ended queue are the special types of queues.

Using arrays and linked lists, queues can be efficiently implemented in high-level programming languages.

Queues are applicable in many areas such as simulations, batch processing in operating systems, scheduling algorithms, buffering requests, multiprogramming platform systems, etc.

Main Difference - Stack vs Queue

Difference Between Stack and Queue

Accessibility to Elements

 In a stack, operations on data can be performed only at the top of the stack.

 In a queue, both ends of the queue are accessible for operations. An insertion takes place at the tail of the queue, and a deletion can be made at the head.

Behaviour

A stack is an LIFO data structure, where the element that was added last to the stack is the first element to be removed. The removal is in reverse order to the order of addition.

A queue is an FIFO data structure, where the element that was added first to the queue will be the first element to be removed. Order of insertion and removal are the same.

Basic Operations

In a stack, an element is inserted at the top of the stack and removed from the top as well.

But in a queue, an element is inserted at the end of a queue and is removed from the front.

Capacity

A stack has a bounded capacity.

 A queue can be of bounded capacity, but is usually implemented without a specific capacity.

Wastage of Memory Space

Since a stack only needs one pointer to keep track of the top of the stack, there is no wastage of memory space.

A queue needs two pointers ‘front’ and ‘rear’ to keep track of both ends of the queue. Therefore, there is wastage of memory space.

Stack vs. Queue – Summary

Both stack and queue are used for the purpose of maintaining ordered lists of elements. While a stack is an LIFO data structure, a queue implements an FIFO approach. Only one end of a stack is accessible for principal operations, but both ends of a queue can be used. 

About the Author: admin


Related pages


what is the difference between a direct and indirect objectdiode and zener diodeplateau landform definitionguar gum and xanthan gumfinite and nonfinite verbs examplesdifference between prose and versedifference between typical and atypical antipsychoticstributes to grandadpecan vs walnut nutritionstructural formula for ethanol and ethanoic acidexamples of adverb of affirmation and negationlarceny theft differenceinductor capacitorantagonist protagonistdifference between prophase 1 and prophase 2what is neurosis and psychosisdifference between catabolic and anaboliccolour of ammonia gassociology vs cultural anthropologymolecular formula of cyclohexanedifference between hurricane and typhoon and cyclonemicrometer and vernier caliperpolarization of light meaningwhat are the differences between prose and poetrywhat is the difference between benign and essential hypertensiondifference of concave and convex mirrorthe difference between connotation and denotationdifference between garnish and decorationdistillation definition chemistrydefine the climax of a storyna3co no2 6define law of conservation of momentumimportance of ppfedt versus estis grapefruit a hybridde fakto i de jurecoenzyme exampleprokaryotes and eukaryotes comparisonwander verbdifference between a labrador and a golden retrieverforming adverbs from adjectivesexample of tactile imageryhow to calculate total owners equityaccounting rate of return arrpolyps and cystsis citric acid vitamin cproperties of amyloseflirting with intentdifference between a covalent bond and an ionic bondmeaning of chamferpolar and nonpolar solvents definitiondifference between hornet and waspductility and malleability are examples ofkinds of adverb with examplesbake and broilwhat is dialects meansdifference between internal and external combustion enginedifference latte and cappuccinopoems with caesuraicteric sclera causeswhat is the difference between ionic and covalentdifference between alkali and acid3rd person omniscient exampleexample of third person omniscient in literaturetotipotent pluripotentdifference between a condo and townhousecast iron or wrought ironairport gst claimwhat is jargon and slangwhat is a compound pendulumdioecious plant definitionsuspension in chemistry definitionprovisional balance sheet formatdifferences between primary and secondary successioncalculate cpi index