The Startup
Published in

The Startup

DSA: Let’s Deep Dive Into the Stack

We use the stack concept in our office work.

A stack is a linear data structure used for storing the data. let’s consider the office work, a developer is working on a long-term project and the manager gives the developer to a new task which is the high priority. So the developer puts the long-term project aside and starts work on the new task. After some time the phone rings and the priority is changed as it must be answered immediately. The developer pushes the present task into the pending tray and answers the phone. after hanging up the phone the developer starts working on the priority start then starts on a long-term project.

Photo by Jeremy Bishop on Unsplash

What is a Stack: A stack is an ordered list in which insertion and deletion are done at one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last In First Out (LIFO).

There are two things into a stack. When an element is inserting in a stack, the concept is called PUSH, and when an element is removed from the stack, called POP.

When we try to do pop from an empty stack is called underflow and when we push the data in a full stack is called overflow.

Implementation:

  1. Simple array based implementation.
  2. Dynamic array based implementation.
  3. Linked list implementation.
  • Simple array based implementation: We use static array and implementation of the basic operation.
  • Dynamic Array Implementation: We face the problem in StaticArray that is Overflow because the size of the stack is fixed. So let’s make it with dynamic array, Here we will expend the size when the stack will overflow.
  • Linked List Implementation: The other way of implementing stacks is by using Linked lists. Push operation is implemented by inserting element at the beginning of the list. Pop operation is implemented by deleting the node from the beginning.

So above three ways we can implement a stack. Let’s compare Array implementation and Linked List implementation

Array Implementation:

  • Operations take constant time.
  • Expensive doubling operating every once in a while

Linked List Implementation:

  • Grows and shrinks gracefully.
  • Every operation takes constant time O(1).
  • Every operation uses extra space and time to deal with references.

That’s it. I hope it will help you to understand the Stack concept. Stay tuned for more topics in Data Structure.

I referenced the book of Data structure and algorithms (Narasimha Karumanchi).

Thank you for reading. Have a happy day. :)

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Scrum Masters should do Business Analysis for the Team.

Mist NFT — Weekly Recap #1

Hiring manager interviewing for A+ JavaScript developer.

Hackathon — Marathon for hackers

Generate APK from an Ionic Project

IOTA — The Missing Artifact in Scrum

Dockerfiles vs. Cloud-native Buildpacks

What I’ve learned while using Spinnaker for our deployments ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arnish Gupta

Arnish Gupta

A Programmer after Sunrise and A Writer after Sunset. ##Blockchain ##Programming ##Development ##Technology_Blogger. visit me at https://about.me/arnishgupta.

More from Medium

Day 4: Interview & HackerEarth

My interview experience with Meta

Internship experience as a software engineering intern at Rootcode

HTTP/1.1, HTTP/2, and HTTP/3: A Comparison