## 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.

## 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

*. Two principal operations can be performed on a queue;*

**tail***enqueue*and

*dequeue*.

*is the insertion operation while*

**Enqueue****is the deletion operation performed on a queue.**

*dequeue*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.

**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.