what is stack

11 months ago 54
Nature

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: Push, which adds an element to the collection, and Pop, which removes the most recently added element that was not yet removed. Additionally, a peek operation can, without modifying the stack, return the value of the last element added. Calling this structure a stack is by analogy to a set of physical items stacked one atop another, such as a stack of plates. The order in which an element is added to or removed from a stack is described as last in, first out, referred to by the acronym LIFO. As with a stack of physical objects, this structure makes it easy to take an item off the top of the stack, but accessing a datum deeper in the stack may require taking off multiple other items first.

Some key points related to stack are:

  • Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
  • Stack has one end, whereas the Queue has two ends (front and rear) .
  • It contains only one pointer top pointer pointing to the topmost element of the stack.
  • Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack.
  • In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack.

Stacks can be implemented in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same. There are some basic operations that allow us to perform different actions on a stack, such as Push, Pop, Peek, and isEmpty.