Giter Club home page Giter Club logo

stack-java's Introduction

This is stack code in java

related to my blog I made this file to everybody can use it

Usage

You can use this in any IDE you want like Eclips, VSCode, Visual Studeo, ... if you want to run this in terminal you should first install Java in your computer! Compile the code and run it like this:

$ javac Main.java
$ java Main

Code

class Main{

    public static void main(String [] args){

        int[] stack = testStack();
        for (int i : stack) {
            System.out.println(i);
        }
    }
 
    
    public static int[] testStack(){

        Stack stack = new Stack(5);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.pop();
        stack.push(5);
        stack.pop();
        stack.push(6);
        stack.pop();
        stack.push(7);
        stack.pop();

        return stack.getStack();
    }




    /**
     * Innerface Stack
     */
    public interface StackInterFace {
    
        public boolean push (int item); // add item to stack
        public int pop();       // remove the last item and push back 
        public void stackIsFull();      // when stack is full call
    }

    public static class Stack implements StackInterFace {

        private int length = 0; // length of stack
        

        private int[] stack;
        private int top = -1;


        // constactor
        public Stack (int length ){
            this.length = length;
            stack = new int[length];
        }

        //gettr for stack
        public int[] getStack(){
            return stack;
        }


        @Override
        public boolean push(int item) {
            if (top >= length-1 ){
                stackIsFull();
                return false;
            }
            top++;
            stack[top] = item;
            return true;
        }

        @Override
        public int pop() {
            if (top==-1){
                return -1;
            }
            int item = stack[top];
            stack[top] = 0;
            top--;
            return item;// first return the top and then  (top -1 )
        }

        @Override
        public void stackIsFull() {
            System.err.println("Stack is full.");
        }

    }
}

Enjoy

stack-java's People

Contributors

mahdirahmani80 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.