import java.lang.*;
class Ideone
{
static interface Iterator<T> {
boolean hasNext();
T next();
}
static interface List<T> {
void add(T data);
void insertFirst(T data);
T removeFirst();
Iterator<T> iterator();
boolean isEmpty();
int getSize();
}
static interface Queue<T> {
T dequeue();
void enqueue(T data);
boolean isEmpty();
int getSize();
}
static interface Stack<T> {
T pop();
T peek();
void push(T data);
boolean isEmpty();
int getSize();
}
static class MyLinkedList<T> implements List<T>, Queue<T>, Stack<T> {
class ListStructure {
T data;
ListStructure next = null;
ListStructure(T data) { this.data = data; }
}
ListStructure head = null;
ListStructure tail = null;
int size = 0;
MyLinkedList() {}
public int getSize() { return size; }
public boolean isEmpty() { return size == 0; }
public void add(T data) {
ListStructure node = new ListStructure(data);
if (tail == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
++size;
}
public void insertFirst(T data) {
ListStructure node = new ListStructure(data);
if (tail == null) {
head = tail = node;
} else {
node.next = head;
head = node;
}
++size;
}
public T removeFirst() {
if (head == null) return null;
ListStructure node = head;
head = node.next;
--size;
return node.data;
}
public T dequeue() {
return removeFirst();
}
public void enqueue(T data) {
add(data);
}
public T pop() {
return removeFirst();
}
public T peek() {
if (head == null) return null;
return head.data;
}
public void push(T data) {
insertFirst(data);
}
public Iterator<T> iterator() {
return new Iterator<T>() {
private ListStructure node = head;
public boolean hasNext() {
return node != null;
}
public T next() {
if (node == null) return null;
ListStructure tmp = node;
node = tmp.next;
return tmp.data;
}
};
}
}
{
List<Integer> list1 = new MyLinkedList<Integer>();
List<String> list2 = new MyLinkedList<String>();
Queue<Integer> queue = new MyLinkedList<Integer>();
Stack<String> stack = new MyLinkedList<String>();
for (int i = 1; i <= 5; ++i) {
list1.
add(Integer.
valueOf(i
* i
+ 1)); list2.add("hoge[" + i + "] = " + (i * i));
stack.push("stk = " + i);
queue.
enqueue(Integer.
valueOf(1 << i
)); }
for (Iterator<Integer> itr = list1.iterator(); itr.hasNext(); ) {
System.
out.
println(itr.
next()); }
for (Iterator<String> itr = list2.iterator(); itr.hasNext(); ) {
System.
out.
println(itr.
next()); }
while (!queue.isEmpty()) {
System.
out.
println(queue.
dequeue()); }
while (!stack.isEmpty()) {
System.
out.
println(stack.
pop()); }
}
}