Last active
October 7, 2016 01:25
-
-
Save jjlumagbas/9e828177727dc2939baec7e06cb6cc64 to your computer and use it in GitHub Desktop.
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 characters
| import java.util.*; | |
| public class LList implements MyList { | |
| private MyNode head; | |
| private int size; | |
| public LList() { | |
| size = 0; | |
| } | |
| public void add(Object item) { | |
| if (head == null) { | |
| head = new MyNode(item, null); | |
| } else { | |
| MyNode current = head; | |
| while (true) { | |
| if (current.getNext() == null) { | |
| break; | |
| } | |
| current = current.getNext(); | |
| } | |
| current.next = new MyNode(item, null); | |
| } | |
| size++; | |
| } | |
| public void add(int i, Object item) { | |
| } | |
| public Object get(int index) { | |
| if (index < 0 || index >= size()) { | |
| throw new IndexOutOfBoundsException(); | |
| } | |
| MyNode current = head; | |
| for (int i = 0; i < index; i++) { | |
| current = current.getNext(); | |
| } | |
| return current.getData(); | |
| } | |
| public void remove(int i) { | |
| } | |
| public void set(int i, Object item) { | |
| } | |
| public int size() { | |
| return size; | |
| } | |
| private class MyNode { | |
| private Object data; | |
| private MyNode next; | |
| public MyNode(Object d, MyNode n) { | |
| data = d; | |
| next = n; | |
| } | |
| public Object getData() { | |
| return data; | |
| } | |
| public MyNode getNext() { | |
| return next; | |
| } | |
| } | |
| public Iterator iterator() { | |
| return new LListIterator(head); | |
| } | |
| private class LListIterator implements Iterator { | |
| private MyNode current; | |
| public LListIterator(MyNode head) { | |
| current = head; | |
| } | |
| public Object next() { | |
| if (current == null) { | |
| throw new NoSuchElementException(); | |
| } | |
| Object data = current.getData(); | |
| current = current.getNext(); | |
| return data; | |
| } | |
| public boolean hasNext() { | |
| return current != null; | |
| } | |
| } | |
| } |
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 characters
| public class LListTest { | |
| public static void main(String[] args) { | |
| LList l = new LList(); | |
| l.add("A"); | |
| l.add("B"); | |
| l.add("C"); | |
| Iterator it = l.iterator(); | |
| System.out.println(it.next()); | |
| System.out.println(it.next()); | |
| System.out.println(it.next()); | |
| it.remove(); | |
| while (it.hasNext()) { | |
| System.out.println(it.next()); | |
| } | |
| for (Object el : l) { | |
| System.out.println(el); | |
| } | |
| } | |
| } |
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 characters
| public interface MyList extends Iterable { | |
| public void add(Object item); | |
| public void add(int i, Object item); | |
| public Object get(int i); | |
| public void remove(int i); | |
| public void set(int i, Object item); | |
| public int size(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment