Open In App

Stack Definition & Meaning in DSA

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A stack is defined as a linear data structure that is open at one end and the operations follow the Last-In-First-Out (LIFO) order.

Example of Stack

Example of Stack

Characteristics of  Stack:

  • The stack follows the LIFO order, which means that the last element added to the stack will be the first element to be removed.
  • A register that points to the top of the stack is known as the stack pointer. It is used to keep track of the current position of the top of the stack.
  • A stack is also characterized by its capacity, which is the maximum number of elements it can hold at any given time. If an attempt is made to push an element onto a full stack, a stack overflow error will occur.

Applications of Stack:

  •  Mathematical expression: Stacks are used to evaluate mathematical expressions, such as infix, postfix, and prefix expressions. They are also used to convert one expression to another.
  • Backtracking: Stacks are used in backtracking algorithms to keep track of the path taken to reach a particular point in the algorithm.
  • Function call: Stacks are used to keep track of function calls and return addresses in programming languages.
  • Memory Management: Stacks are used in memory management to allocate and deallocate memory for variables and data structures.

To learn more about stack applications. refer to this article.

Advantages of Stack:

  • Stacks are simple to implement and can be created using arrays, linked lists, or even dynamic memory allocation.
  • Stacks are efficient in memory management as they only allocate memory for the elements that are currently in the stack.
  • The push and pop operations have O(1) time complexity, which means that take constant time regardless of the size of the stack.

To learn more about the advantages of the stack, refer to this article.

Disadvantages of Stack:

  • Static stacks have a fixed size, which means that in a stack we can add limited elements.
  • Stacks can suffer from overflow when you try to add more elements than the stack size, and underflow when you try to remove an element from an empty stack.
  • Stacks can only be used to store and retrieve data in a specific order, whereas queues and trees can be used to store and retrieve data in a variety of orders.

To learn more about the disadvantages of the stack, refer to this article.

What else can you read?


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads