Stack Data Structure
Stack Data Structure
Stack data structure is defined as the data structure in which the operation are performed based on LIFO principle i.e. Last In First Out. And it is collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle. The LIFO principle state that, the last element inserted inside the stack is removed they first.
Working of Stack
Above example size of stack is 5. Lets supposed this stack is empty and we have taken the stack size is 5 and then pushing the elements one by one until the stack becomes full.
When the stack is full the size of stack is 5. In above figure element goes from top to the bottom when we entering the new element. and stack filled up form bottom to top.
When we performed the delete operation on given stack, there is only one opening for entry and exit.Stack operation follows the LIFO principle so,here 5 is entered the first so, it will removed first from the given stack.
Basic Operations of Stack
PUSH:
Enter the item at top of the stack
POP:
Remove the item from top of the stack.
To check which element is at the top of the stack additional primitived can be defined as:
IsEmpty:
Reports whether the stack is Empty
IsFull:
Repots whether stack is Full
Initialize:
Creates/Initializes the stack
Destroy:
Deletes the contents of the stack
Analysis of Stack Operations
- Push Operation : O(1)
- Pop Operation : O(1)
- Top Operation : O(1)
- Search Operation : O(n)
Algorithm of PUSH operation on stack
Step 1: If Top=Max-1 Print “Overflow : Stack is full” and Exit End If Step 2: Top=Top+1 Step 3: Stack[TOP]=Element Step 4: End
Algorithm of POP operation on stack
Step 1: If TOP=-1 Print “Underflow: Stack is empty” and Exit End if Step 2: Set Del_element=Stack[Top] Step 3: Top=Top-1 Step 4: Del_Element Step 5: End
Program for Stack in C
#include <limits.h> #include <stdio.h> #include <stdlib.h> // A structure of stack struct Stack { int top; unsigned capacity; int* array; }; // function for create a stack struct Stack* createStack(unsigned capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; } // Stack is full int isFull(struct Stack* stack) { return stack->top == stack->capacity - 1; } // Stack is empty int isEmpty(struct Stack* stack) { return stack->top == -1; } // Function for add item to stack and increases top by 1 void push(struct Stack* stack, int item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to stack\n", item); } // Function for remove an item from stack and decreases top by 1 int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top--]; } // Function for return the top int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top]; } // code int main() { struct Stack* stack = createStack(100); push(stack, 1); push(stack, 2); push(stack, 3); printf("%d popped from stack\n", pop(stack)); return 0; }
Output
1 pushed into stack 2 pushed into stack 3 pushed into stack 3 Popped from stack Top element is : 2
Program for Stack in C++
#include <bits/stdc++.h> using namespace std; #define MAX 1000 class Stack { int top; public: int a[MAX]; Stack() { top = -1; } bool push(int x); int pop(); int peek(); bool isEmpty(); }; bool Stack::push(int x) { if (top >= (MAX - 1)) { cout << "Stack is Overflow"; return false; } else { a[++top] = x; cout << x << " pushed into stack\n"; return true; } } int Stack::pop() { if (top < 0) { cout << "Stack is Underflow"; return 0; } else { int x = a[top--]; return x; } } int Stack::peek() { if (top < 0) { cout << "Stack is Empty"; return 0; } else { int x = a[top]; return x; } } bool Stack::isEmpty() { return (top < 0); } // code int main() { class Stack s; s.push(1); s.push(2); s.push(3); cout << s.pop() << " Popped from stack\n"; //print all Items cout<<"Items present in stack : "; while(!s.isEmpty()) { cout<<s.peek()<<" "; s.pop(); } return 0; }
Output
1 pushed into stack 2 pushed into stack 3 pushed into stack 3 Popped from stack Items present in stack : 2 1
Program for Stack in Python
class Stack { static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size boolean isEmpty() { return (top < 0); } Stack() { top = -1; } boolean push(int x) { if (top >= (MAX - 1)) { System.out.println("Stack is Overflow"); return false; } else { a[++top] = x; System.out.println(x + " pushed into stack"); return true; } } int pop() { if (top < 0) { System.out.println("Stack is Underflow"); return 0; } else { int x = a[top--]; return x; } } int peek() { if (top < 0) { System.out.println("Stack is Underflow"); return 0; } else { int x = a[top]; return x; } } void print(){ for(int i = top;i>-1;i--){ System.out.print(" "+ a[i]); } } } // Driver code class Main { public static void main(String args[]) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); System.out.println(s.pop() + " Popped from stack"); System.out.println("Top Item is :" + s.peek()); System.out.print("Items present in stack :"); s.print(); } }
Output
1 pushed into stack 2 pushed into stack 3 pushed into stack 3 Popped from stack Top Item is : 2 Items present in stack : 2 1
Applications of Stack Data Structure
- Expression Conversion
- Expression evaluation
- Parsing well formatted parenthesis
- Decimal to Binary Conversation
- Reversing a string
Advantages of Stack
- Efficient data management:
- Efficient management of functions
- Control over memory
- Smart memory management
- Not easily corrupted
- Does not allow resizing of variables
Disadvantage of Stack
- Limited memory size
- Chances of stack overflow
- Random access is not possible
- Unreliable
- Undesired termination
Read more, Divide and Conquer Algorithm