What is stack and queue?


Stack

Stack is a container of objects or say ADT (Abstract-Data-Type used in most programming languages) that are inserted and removed according to the last-in first-out (LIFO) principle.

Simply to understand with example – a deck of cards or a pile of plates, etc.

Queue

Queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle.

Simply to understand with example – a queue data structure is a line of people waiting to buy a ticket at a cinema hall/railways etc. The new person will join the line from the end and the person standing at the front will be the first to get the ticket and leave the line.

The difference Between Stack Data Structure and Queue Data Structure

Parameter Stack Queue
Basics (both are linear data structure) The objects are removed or inserted at the same end. The objects are removed and inserted from two different ends.
Pointers It has only one pointer- the top. This pointer indicates the address of the topmost element(last inserted one of the stack). It uses two pointers (in a simple queue) for reading and writing data from both the ends- the front and rear. The rear one indicates the address of the last inserted element, whereas the front pointer indicates the address of the first inserted element in a queue.
Operations Stack uses two operations, push and pop. The pop operation functions to remove the element from the list, while the push operation functions to insert the element in a list. Queue uses two operations, enqueue and dequeue. The dequeue operation deletes the elements from the queue, and the enqueue operation inserts the elements in a queue.
Structure Insertion and deletion of elements take place from one end only. That is called the top. Insertion and deletion of elements takes place from both ends. Insertion uses the rear end, and deletion uses the front end.
Full Condition Stack is full when top== max-1 Queue is full when rear==max-1
Empty Condition Stack is empty when top==-1 Queue is empty When front = rear+1 or front== -1
Types A Stack data structure does not have any types. A Queue data structure has three types (circular queue, priority queue, and double-ended queue).

👈       👉