package com.evs.objava33.class12;
public class LinkedList<T> {
private ListItem start, end, current;
public LinkedList() {
start = end = current = null;
}
public LinkedList(T item) {
addItem(item);
}
public T getFirst() {
current = start;
return start == null ? null : start.item;
}
public T getNext() {
if (current != null) {
current = current.next;
}
return current == null ? null : current.item;
}
public void addItem(T[] items) {
if (items != null) {
for (T s : items) {
addItem(s);
}
}
}
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Linked List: ");
for (T item = getFirst(); item != null; item = getNext()) {
buf.append(item);
buf.append(",");
}
return buf.toString();
}
public void addItem(T item) {
ListItem newItem = new ListItem(item);
if (start == null) {
start = end = newItem;
} else {
end.next = newItem;
end = newItem;
}
}
private class ListItem {
T item;
ListItem next;
public ListItem(T item) {
this.item = item;
}
@Override
public String toString() {
return "ListItem [item=" + item + "]";
}
}
}
No comments:
Post a Comment