gistfile1.txt /* 栈先进后出*/public class Stack { private Node first; private int N; private class Node{ Iteam item; Node next; } public boolean isEmpty(){ return first == null; } public int size(){ return N; } public void push(Iteam i
/* 栈先进后出 */ public class Stack{ private Node first; private int N; private class Node{ Iteam item; Node next; } public boolean isEmpty(){ return first == null; } public int size(){ return N; } public void push(Iteam item){ Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst.item; N++; } public Iteam pop(){ Iteam item = first.item; first = first.next; N--; return item; } } /* 队列先进先出 */ public class Queue { private Node first; private Node last; private int N; private class Node{ Iteam item; Node next; } public boolean isEmpty(){ return first == null; } public int size(){ return N; } public void enqueue(Iteam item){ Node oldlast = last; last = new Node(); last.item = item; last.next = null; if(isEmpty()) //如果队列为空则把尾结点(唯一的节点)作为头节点 first = last; else oldfirst.next = last; N++; } public Iteam dequeue(){ Iteam item = first.item; first = first.next; if(isEmpty()) last = null; N--; return item; } } /* 背包不关注顺序不支持单个删除 */ import java.util.Iterator; //导入迭代接口 public class Bag implements Iterable { private Node first; private class Node{ Iteam item; Node next; } public void add(Iteam item){ Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; } public Iterator Iterator(){ return new listIterator(); } private class listIterator implements Iterator { private Node current = first; public boolean hasNext(){ return current != null; } public void remove(){} public Iteam next(){ Iteam item = current.item; current = current.next; return item; } } }