import java.util.Iterator;
public vlass ResizingArrayStack<Item> implements Iterable<Item>
{
private Item[] a=(Item[])new object[1]; //栈元素
private int N=0;
public boolean isEmpty(){return N==0;}
public int size(){return N;}
private void resize(int max)
{
Item[] temp=(Item[])new Object[max];
for(int i=0;i<N;i++
temp[i]=a[i];
a=temp;
}
public void push(Item item)
{
if(n==a.length)resize(2*s.length);
a[n++]=item;
}
public Item pop()
{
Item item=a[--N];
a[N]=null;
if(N>0&&N==a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item>iterator()
{
return new ReverseArrayIterator();}
private class ReverseArrayIterator implements Iterator<Item>
{
private int i=N;
public boolean hasNext(){return i>0;}
public Item next(){return a[--i];}
public void remove(){}
}
}
public class Stack<Item> implements Iterable<Item>
{
private Node first;
private int N;
private class Node
{
Item item;
Node next;
}
public boolean isEmpty()
{
return first==null;
}
public int size() {return N;}
public void push(Item item)
{
//向栈顶添加元素
Node oldfirst=first;
forst=new Node();
first.item=item;
first.next=oldfirst;
N++;
}
public Item pop()
{
//从栈顶删除元素
Item item=first.item;
first=first.next;
N--;
return item;
}
}
public class Queue<Item> implements Iterable<Item>
{
private Node first;
private Node last;
private int N;
private class Node
{
Item item;
Node next;
}
public boolean isEmpty()
{
return first==null;
}
public int size() {return N;}
public void push(Item item)
{
//向表尾添加元素
Node oldlast=last;
last=new Node();
last.item=item;
last.next=null;
if(isEmpty())first=last;
else
oldlast.next=last;
N++;
}
public Item pop()
{
//从栈顶删除元素
Item item=first.item;
first=first.next;
if(isEmpty())
last=null;
N--;
return item;
}
}
public class Bag<Item> implements Iterable<Item>
{
private Node first;
private class Node
{
Item item;
Node next;
}
public boolean isEmpty()
{
return first==null;
}
public int size() {return N;}
public void add(Item item)
{
//和stack的push()方向完全相同
Node oldfirst=first;
first=new Node();
first.item=item;
first.next=oldfirst;
N++;
}
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;
}
}
}