[Java数据结构]线性表之栈Stack

》》先进后出

》》代码

package 栈和队列;


class stackArr{
	
	private int maxSize;
	private long[] stackArr;
	private int top;
	
	
	public stackArr(int s){
		maxSize =s;
		stackArr = new long[maxSize];
		top=-1;
				
	}
	
	public void push(long j){//压栈 stackArr[++top] //先top+1,再输出
		top++;
		stackArr[top]=j;
	}
	
	public long pop(){//出栈
		
		return stackArr[top--];//先top再top-1
	}
	
	public long peek(){//返回顶点值
		return stackArr[top];
	}
	
	public boolean isEmpty(){
		
		return (top==-1);
		
		
	}
	public boolean ifFull(){
		return (top==maxSize-1);
	}
	
	
	
}






public class Stack {

	public static void main(String[] args) {
		stackArr arr= new stackArr(20);
		arr.push(20);
		arr.push(30);
		arr.push(50);
		arr.push(60);
		arr.push(70);
		
		while(!arr.isEmpty()){//!top==-1
			long value = arr.pop();
			System.out.print(value+" ");
			
			
		}
		
		
	}

}


你可能感兴趣的:(数据结构)