码迷,mamicode.com
首页 > 编程语言 > 详细

java实现泛型栈

时间:2014-10-20 11:52:50      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:blog   http   ar   java   2014   on   log   代码   amp   

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实现泛型栈

标签:blog   http   ar   java   2014   on   log   代码   amp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!