码迷,mamicode.com
首页 > 其他好文 > 详细

下压堆栈(链表实现)

时间:2018-04-27 21:40:37      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:添加元素   imp   asn   删除   move   empty   AC   current   ret   

import java.util.Iterator;
public class Stack<Item>
{
    private Node first;   //栈顶
    private int N;        //元素数量
    private class Node
    {   //定义节点的嵌套类
        Item item;
        Node next;
    }
    public boolean isEmpty() {  return first == null; } // 或: N == 0
	public int size() {  return N; }
	public void push(Item item)
	{	// 向栈顶添加元素
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		N++;
	}
	public Item pop()
	{	//从栈顶删除元素
		Item item = first.item;
		first = first.next;
		N--;
		return item;
	}
	public Iterator<Item> iterator()
	{	return new ListIterator();	}
	private class ListIterator implements Iterator<Item>
	{
		private Node current = first;
		public boolean hasNext()
		{	return current != null;	}
		public void remove() { }
		public Item next()
		{
			Item item = current.item;
			current = current.next;
			return item;
		}
	}       
}

  

下压堆栈(链表实现)

标签:添加元素   imp   asn   删除   move   empty   AC   current   ret   

原文地址:https://www.cnblogs.com/auhz/p/8964570.html

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