數據結構之棧

1.數組實現棧結構

package stack;

public class ArrayStack {
	private String[] items;//數組
	private int n;//棧的大小
	private int count;//棧中元素個數
	
	//初始化數組,申請一個大小爲n的數組空間
	public ArrayStack(int capacity){
		this.items=new String[capacity];
		this.n=capacity;
		this.count=0;
	}
	
	//入棧操作
	public boolean push(String item){
		//數組空間不足,直接返回false,入棧失敗
		if(count==n)
			return false;
		// 將 item 放到下標爲 count 的位置,並且 count+1
		items[count]=item;
		++count;
		return true;
	}
	
	//出棧操作
	public String pop(){
		// 棧爲空,則直接返回 null
		if(count==0){
			return null;
		}
		// 返回下標爲 count-1 的數組元素,並且棧中元素個數count-1
		String tmp=items[count-1];
		--count;
		return tmp;
	}
}

2.鏈表實現棧結構

package stack;

/**
 * 基於鏈表實現棧
 *
 */
public class StackBasedLinkedList {
	private Node top=null;
	
	public static void main(String[] args) {
		StackBasedLinkedList list=new StackBasedLinkedList();
		list.push(0);
		list.push(1);
		list.push(2);
		list.push(3);
		list.printAll();
		list.pop();
		list.printAll();
	}
	
	//入棧
	public void push(int value){
		Node newNode=new Node(value,null);
		//判斷是否棧空
		if(top==null){
			top=newNode;
		}else{
			newNode.next=top;
			top=newNode;
		}
	} 
	//出棧
	public int pop(){
		//如果沒數據
		if(top==null)
			return -1;
		int value=top.data;
		top=top.next;//替代當前Top
		return value;
	}
	
	public void printAll(){
		Node p=top;
		while(p!=null){
			System.out.println(p.data+" ");
			p=p.next;
		}
		System.out.println();
	}
	
	private static class Node{
		private int data;
		private Node next;
		
		public Node(int data,Node next){
			this.data=data;
			this.next=next;
		}
		public int getData(){
			return data;
		}
	}
}	

3.利用棧結構來實現瀏覽器的前進和後退功能

package stack;

/**
 * 使用前後棧實現瀏覽器的前進後退
 *
 */
public class SampleBrowser {
	
	public static void main(String[] args) {
		SampleBrowser browser=new SampleBrowser();
		browser.open("http://www.baidu.com");
		browser.open("http://news.baidu.com/");
		browser.open("http://news.baidu.com/ent");
		browser.goBack();//news.baidu.com/
		browser.goBack();//www.baidu.com
		browser.goForward();//news.baidu.com/
		browser.open("http://www.qq.com");
		browser.goForward();//can't go forward
		browser.goBack();//news.baidu.com/
		browser.goForward();//www.qq.com
		browser.goBack();//news.baidu.com/
		browser.goBack();//www.baidu.com
		browser.goBack();//can't go back
		browser.goBack();//can't go back
		browser.checkCurrentPage();//www.baidu.com
		
	}
	
	private String currentPage;
	private LinkedListBasedStack backStack;
	private LinkedListBasedStack forwardStack;
	
	
    public SampleBrowser() {
    	this.backStack=new LinkedListBasedStack();
    	this.forwardStack=new LinkedListBasedStack();
	}

    public void open(String url){
    	if(this.currentPage!=null){
    		this.backStack.push(currentPage);
    		this.forwardStack.clear();
    	}
    	showUrl(url, "Open");//賦值currentPage
    }

    public boolean canGoBack(){
    	return backStack.size()>0;
    }
    
    public boolean canGoForward(){
    	return forwardStack.size()>0;
    }
    
    public String goBack(){
    	if(this.canGoBack()){
    		this.forwardStack.push(currentPage);
    		String backUrl=backStack.pop();
    		showUrl(backUrl, "Back");
    		return backUrl;
    	}
    	System.out.println("* Cannot go back, no pages behind.");
    	return null;
    }
    public String goForward(){
    	if(this.canGoForward()){
    		this.backStack.push(currentPage);
    		String forwardUrl=forwardStack.pop();
    		showUrl(forwardUrl, "Forward");
    		return forwardUrl;
    	}
    	System.out.println("* Cannot go forward, no pages ahead.");
    	return null;
    }
    
    public void showUrl(String url,String prefix){
    	this.currentPage=url;
    	System.out.println(prefix+" page=="+url);
    }
    
    public void checkCurrentPage(){
    	System.out.println("Current page is:"+this.currentPage);
    }
    
	/**
     * A LinkedList based Stack implementation.
     */
	public static class LinkedListBasedStack{
		private int size;
		private Node top;
		
//		public static void main(String[] args) {
//		  LinkedListBasedStack stack = new LinkedListBasedStack();
//		  stack.push("A");
//		  stack.push("B");
//		  stack.push("C");
//		  stack.pop();
//		  stack.push("D");
//		  stack.push("E");
//		  stack.pop();
//		  stack.push("F");
//		  stack.print();
//		
//		  String data = stack.getTopData();
//		  System.out.println("Top data == " + data);
//		}
		
		public static Node createNode(String data,Node next){
			return new Node(data,next);
		}
		
		public void clear(){
			top=null;
			this.size=0;
		}
		
		public int size(){
			return this.size;
		}
		
		public void push(String data){
			Node node=createNode(data, top);
			top=node;
			size++;
		}
		
		public String pop(){
			Node popNode=this.top;
			if(popNode==null){
				System.out.println("Stack is empty");
				return null;
			}
			top=popNode.next;
			if(size>0)
				size--;
			return popNode.data;
		}
		
		public String getTopData(){
			if(top!=null){
				return top.data;
			}else{
				return null;
			}
		}
		
		public void print(){
			System.out.println("Print Stack:");
			Node currentNode=this.top;
			while(currentNode!=null){
				System.out.print(currentNode.data+"\t");
				currentNode=currentNode.next;
			}
			System.out.println();
		}
		
		public static class Node{
			private String data;
			private Node next;
			public Node(String data){
				this(data,null);
			}
			public Node(String data,Node next){
				this.data=data;
				this.next=next;
			}
			public String getData() {
				return data;
			}
			public void setData(String data) {
				this.data = data;
			}
			public Node getNext() {
				return next;
			}
			public void setNext(Node next) {
				this.next = next;
			}
			
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章