DEV Community

Cover image for What is Stack in Dsa.
Sarthak sachdeva
Sarthak sachdeva

Posted on

What is Stack in Dsa.

In Data Structures and Algorithms (DSA), a stack is a linear data structure that operates on the principle of Last In, First Out (LIFO). This means that the last item added to the stack is the first one to be removed. You can think of it like a stack of plates: the last plate placed on top is the first one you take off.

## Key Operations of a Stack

  1. Push: Adds an element to the top of the stack.
  2. Pop: Removes and returns the top element from the stack. If the stack is empty, this operation might return an error or exception depending on the implementation.
  3. Peek/Top: Returns the top element of the stack without removing it.
  4. IsEmpty: Checks if the stack is empty.
  5. Size: Returns the number of elements in the stack.

## see Example in image

Image description

Image description

in this array we can add elements and we have a fix file of array to store the value according to the size , we have a indexing of each array values .

Image description

in this our stack diagram

  • stack is base on LIFO is last in first out . in stack we have a top element to decide which one is first element

Array and Stack

Image description


perform pop operation in stack


Image description

  • when i pop last element to top value is change, top value start from 3

Image description


-- if you to make a stack implementation in java you can see my post about how to implement stack in java
implement stack in java post

here github link for code ...
github code repo

Top comments (0)