您的位置:首页 > 编程语言 > Java开发

java实现栈

2016-07-04 22:05 190 查看
/**
* Created by murphy on 2016/7/4.
*/
public class Stack {
private int DEFAULT_SIZE=12;
private int[] arr;
private int count=0;

public Stack(int size){
arr=new int[size];
}
public Stack(){
arr=new int[DEFAULT_SIZE];
}

public void push(int val){
arr[count++]=val;
}
public int pop(){
int temp=arr[count-1];
count--;
return temp;
}
public int peek(){
return arr[count-1];
}
public int size(){
return count;
}

public boolean isEmpty(){
return count == 0;
}
public static void main(String[] args){
Stack stack = new Stack();
stack.push(10);

stack.push(20);

stack.push(30);

System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.size());
System.out.println(stack.isEmpty());
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 算法 数据结构