How Stack works : with java


Stack එකක් කියන්නෙ මොකක්ද ?  මේ blog post එකෙන් ඒ ගැන කතා කරමු.
  • What is a stack?
Allows access to only one data item; the last item inserted
If you remove this item, then you can access the next-to-last item inserted


  • what happens in a stack?
In a stack all insertions and deletions are made at one end (Top).
Insertions and deletions are restricted from the Middle and at the End of a Stack

Adding an item is called  Push
Removing an item is called  Pop


Elements are removed from a Stack in the reverse order of that in which the elements
      were inserted into the Stack
The elements are inserted and removed according to the Last-In-First-Out (LIFO) principle.



  • Here is the java program to learn the work of a stack

StackX.java##########################################################
//tutorials_by_sathsara
//www.sathsaratutorials.blogspot.com
//On_17th_july_2020

package stack;

public class StackX {
   
    private int maxSize ;        //set the size of the stack array
    private double[] stackArray;   
    private int top;            //top of the stack
    boolean empty;

    //constructor
    public StackX(int s) {
        maxSize = s;
        stackArray = new double[maxSize];
        top = -1;
    }

    //add items to the stack from 0th position
    public void push(double j)
    {
        if(top == maxSize -1)
            System.out.println("stack is full !");
        else
        {
            stackArray[++top] =j;
            System.out.println(" value "+ j +" enterd to the stack position " + top);
        }
           
    }

   
    //removing values from the top
    public double pop()
    {
        if(top == -1)
            return 99;
       
        else
            return stackArray[top--];
           
    }
   
    //check whether the stack is empty
    public boolean isEmpty()
    {
        if(top == -1 )
           empty = true;
       
        return   empty;

    }

    //very similar to the pop
    public double peek()
    {
        if(top == -1)
            return -99;
        else
            return stackArray[top];   
    }
}

---------------------------------------------------------------------------------------------------
StackApp.java#####################################################
//tutorials_by_sathsara
//www.sathsaratutorials.blogspot.com
//On_17th_july_2020


package stack;

public class StackApp {

    public static void main(String[] args) {
       
       
        StackX st = new StackX(5);
       
        st.push(10);    //push items to the stack
        st.push(20);
        st.push(30);
        st.push(40);
        st.push(50);
        //st.push(60);
       
       
        System.out.println();        //space line only
       
        while(!st.isEmpty())        //removing values from the stack until its empty
        {
            double val = st.pop();
            System.out.println("removing value "+ val);
           
        }
       
        while(!st.isEmpty())
        {
            double val = st.peek();
            System.out.println("removing value "+ val);
           
        }
    }

}





We will meet again.
Thank You!

check out my youtube channel :
https://www.youtube.com/channel/UCLD_EGJOhzPx4qu1OhOfeJg

Join with me on social media:
Social Media
[Instagram]
https://www.instagram.com/_sathsa_ra/

[LinkedIn ]

https://www.linkedin.com/in/thathsara-hewage🇱🇰-sathsara-43046b191


Unauthorized things may be subject to copy rights.
@thathsarahewage | SriLanka2020





Comments

Popular posts from this blog

How to setup Python Environment in your PC

C programming for beginners -Episode 02 | How to use printf

Start guide for android studio