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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章