首页 > 编程语言 > 详细

java实现泛型栈

时间:2014-10-20 11:51:29      阅读:235      评论:0      收藏:0      [点我收藏+]
package com.test.common;


public class LinkedStack<T> {

	private class Node<U>{
		U item;
		Node<U> next;
		Node(){item=null;next=null;}
		Node(U item,Node<U> next)
		{
			this.item=item;
			this.next=next;
		}
		boolean end()
		{
			return item==null && next==null;
		}
	}
	private Node<T> top=new Node<T>();
	public void push(T item)
	{
		top=new Node<T>(item,top);
	}
	public T pop()
	{
		T result=top.item;
		if(!top.end())
		{
			top=top.next;	
		}
		return result;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedStack<String> stack=new LinkedStack<String>();
		stack.push("jianghuiwen");
		stack.push("wangjun");
		
		System.out.println(stack.pop());
	}

}

代码如上所示,创建了一个泛型的栈,并且提供pop和push两种操作方法。输出结果如图

bubuko.com,布布扣

java实现泛型栈

原文:http://blog.csdn.net/itbuluoge/article/details/40296709

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!