首页 > 其他 > 详细

数据结构之栈

时间:2020-12-22 23:43:37      阅读:36      评论:0      收藏:0      [点我收藏+]

数据结构之栈

//StackX.java

public class StackX {
    private int maxSize;        //size of stack array
    private long[] stackArray;
    private int top;

    public StackX(int s){
        maxSize = s;            //set array size
        stackArray = new long[maxSize]; //create array
        top = -1;               //no items yet
    }

    public void push(long j) {  //put item on top of stack
        stackArray[++top] = j;  //increment top,insert item
    }

    public long pop(){  //take item from top of stack
        return stackArray[top--];
    }

    public long peek(){     //peek at top of stack
        return stackArray[top];
    }

    public boolean isEmpty(){   //true if stack is empty
        return (top == -1);
    }

    public boolean isFull(){ //true if stack is full
        return (top == maxSize-1);
    }
}

//StackApp.java

public class StackApp {
    public static void main(String[] args) {
        StackX theStack = new StackX(10); //make new stack

        theStack.push(20);
        theStack.push(40);
        theStack.push(60);
        theStack.push(80);

        while (!theStack.isEmpty()){
            long value = theStack.pop();
            System.out.println(value);
            System.out.println(" ");
        }
        System.out.println(" ");
    }//end main
}

数据结构之栈

原文:https://www.cnblogs.com/cnqijian/p/14175920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!