Stack
Stack is a linear data structure that follows a particular order in which the operations are performed. The order is LIFO(Last in first out). Entering and retrieving data is possible from only one end. The entering and retrieving of data is also called push and pop operation in a stack. There are different operations possible in a stack like reversing a stack using recursion, Sorting, Deleting the middle element of a stack, etc.
Characteristics of a Stack:
Stack has various different characteristics which are as follows:
- Stack is used in many different algorithms like Tower of Hanoi, tree traversal, recursion, etc.
- Stack is implemented through an array or linked list.
- It follows the Last In First Out operation i.e., an element that is inserted first will pop in last and vice versa.
- The insertion and deletion are performed at one end i.e. from the top of the stack.
- In stack, if the allocated space for the stack is full, and still anyone attempts to add more elements, it will lead to stack overflow.
Applications of Stack:
Different applications of Stack are as follows:
- The stack data structure is used in the evaluation and conversion of arithmetic expressions.
- It is used for parenthesis checking.
- While reversing a string, the stack is used as well.
- Stack is used in memory management.
- It is also used for processing function calls.
- The stack is used to convert expressions from infix to postfix.
- The stack is used to perform undo as well as redo operations in word processors.
- The stack is used in virtual machines like JVM.
- The stack is used in the media players. Useful to play the next and previous song.
- The stack is used in recursion operations.
Operation performed on stack ;
A stack is a linear data structure that implements the Last-In-First-Out (LIFO) principle. Here are some common operations performed on stacks:
- Push: Elements can be pushed onto the top of the stack, adding a new element to the top of the stack.
- Pop: The top element can be removed from the stack by performing a pop operation, effectively removing the last element that was pushed onto the stack.
- Peek: The top element can be inspected without removing it from the stack using a peek operation.
- IsEmpty: A check can be made to determine if the stack is empty.
- Size: The number of elements in the stack can be determined using a size operation.
These are some of the most common operations performed on stacks. The specific operations and algorithms used may vary based on the requirements of the problem and the programming language used. Stacks are commonly used in applications such as evaluating expressions, implementing function call stacks in computer programs, and many others.
Real-Life Applications of Stack:
- Real life example of a stack is the layer of eating plates arranged one above the other. When you remove a plate from the pile, you can take the plate to the top of the pile. But this is exactly the plate that was added most recently to the pile. If you want the plate at the bottom of the pile, you must remove all the plates on top of it to reach it.
- Browsers use stack data structures to keep track of previously visited sites.
- Call log in mobile also uses stack data structure.
Want to get started with Stack? You can try out our curated articles and lists for the best practice: