A stack is a linear (non-primitive) data structure in which elements are inserted and deleted only from
Stacks are based on the LIFO (Last In First Out) principle, which states that the element inserted at the last is the first element to come out. For example, think of a stack of plates in a restaurant.
push(): Used to add elements.
pop(): Used to delete an element.
peek(): Allows the user to see the element on the top of the stack.
isEmpty(): Used to check if a stack is empty or not. To prevent performing operations on an empty stack, you are required to allocate the size of the stack that will be updated during push and pop operations. This operation basically returns the boolean value
True when the size is zero. Otherwise, it returns
You can implement Stacks by using Arrays or Linked Lists.
It is mostly used in solving problems that involve recursion.
Stacks are also used during function calls, converting an Infix to Postfix, during Depth First Search (DFS) and many more instances.
A queue is a linear (non-primitive) data structure in which elements are inserted at the rear of the list and deletion at the front of the list.
It is based on FIFO (First In First Out) principle.
Enqueue: Enqueue means inserting an element in the queue. A new element is inserted at the back of the queue, similar to how a person enters a queue at a ticket counter.
Dequeue: Dequeue means removing an element from the queue. Since it is based on the FIFO principle, we need to remove the element that was inserted first.
Front: Similar to the
peek operation in stacks, it returns the value of the element at the front without removing it.
isEmpty: Checks if the queue is empty. Its operation is as same as for stacks.
You can implement Queues using Arrays or Linked Lists.
Other applications include handling high-priority processes in an operating system, requesting your printer to print pages, Breadth First Search traversal in graph to store the nodes that need to be processed, and others.
It is also used in solving problems with sequential processing.
View all Courses