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

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 .

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


perform pop operation in stack


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


— 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

原文链接:What is Stack in Dsa.

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容