Created
September 17, 2012 13:44
-
-
Save tzafrir/3737339 to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ import java.lang.IndexOutOfBoundsException; public class Main { public static void main(String args[]) { LinkedList<Integer> l = new LinkedList<Integer>(); l.add(4); l.add(0); l.add(9); l.add(131, 1); l.add(12); l.add(151, 5); for (int i = 0; i < l.size(); ++i) { System.out.println(l.get(i)); } } } class LinkedList<T> { private class Node { public T data; public Node next = null; public Node(T data) { this.data = data; } } Node head = new Node(null); int size = 0; public LinkedList() { } public int size() { return size; } public boolean add(T e) { Node n = head; for (; n.next != null; n = n.next); n.next = new Node(e); ++size; return true; } public boolean add(T e, int i) { if (i < 0 || i > size) { throw new IndexOutOfBoundsException(); } if (i == size) { add(e); return true; } Node n = head; for (int j = 0; j < i; j++) { n = n.next; } Node next = n.next; n.next = new Node(e); n.next.next = next; ++size; return true; } public T get(int i) { if (i < 0 || i >= size) { throw new IndexOutOfBoundsException(); } Node n = head; for (int j = 0; j < i + 1; j++) { n = n.next; } return n.data; } }