import java.io.*;

class ListElem implements Externalizable {
	int i;
	ListElem next;
	ListElem(int i, ListElem next) {
		this.i = i;
		this.next = next;
	}
	public ListElem() {}

	public void readExternal(ObjectInput o) throws IOException, ClassNotFoundException {
		// Sytem.out.println("readExternal");
		i = o.readInt();
		next = (ListElem) o.readObject();
	}

	public void writeExternal(ObjectOutput o) throws IOException {
		// Sytem.out.println("writeExternal");
		o.writeInt(i);
		o.writeObject(next);
	}
}	

class List implements Serializable {
	ListElem testa;

	void insTesta(int i) {
		testa = new ListElem(i, testa);
	}

	public String toString() {
		String s = "";
		ListElem scorri = testa;
		while (scorri != null) {
			s += scorri.i + " ";
			scorri = scorri.next;
		}
		return s;
	}
}

class ScriviLista {
	public static void main(String[] args) {
		PrintWriter p = new PrintWriter(System.out, true);
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(
					new FileOutputStream("list.dat"));

			List l = new List();

			for (int i = 0; i < 10; i++)
				l.insTesta(i);

			oos.writeObject(l);

		} catch (IOException e) {
			p.println(e.getMessage());
		} finally {
			try {
				if (oos != null) oos.close();
			} catch (IOException e) {
			}
		}
	}
}

class LeggiLista {
	public static void main(String[] args) {
		PrintWriter p = new PrintWriter(System.out, true);
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(
					new FileInputStream("list.dat"));

			List l = (List) ois.readObject();

			p.println(l);
		} catch (Exception e) {
			p.println(e.getMessage());
		} finally {
			try {
				if (ois != null) ois.close();
			} catch (IOException e) {
			}
		}
	}
}
