What is Stack | Stack Implementation using Array : Java2021 - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Thursday, July 16, 2020

What is Stack | Stack Implementation using Array : Java2021

What is Stack and Implementation of Stack using Array
In this article we will discuss about stack
  • What is stack?
  • What is the principal of stack?
  • Some application of stack?
  • How to insert data?
  • How to remove data and many operations?
What is a stack? 
Stack is a linear data structure.
Explaining : Yes, we can say that when we keep a book from the posts of a book, then even if we keep a book above it, it becomes a stack, then this is what we call the stack. If we lift from above, now it is not that we can lift the book from the middle, this all phenomena are stacking.

Principal :  
when we insert a value into a stack and removing a value from stack then the principal work last in first out means the principle of stack is LIFO

Operations
  • Insert a value into Stack (PUSH)
  • Removing the value from Stack (POP)
  • Peek the value from Stack (PEEK)
Explaining: What is PUSH, POP, PEEK 
- The first operation is push simply push means to insert a data into stack this is called a push.
- The second operation is pop simply pop means remove the element from stack.
- Last operation is peak simply pick means return the value of the top of the stack this is called peak.

Implementation:
  • Using Array
  • Using Dynamic Array
  • Using Linked List
 And now we will discuss more methods in stack like stack is empty stack is full, the size of stack.
  • Inserting()
  • Deleting()
  • isEmptyStack
  • isFullStack
  • sizeStack()

We will discuss about programs : 

class Stack{
    int capacity;
    int top;
    int arr[];
    public Stack(){
        top=-1;
        capacity=5;
        arr=new int[capacity];
    }
    public boolean isEmpty(){
        return top==-1;
    } //check stack is empty
    public boolean isFull(){
        return top==capacity-1;
    } //check stack is full
    public int push(int data){ // insert data into stack
        if (isFull()) {
            System.out.println("stack is full");
        }
        return arr[++top] = data;
    }
    public int pop(){ //remove data into stack
        if (isEmpty()){
            System.out.println("stack is empty");
        }
            return arr[top--];
        }
    public int peek(){   //return top value of stack
        return arr[top];
    }
    public void printStack(){   // print value of stack using for each loop
        for (int a:arr){
            System.out.print(a+" ");
        }
    }
}
public class StackUsingArray {  //implementation of stack using array
    public static void main(String[] args) {
        Stack st=new Stack();
        st.push(10);
        st.push(20);
        st.push(30);
        st.push(40);
        st.printStack(); //print stack
        System.out.println(st.pop()); // remove 40 from top
        st.printStack(); // print stack
        //System.out.println();
    }
}
Output : 
// last 40 value show we pop the element from stack





Real Life Example of Stack:
  • Reverse a number
  • Undo in browser text editors and many more
  • Wearing and removing bangles
  • And this is also a stack:plates/books in a cupboard
  • And many more application of stacks

Recommended Topics for Freshers :
  • Polish Notation (Infix,Prefix,Postfix)
  • List (Linked List,Doubly,Circular,Doubly Circular)
  • Queue (DS Queue,Circular Queue)
  • Tree (Binary Tree,BST,AVL Tree,B+ Tree,Threaded Binary Tree)
  • Graph ( DFS,BFS,Spaning)
  • Searching ( Linear,Binary)
  • Sorting (Binary Sort,Merge Sort,Quick Sort etc).
Sorting and Searching Programs in Java
Start with your Choice
-Important Programs for Freshers
Learn Pattern Programs 
Click to learn Array Programs
Click to Learn MySQL 
for any complaint regarding my Blog please visit contact us page and write what problem you have!


No comments:

Pages