Stack(棧)--棧的各種表現形式

介紹數字型棧結構

/**
 * 棧X數字型
 * */
public class StackX {
	
	private int maxSize;
	private long[] stackArrary;
	private int top;
	
	public StackX(int s){
		maxSize=s;
		stackArrary=new long[maxSize];
		top=-1;
	}
	
	public void push(long j){//入棧
		stackArrary[++top]=j;
	}
	
	public long pop(){//彈棧
		return stackArrary[top--];
	}
	
	public long peek(){//獲取指針的現在位置
		return stackArrary[top];
	}
	
	public boolean isEmpty(){//判斷棧空
		return (top==-1);
	}
	
	public boolean isFull(){//判斷棧滿
		return (top==maxSize-1);
	}
}



介紹字母型棧結構

/**
 * 棧Y字母型
 * */
public class StackY {
	
	private int maxSize;
	private char[] stackArrary;
	private int top;
	
	public StackY(int s){
		maxSize=s;
		stackArrary=new char[maxSize];
		top=-1;
	}
	
	public void push(char j){//入棧
		stackArrary[++top]=j;
	}
	
	public char pop(){//彈棧
		return stackArrary[top--];
	}
	
	public char peek(){//獲取指針的現在位置
		return stackArrary[top];
	}
	
	public boolean isEmpty(){//判斷棧空
		return (top==-1);
	}
	
	public boolean isFull(){//判斷棧滿
		return (top==maxSize-1);
	}
}


介紹表達式棧結構

/**
 * 表達式棧
 * */
public class StackZ {

	private int maxSize;
	private char[] stackArrary;
	private int top;
	
	public StackZ(int s){
		maxSize=s;
		stackArrary=new char[maxSize];
		top=-1;
		
	}
	
	public void push(char j){
		stackArrary[++top]=j;
	}
	
	public char pop(){
		return stackArrary[top--];
	}
	
	public char peek(){
		return stackArrary[top];
	}
	
	public boolean isEmpty(){
		return (top==-1);
	}
	
	public int size(){
		return top+1;
	}
	
	public char peekN(int n){
		return stackArrary[n];
	}
	
	public void displayStack(String s){
		System.out.print(s);
		System.out.print("stack (bootom-->top): " );
		for(int j=0;j<size();j++){
			System.out.print(peekN(j));
			System.out.print(' ');
		}
		System.out.println("");
	}
}



介紹後綴算式棧結構

/**
 * 後綴算式棧
 * */
public class StackW {

	private int maxSize;
	private int[] stackArrary;
	private int top;
	
	public StackW(int s){
		maxSize=s;
		stackArrary=new int[maxSize];
		top=-1;
		
	}
	
	public void push(int j){
		stackArrary[++top]=j;
	}
	
	public int pop(){
		return stackArrary[top--];
	}
	
	public int peek(){
		return stackArrary[top];
	}
	
	public boolean isEmpty(){
		return (top==-1);
	}
	
	public boolean isFull(){
		return (top==maxSize-1);
	}
	
	public int size(){
		return top+1;
	}
	
	public int peekN(int n){
		return stackArrary[n];
	}
	
	public void displayStack(String s){
		System.out.print(s);
		System.out.print("stack (bootom-->top): " );
		for(int j=0;j<size();j++){
			System.out.print(peekN(j));
			System.out.print(' ');
		}
		System.out.println("");
	}
	
}



只列舉出一個數字棧的實現類,代碼如下:

/**
 * 棧X實現
 * */
public class StackApp {
	
	public static void main(String[] args) {
		StackX theStack=new StackX(10);
		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(" ");
	}
	
	

}


發佈了26 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章