Stack is linear Data Structure which is used to hold data. Imagine a stack as a tower of books.
When we add a book, we place it on top, and when we remove one, you take it from the top. This means the last book we placed is always the first one we take out. That is the concept of LIFO — Last In First Out.
The basic 4 operations we can do on a stack are: 1. Push(): Adds a new element on the stack. 2. Pop(): Removes and returns the top element from the stack. 3. Peek(): Returns the top element on the stack. 4. isEmpty(): Checks if the stack is empty. 5. Size(): Finds the number of elements in the stack.
Type of implementing Stack Data Structure Using Array Data Structure: It is fixed size implementation where we can store the data up to a limit. It required predefined capacity. Using Linked List: It is a dynamic allocation where we can store the data variable times and the memory usage is much high as compared to array.
Application of Stack 1. Stack is the base about recursion. While a recursion function is called, a call stack is created in memory. Internally all the data during execution push at stack and order wise pop out from the stack. 2. It is used in evaluating the expressions like prefix, postfix, infix expression. 3. Backtracking algorithms, such as depth-first search (DFS) in graph theory, depend on the stack data structure to systematically explore paths and make choices at each step. 4. The stack plays a critical role in memory management, illustrating the application of stack ADT in data structure. It is used to store function call information, local variables, and execution-related data.
Implementation of Stack using array(Here we use java programming language): import java.util.*; public class Main { class Stack { int size = 10000; int arr[] = new int[size]; int top = -1; void push(int x) { top++; arr[top] = x; } int pop() { int x = arr[top]; top--; return x; } int top() { return arr[top]; } int size() { return top + 1; } public static void main(String args[]) { Stack s = new Stack(); s.push(6); s.push(3); s.push(7); System.out.println("Top of the stack before deleting any element " + s.top()); System.out.println("Size of the stack before deleting any element " + s.size()); System.out.println("The element deleted is " + s.pop()); System.out.println("Size of the stack after deleting an element " + s.size()); System.out.println("Top of the stack after deleting an element " + s.top()); } } }







