What is a stack?

What is a stack?

A stack is a type of data structure. Here, only the top element in the structure can be accessed. Stack follows LIFO, which stands for Last In First Out. Basically, what this means is that in the stack, as new data keeps getting added to the structure, the previous data keeps getting pushed out and cannot be accessed. Only the last data to be added can be accessed.

Compare this to a stack of plates. Each plate is stacked on top of each other. However, when it comes to removing the plate, either to use or to wash, the first plate to be taken would be from the top of the pile. If one removes a plate from the middle or the bottom, they risk toppling the entire pile. Hence, the last plate to be put on the stack, is the first one to be removed.

In the pushdown stacks, only two operations are allowed: push the item into the stack, and pop the item out of the stack. Push adds an item to the top of the stack, and pop removes the item from the top.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.