diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ec4f15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +!*.java + diff --git a/list/.idea/uiDesigner.xml b/list/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/list/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/list/.idea/vcs.xml b/list/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/list/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/list/src/LinkApp.java b/list/src/LinkApp.java new file mode 100644 index 0000000..7770993 --- /dev/null +++ b/list/src/LinkApp.java @@ -0,0 +1,113 @@ +import lists.DSideLinkedList; +import lists.Iterator; +import lists.LinkedList; +import lists.List; + +public class LinkApp { + public static void main(String[] argv) { + // test LinkedList + testLinkedList(); + + // test DSideLinkedList + testDSideLinkedList(); + + // test Iterator + testIterator(); + } + + static void testLinkedList() { + System.out.println("------ Test LinkedList ------"); + + List list = new LinkedList(); + + list.insert("A", 12); + list.insert("B", 32); + list.insert("C", 2); + list.insert("D", 15); + list.insert("E", 7); + list.insert("F", 22); + list.insert("G", 77); + list.display(); + + System.out.println(); + + list.delete(); + list.display(); + + System.out.println(); + + list.delete("D"); + list.display(); + + System.out.println(); + + System.out.println(list.find("E")); + System.out.println(); + + System.out.println(list.find("D")); + System.out.println(); + } + + static void testDSideLinkedList() { + System.out.println("------ Test DSideLinkedList ------"); + + DSideLinkedList dlist = new DSideLinkedList(); + + dlist.insert("AA",11); + dlist.insert("BB",22); + dlist.insert("DD",33); + dlist.display(); + + System.out.println(); + + dlist.insertLast("EE",44); + dlist.insertLast("FF",55); + dlist.display(); + + System.out.println(); + + dlist.delete("EE"); + dlist.display(); + + System.out.println(); + } + + static void testIterator() { + System.out.println("------ Test Iterator ------"); + + List list = new LinkedList(); + Iterator iterator = new Iterator(list); + + System.out.println(iterator.getCurrent()); + + iterator.nextLink(); + System.out.println(iterator.getCurrent()); + + System.out.println(iterator.atEnd()); + + iterator.deleteCurrent(); + System.out.println(iterator.getCurrent()); + + iterator.reset(); + System.out.println(iterator.getCurrent()); + + System.out.println(); + + iterator.insertAfter("AA", 11); + iterator.insertAfter("BB", 22); + iterator.insertAfter("CC", 33); + iterator.insertAfter("DD", 44); + iterator.insertAfter("EE", 55); + list.display(); + + System.out.println(); + + iterator.reset(); + iterator.nextLink(); + iterator.nextLink(); + iterator.nextLink(); + iterator.insertBefore("XX", 0); + iterator.insertBefore("ZZ", 0); + list.display(); + } +} diff --git a/list/src/lists/DSideLinkedList.java b/list/src/lists/DSideLinkedList.java new file mode 100644 index 0000000..b0424a9 --- /dev/null +++ b/list/src/lists/DSideLinkedList.java @@ -0,0 +1,33 @@ +package lists; + +public class DSideLinkedList extends List { + + private Link last; + + @Override + public void insert(String name, int age) { + Link newLink = new Link(name, age); + + if (first == null) { + last = newLink; + } + else { + newLink.next = first; + } + + first = newLink; + } + + public void insertLast(String name, int age) { + Link newLink = new Link(name, age); + + if (last == null) { + first = newLink; + } + else { + last.next = newLink; + } + + last = newLink; + } +} diff --git a/list/src/lists/IIterator.java b/list/src/lists/IIterator.java new file mode 100644 index 0000000..2472863 --- /dev/null +++ b/list/src/lists/IIterator.java @@ -0,0 +1,18 @@ +package lists; + +interface IIterator { + + void reset(); + + void nextLink(); + + Link getCurrent(); + + boolean atEnd(); + + void insertAfter(String name, int age); + + void insertBefore(String name, int age); + + String deleteCurrent(); +} diff --git a/list/src/lists/Iterator.java b/list/src/lists/Iterator.java new file mode 100644 index 0000000..950be8b --- /dev/null +++ b/list/src/lists/Iterator.java @@ -0,0 +1,91 @@ +package lists; + +public class Iterator implements IIterator { + + private List list; + + private Link current; + private Link previous; + + public Iterator(List _list) { + list = _list; + + current = list.getFirst(); + previous = null; + } + + @Override + public void reset() { + current = list.getFirst(); + previous = null; + } + + @Override + public void nextLink() { + if (current == null) { + return; + } + + previous = current; + current = current.next; + } + + @Override + public Link getCurrent() { + return current; + } + + @Override + public boolean atEnd() { + return (current == null ? true : current.next == null); + } + + @Override + public void insertAfter(String name, int age) { + Link newLink = new Link(name, age); + + if (current == null) { + list.setFirst(newLink); + } + else { + previous = current; + + newLink.next = current.next; + current.next = newLink; + } + + current = newLink; + } + + @Override + public void insertBefore(String name, int age) { + Link newLink = new Link(name, age); + + if (current == null) { + list.setFirst(newLink); + } + else { + previous.next = newLink; + newLink.next = current; + } + + current = newLink; + } + + @Override + public String deleteCurrent() { + String name = null; + + if (current == null) { + return null; + } + + name = current.name; + + if (atEnd() != true) { + previous.next = current.next; + } + + return name; + } +} diff --git a/list/src/lists/Link.java b/list/src/lists/Link.java new file mode 100644 index 0000000..f5c62f4 --- /dev/null +++ b/list/src/lists/Link.java @@ -0,0 +1,20 @@ +package lists; + +class Link { + + String name; + int age; + + Link next; + + public Link(String _name, int _age) { + name = _name; + age = _age; + } + + @Override + public String toString() { + return String.format("Name: %s, Age: %d", name, age); + } +} + diff --git a/list/src/lists/LinkedList.java b/list/src/lists/LinkedList.java new file mode 100644 index 0000000..f421ed0 --- /dev/null +++ b/list/src/lists/LinkedList.java @@ -0,0 +1,15 @@ +package lists; + +public class LinkedList extends List { + + @Override + public void insert(String name, int age) { + Link newLink = new Link(name, age); + + if (first != null) { + newLink.next = first; + } + + first = newLink; + } +} diff --git a/list/src/lists/List.java b/list/src/lists/List.java new file mode 100644 index 0000000..4868969 --- /dev/null +++ b/list/src/lists/List.java @@ -0,0 +1,74 @@ +package lists; + +abstract public class List { + Link first; + + public List() { + } + + abstract public void insert(String name, int age); + + public Link delete() { + Link current = first; + + if (current != null) { + first = current.next; + } + + return current; + } + + public Link delete(String name) { + Link current = first; + Link prev = first; + + while (current != null) { + if (current.name.equals(name) == true) { + break; + } + + prev = current; + current = current.next; + } + + prev.next = current.next; + + return current; + } + + public Link find(String name) { + Link current = first; + + while (current != null) { + if (current.name.equals(name) == true) { + break; + } + + current = current.next; + } + + return current; + } + + public void display() { + Link current = first; + + while (current != null) { + System.out.println(current); + + current = current.next; + } + } + + public boolean isEmpty() { + return (first == null); + } + + Link getFirst() { + return first; + } + + void setFirst(Link link) { + first = link; + } +}