欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

栈的基本认识

程序员文章站 2022-04-12 10:32:12
...
public class Stack{
	public static void main(String args[]){
		Stackx theStack = new Stackx(10);	
		
		theStack.push(12);
		theStack.push(13);
		theStack.push(14);
		theStack.push(15);
				
		while(!theStack.isEmpty()){
		    long value=theStack.pop();
		    System.out.print(value+" ");		    	
		}
		System.out.println();
	}
	
}
	
	
//栈  
class Stackx{
	//三个必须元素
	private int maxSize;
	private long[] stackArray;
	private int top;	
	
	//构造栈
	public Stackx(int s){
	 maxSize=s;
	 stackArray=new long[maxSize];
	 top=-1;//栈为空
	}
	
	//栈所具有的动作
	public void push(long j){
	stackArray[++top]=j;//创建顶层空间并插入值
	}		
	public long pop(){
	 return stackArray[top--];
	}
	public long peek(){
	 return stackArray[top];
	}			
	public boolean isEmpty(){
	 return (top==-1);
	}
	public boolean isFull(){
	 return (top==maxSize-1);
	}
		
}	

 

相关标签: J#