Stacks and Queues

Yugandharkumar
3 min readMar 7, 2023

--

Definition:

Both Stacks and Queues are linear data structures modeling real-life scenarios. Stack models a Last In First Out (LIFO) scenario and the Queue models a First In First Out Scenario(FIFO).

Both data structures are used to organize access to data, the difference is in the way every structure organizes that access…

Basic representation of Quee & Stack

Push is the adding method to the stack while Pop is the method to access and delete the top element of the stack.

Enqueue is the method to add an element to a Queue, and Dequeue is to remove previously enqueued elements.

What is Stack?

Stacks are popular, linear data structures — or more abstractly a sequential collection. Stack will implemented as Last In First Out(LIFO) Scenerio. There are two principle operations involved with stack; 1) the addition of elements, also known as push 2) the removal of elements, also known as pop.

A simple daily real-time example which we’re using is Instagram. In Instagram the Posts are like Last In First Out(LIFO). If you upload a post in insatgram it will show the new post as first post and other post as old post.

Realtime Example of Stack :

  1. A realtime example of stack is a stack of books as shown in the below figure. We can not take a book from the stack without removing books that are first stacked on top of it.
  2. A second real-time example of stack is a pile of plates in a cafeteria where last washed plate will be out first for use.
  3. Similarly, a DVD disk holder where CDs are arranged in such that the last CD will be out first for use. This is also an example of stack.

What is Quee?

Queues are popular, linear data structures that store elements in a First In First Out (FIFO) order. Thinking of a queue like a line at a store is the easiest way to conceptualize how queues work.

Realtime Example of Queue:

The most common realtime example of Queue is a waiting line in the supermarket. The cashier services the person that is at the beginning of the line first. Other customers enter the line only at the other end and wait for service.

Similarly, a queue in java is a common data structure in which there are two ends: front (head) and back (tail) of the queue. Elements are always inserted into the end (tail) of the queue and are accessed and deleted from the beginning (head) of the queue, as shown in the below figure.

--

--

Yugandharkumar
Yugandharkumar

Written by Yugandharkumar

Android DEV skilled in Kotlin, Jetpack Compose, and MVVM. Passionate about building intuitive apps and exploring new tech. Let’s create something amazing! 🚀

No responses yet