数据结构之栈

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;
			}
			
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值