ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```java package ds.impl; import ds.util.Node; import ds.Bag; import java.util.Iterator; import ds.util.ListIterator; public class LinkedBag<T> implements Iterable<T>, Bag<T> { private Node<T> first; private int size; public LinkedBag() { first = null; size = 0; } @Override public int size() { return size; } @Override public void add(T data) { Node<T> old = first; first = new Node<>(); first.data = data; first.next = old; size++; } @Override public String toString() { StringBuilder s = new StringBuilder(); for (T data : this) { s.append(data).append(' '); } return s.toString(); } @Override public Iterator<T> iterator() { return new ListIterator(first); } } ```