【Java】 大話數據結構(6) 棧的順序與鏈式存儲

 

本文根據《大話數據結構》一書,實現了Java版的棧的順序存儲結構、兩棧共享空間、棧的鏈式存儲機構java

:限定僅在表尾進行插入和刪除操做的線性表。node

棧的插入(進棧)和刪除(出棧)操做以下圖所示。數組

   

 

1.棧的順序存儲結構數據結構

  用數組存放數據,top變量來指示棧頂元素在數組中的位置(棧頂指針)。一個長度爲5的棧的示意圖以下:ide

  實現程序:函數

/**
 * 棧的順序儲存結構
 * 
 * 問題:構造器中,泛型數組建立是否有更好的方法?
 * @author Yongh
 *
 */
public class SqStack<E> {
	private E[] data;
	private int top;  //棧頂指針,top=-1時爲空棧
	private int maxSize;
	private static final int DEFAULT_SIZE= 10;
	
	public SqStack() {
		this(DEFAULT_SIZE);
	}
	public SqStack(int maxSize) {
		//沒法建立泛型數組 data=new E[maxSize];
		data=(E[]) new Object[maxSize];
		top=-1;
		this.maxSize=maxSize;
	}
	
	public void push(E e) {
		if(top==maxSize-1) 
			throw new RuntimeException("棧已滿,沒法進棧!");
		top++;
		data[top]=e;
	}
	
	public E pop() {
		if(top==-1)
			throw new RuntimeException("空棧,沒法出棧!");
		E e=data[top];
		top--;
		return e;
	}
	
	public void printStack() {
		if(top==-1) {
			System.out.println("空棧");			
		}else {
			for(int i=0;i<=top;i++) {
				System.out.println(data[i]);
			}
		}		
	}
}

  

  測試代碼:測試

public class StackTest {
	public static void main(String[] args) {
		SqStack<Student> sqStack=new SqStack<Student>();		
	    Student[] students= {new Student("小A",11),new Student("小B",12),new Student("小C",13),
	            new Student("小D",14),new Student("小E",151)};
	    for(int i=0;i<5;i++) {
	    	sqStack.push(students[i]);
	    }
	    sqStack.printStack();
	    for(int i=0;i<5;i++) {
	    	sqStack.pop();
	    }
	    sqStack.printStack();	    
	}       
}

class Student{
    public Student(String name, int age) {
        this.name=name;
        this.age=age;
    }
    String name;
    int age;
    public String toString() {
    	return name;
    }
}

  

小A
小B
小C
小D
小E
空棧
StackTest

 

2.兩棧共享空間this

  經過一個數組存放兩個棧,能較好地利用空間。用top1和top2變量表示棧1和棧2的棧頂指針,兩個棧的棧底分別位於數組的頭部和尾部。spa

  實現程序(在SqStack程序的基礎上稍加改造便可):3d

/** 
 * 棧的順序儲存結構(兩棧共享空間)
 * 
 * 注意點:棧滿條件爲top1+1==top2
 * 
 * @author Yongh
 *
 */
public class SqDoubleStack<E> {
	private E[] data;
	private int top1;  //棧1棧頂指針,top=-1時爲空棧
	private int top2;  //棧2棧頂指針,top=maxSize-1時爲空棧
	private int maxSize;
	private static final int DEFAULT_SIZE= 10;
	
	public SqDoubleStack() {
		this(DEFAULT_SIZE);
	}
	public SqDoubleStack(int maxSize) {
		//沒法建立泛型數組 data=new E[maxSize];
		data=(E[]) new Object[maxSize];
		top1=-1;
		top2=maxSize-1;
		this.maxSize=maxSize;
	}
	
	/*
	 * 進棧操做,stackNumber表明要進的棧號
	 */
	public void push(int stackNumber,E e) {		
		if(top1+1==top2) 
			throw new RuntimeException("棧已滿,沒法進棧!");
		if(stackNumber==1) {
			data[++top1]=e;			
		}else if(stackNumber==2) {
			data[--top2]=e;			
		}else {
			throw new RuntimeException("棧號錯誤!");
		}

	}
	
	/*
	 * 出棧操做
	 */
	public E pop(int stackNumber) {
		E e;
		if(stackNumber==1){
			if(top1==-1)
				throw new RuntimeException("空棧1,沒法出棧!");
			e=data[top1--];
		}else if(stackNumber==2) {
			if(top2==maxSize-1)
				throw new RuntimeException("空棧2,沒法出棧!");
			e=data[top2++];
		}else {
			throw new RuntimeException("棧號錯誤!");
		}
		return e;
	}	
}

  

3.棧的鏈式存儲結構

   經過單向鏈表實現的棧,棧頂放在單鏈表的頭部(注意進棧操做並非往鏈表的後面插入,而是從頭部插入)。

  鏈棧的示意圖以下。

  插入與刪除操做示意圖:

  

  實現程序:

/**
 * 
 * 棧的鏈式存儲結構
 * 
 * @author Yongh
 */
public class LinkStack<E> {
	private StackNode<E> top;
	private int count;
	
	private class StackNode<E>{
		E data;
		StackNode<E> next;
		public StackNode(E data,StackNode<E> next) {
			this.data=data;
			this.next=next;
		}
	}
	
	public LinkStack() {
		top=new StackNode<E>(null, null);
		count=0;
	}
	
	public void push(E e) {
		StackNode<E> node=new StackNode<E>(e, top);
		top=node;
		count++;
	}
	
	public E pop() {
		if(count==0)
			throw new RuntimeException("空棧,沒法出棧!");
		StackNode<E> node;
		node=top;
		top=top.next;
		count--;
		E e=node.data;
		node=null;
		return e;
	}
	
	public void printStack() {
		if(count==0) {
			System.out.println("空棧");			
		}else {
			StackNode<E> node=top;
			for(int i=0;i<count;i++) {
				System.out.println(node.data);
				node=node.next;
			}
		}		
	}
	
	/*
	 * 測試代碼
	 */
	public static void main(String[] args) {
		LinkStack<Student> linkStack=new LinkStack<Student>();		
	    Student[] students= {new Student("小A",11),new Student("小B",12),new Student("小C",13),
	            new Student("小D",14),new Student("小E",151)};
	    for(int i=0;i<5;i++) {
	    	linkStack.push(students[i]);
	    }
	    linkStack.printStack();	
	    System.out.println("----");
	    for(int i=0;i<5;i++) {
	    	System.out.println(linkStack.pop());
	    }
	    linkStack.printStack();	    
	}
}

  

小E
小D
小C
小B
小A
----
小E
小D
小C
小B
小A
空棧
LinkStack

 

 4.棧的應用

  (1)實現遞歸

    一些問題(如斐波那契數列的求解),可經過遞歸函數得到,而遞歸函數是由棧來實現的。

典型的斐波那契數列

  (2)四則運算表達式求值

    利用後綴表達式(逆波蘭表示法)結合棧能夠實現四則運算表達式的求解。並且經過棧,就能夠把咱們平時用的中綴表達式轉化爲後綴表達式。    

相關文章
相關標籤/搜索