/** To test if deleting the node following the current node */ @Test public void testDeleteNext() { // delete the node after the current node filledTest.deleteNext(filledTest.getFirstNode()); // the e should have been deleted assertEquals("tstString", filledTest.toString()); }
/** To test if deleting the first node */ @Test public void testDeleteFirst() { // delete the first node filledTest.deleteFirst(); // the first letter should have been deleted assertEquals("estString", filledTest.toString()); }
public static void main(String[] args) { DoublyLinkedList<Number> doublyLinkedList = new DoublyLinkedList<>(); for (int i = 0; i < 1000000; i++) { doublyLinkedList.add(i); } LinkedList<Number> numbers = new LinkedList<>(); for (int i = 0; i < 1000000; i++) { numbers.add(i); } Timer timer = new Timer(); System.out.println("Addition/remove to/from the beggining test"); System.out.println("=============================="); timer.start(); doublyLinkedList.add(0, 45); System.out.println("DoublyLinkedList.add(e): " + timer.getElapsedTime()); timer.start(); numbers.add(0, 45); System.out.println("LinkedList.add(e): " + timer.getElapsedTime()); System.out.println("------------"); timer.start(); doublyLinkedList.remove(0); System.out.println("DoublyLinkedList.remove(e): " + timer.getElapsedTime()); timer.start(); numbers.remove(0); System.out.println("LinkedList.remove(e): " + timer.getElapsedTime()); System.out.println("=============================="); System.out.println("Addition/remove to/from the middle test"); System.out.println("=============================="); timer.start(); doublyLinkedList.add(500000, 45); System.out.println("DoublyLinkedList.add(e): " + timer.getElapsedTime()); timer.start(); numbers.add(500000, 45); System.out.println("LinkedList.add(e): " + timer.getElapsedTime()); System.out.println("------------"); timer.start(); doublyLinkedList.remove(500000); System.out.println("DoublyLinkedList.remove(e): " + timer.getElapsedTime()); timer.start(); numbers.remove(500000); System.out.println("LinkedList.remove(e): " + timer.getElapsedTime()); System.out.println("=============================="); System.out.println("Addition/remove to/from the end test"); System.out.println("=============================="); timer.start(); doublyLinkedList.add(doublyLinkedList.size() - 1, 222); System.out.println("DoublyLinkedList.add(e): " + timer.getElapsedTime()); timer.start(); numbers.add(numbers.size() - 1, 222); System.out.println("LinkedList.add(e): " + timer.getElapsedTime()); System.out.println("------------"); timer.start(); doublyLinkedList.remove(doublyLinkedList.size() - 1); System.out.println("DoublyLinkedList.remove(e): " + timer.getElapsedTime()); timer.start(); numbers.remove(numbers.size() - 1); System.out.println("LinkedList.remove(e): " + timer.getElapsedTime()); }
public Iterable<DirectedEdge> edges() { DoublyLinkedList<DirectedEdge> list = new DoublyLinkedList<DirectedEdge>(); for (int v = 0; v < V; v++) { for (DirectedEdge e : adj(v)) { list.add(e); } } return list; }
/** To test getLast this means that getLastNode also works */ @Test public void testLast() { // the last letter is "g" assertEquals("g", filledTest.getLast()); assertEquals("e", twoTest.getLast()); assertEquals("t", oneTest.getLast()); // The get last is funky for the empty test and I have no clue why!!! // assertEquals ("", emptyTest.getLast()); }
/** To test getFirst this means that getFirstNode also works */ @Test public void testFirst() { // the first letter is "t" assertEquals("t", filledTest.getFirst()); // the first letter is t assertEquals("t", twoTest.getFirst()); assertEquals("t", oneTest.getFirst()); // The get first is funky for the empty test and I have no clue why!!! // assertEquals ("null", emptyTest.getFirst()); }
public static void main(String[] args) { DoublyLinkedList linkedList = new DoublyLinkedList(); linkedList.print(); linkedList.add(1, 1); linkedList.add(2, 2); linkedList.print(); linkedList.remove(1); linkedList.print(); linkedList.add(3, 2); linkedList.add(4, 3); linkedList.print(); linkedList.reversePrint(); /*linkedList.print(); linkedList.reverseWithIteration(); linkedList.print(); linkedList.reverseWithRecursion(linkedList.getHead()); linkedList.print(); System.out.println("Printed using recursion List: "); linkedList.printWithRecursion(linkedList.getHead()); System.out.println("Print list reverse recursively without actually reversing the list \nList: "); linkedList.printReverseWithRecursion(linkedList.getHead());*/ /*linkedList.remove(1); linkedList.print(); linkedList.remove(3); linkedList.print();*/ }
/** To test if able to get size of the list */ @Test public void testSize() { // the size of "testString" is 10 letters, i.e. 10 nodes assertEquals(10, filledTest.size()); // the size of twoTest is 2 assertEquals(2, twoTest.size()); // the size of oneTest is 1 assertEquals(1, oneTest.size()); // the size of the emptyTest is zero assertEquals(0, emptyTest.size()); }
/** To test if the list is empty */ @Test public void testEmpty() { // the list is not empty because @before we made a LL and filled it assertEquals(false, filledTest.isEmpty()); // the list is not empty because @before we made a LL and filled it assertEquals(false, twoTest.isEmpty()); // the list is not empty because @before we made a LL and filled it assertEquals(false, oneTest.isEmpty()); // the list is empty because we made an empty LL assertEquals(true, emptyTest.isEmpty()); }
/** To test if inserting at the end */ @Test public void testInsertLast() { // insert a "Z" after the last node filledTest.insertLast("Z"); // twoTest.insertAfter(twoTest.getLastNode(),"Z"); // oneTest.insertAfter(oneTest.getLastNode(),"Z"); // I get a null pointer // emptyTest.insertAfter(emptyTest.getLastNode(),"Z"); // the last node should now hold "Z" assertEquals("Z", filledTest.getLastNode().toString()); // assertEquals ("Z", twoTest.getLastNode().toString()); // assertEquals ("Z", oneTest.getLastNode().toString()); }
/** To test if inserting after a node works */ @Test public void testInsertAfter() { // insert a Z at the spot after the head filledTest.insertAfter(filledTest.getFirstNode(), "Z"); // insert a Z at the spot after the head twoTest.insertAfter(twoTest.getFirstNode(), "Z"); // insert a Z at the spot after the head oneTest.insertAfter(oneTest.getFirstNode(), "Z"); // the spot after the head should hold a "Z" assertEquals("Z", filledTest.getFirstNode().getNext().toString()); assertEquals("Z", twoTest.getFirstNode().getNext().toString()); assertEquals("Z", oneTest.getFirstNode().getNext().toString()); }
public void testBasicBuildAndGet() { for (int size = 0; size < 15; ++size) { final DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); DoublyLinkedList.LinkNode<Integer> prevLinkNode = null; for (int i = 0; i < size; ++i) { final DoublyLinkedList.LinkNode<Integer> linkNode = list.add(i); assertEquals(i, linkNode.getIndex()); assertEquals("i=" + i, (Integer) i, linkNode.getElt()); assertEquals(prevLinkNode, linkNode.getPrev()); assertNull(linkNode.getNext()); assertEquals(i + 1, list.size()); prevLinkNode = linkNode; } assertEquals("size=" + size, size, list.size()); for (int i = 0; i < size; ++i) { assertEquals("i=" + i, (Integer) i, list.get(i)); } } }
/** To test if deleting the last node */ @Test public void testDeleteLast() { // delete the last node filledTest.deleteLast(); twoTest.deleteLast(); oneTest.deleteLast(); emptyTest.deleteLast(); // the test should check that the last element was deleted assertEquals("testStrin", filledTest.toString()); assertEquals("t", twoTest.toString()); assertEquals("", oneTest.toString()); assertEquals("", emptyTest.toString()); }
private void swap(ListElement A, ListElement B, DoublyLinkedList in) { // next-zeiger. ListElement ntemp = A.next; A.next = B.next; B.next = ntemp; A.next.prev = A; B.next.prev = B; // prev-zeiger. ListElement ptemp = A.prev; A.prev = B.prev; B.prev = ptemp; A.prev.next = A; B.prev.next = B; if (A == in.first) { in.first = B; } else if (B == in.first) { in.first = A; } }
/** To create a generic list, and subtly test if insertFirst function works */ @Before public void createList() { // create a string called "testString" backwards String string = "gnirtStset"; // go through a for loop for (int i = 0; i < string.length(); i++) { // create a new node with each letter (starting at the end, which is why I had to make it // backwards before) filledTest.insertFirst(String.valueOf(string.charAt(i))); } // create a string called "te" backwards String two = "et"; // go through a for loop for (int j = 0; j < two.length(); j++) { // create a new node with each letter (starting at the end, which is why I had to make it // backwards before) twoTest.insertFirst(String.valueOf(two.charAt(j))); } // create a new node with t oneTest.insertFirst("t"); }
/** To test if inserting at the first spot works */ @Test public void testInsertFirst() { // insert a letter at the first node filledTest.insertFirst("X"); twoTest.insertFirst("Y"); oneTest.insertFirst("Z"); emptyTest.insertFirst("A"); // the first letter should be whatever I've inserted (see above) assertEquals("X", filledTest.getFirst()); assertEquals("Y", twoTest.getFirst()); assertEquals("Z", oneTest.getFirst()); assertEquals("A", emptyTest.getFirst()); }
public static void main(String[] args) { DoublyLinkedList<Integer, String> list = new DoublyLinkedList<Integer, String>(); list.add(5, "James"); System.out.println(list.size()); System.out.println(list.get(5)); list.add(5, "Thomas"); System.out.println(list.size()); System.out.println(list.get(5)); }
public static void main(String[] args) throws Exception { // Set up output PrintWriter pen = new PrintWriter(System.out, true); // Create some lists DoublyLinkedList<String> strings = new DoublyLinkedList<String>(); DoublyLinkedList<Integer> numbers = new DoublyLinkedList<Integer>(); // Prepend a few elements numbers.prepend(42); numbers.prepend(77); numbers.prepend(11); printList(pen, numbers); // Append a few elements numbers.append(1); numbers.append(2); numbers.append(3); printList(pen, numbers); // Delete the first element pen.println("Deleting first"); Cursor c = numbers.front(); numbers.delete(c); pen.println(numbers.get(c)); printList(pen, numbers); // Delete the third element pen.println("Deleting third"); Cursor d = numbers.front(); numbers.advance(d); numbers.advance(d); numbers.delete(d); printList(pen, numbers); // Insert and get some elements Cursor f = strings.front(); // Insert into null list strings.insert("world", f); printList(pen, strings); pen.println(strings.get(f)); // Insert at front of list strings.insert("hello", f); // Expected output: // "[hello world ]/hello/world/hello/hello/[hello world ]" printList(pen, strings); pen.println(strings.get(f)); strings.advance(f); pen.println(strings.get(f)); if (strings.hasPrev(f)) { pen.println(strings.getPrev(f)); } else { pen.println("hasPrev false negative"); } strings.retreat(f); if (strings.hasPrev(f)) { pen.println("hasPrev false positive"); } pen.println(strings.get(f)); pen.flush(); printList(pen, strings); // Insert mid-list strings.advance(f); strings.insert("foo", f); printList(pen, strings); pen.println(strings.get(f)); strings.advance(f); strings.insert("bar", f); printList(pen, strings); pen.println(strings.get(f)); // Testing Swap /* * pen.println(); pen.println("Testing swap(c1, c2):"); * pen.print(" Current list: "); printList(pen, strings); Cursor s1 = * strings.front(); Cursor s2 = strings.front(); * * // Swap first two: * * * Thread.sleep(1000); System.out.println("ONE"); Thread.sleep(1000); * * strings.advance(s2); strings.swap(s1, s2); * pen.print(" [foo hello bar world ] = "); printList(pen, strings); * pen.println(" s1 = " + strings.get(s1) + "; s2 = " + * strings.get(s2)); * * Thread.sleep(1000); System.out.println("TWO"); Thread.sleep(1000); * * strings.advance(s1); * * strings.advance(s2); strings.advance(s2); pen.println(" s1 = " + * strings.get(s1) + "; s2 = " + strings.get(s2)); strings.swap(s1, s2); * pen.print(" [foo bar hello world ] = "); printList(pen, strings); * pen.print(" s1 = " + strings.get(s1) + "; s2 = " + * strings.get(s2)); */ // Testing Search pen.println(); pen.println("Testing search(c, pred):"); pen.print(" Current list: "); printList(pen, numbers); Cursor g = numbers.front(); Even even = new Even(); Seven seven = new Seven(); System.out.println(" cursor @ " + numbers.get(g)); System.out.println(" " + numbers.search(g, even)); System.out.println(" cursor @ " + numbers.get(g)); pen.print(" Current list: "); printList(pen, numbers); numbers.advance(g); System.out.println(" cursor @ " + numbers.get(g)); System.out.println(" " + numbers.search(g, even)); System.out.println(" cursor @ " + numbers.get(g)); pen.print(" Current list: "); printList(pen, numbers); System.out.println(" cursor @ " + numbers.get(g)); System.out.println(" " + numbers.search(g, seven)); System.out.println(" cursor @ " + numbers.get(g)); System.out.println(" cursor @ " + numbers.get(g)); System.out.println(" " + numbers.search(g, even)); System.out.println(" cursor @ " + numbers.get(g)); // Testing select /* Odd odd = new Odd(); pen.println("Testing select(pred):"); pen.print(" Current list: "); System.out.print("even numbers: "); DoublyLinkedList<T> sevens = (DoublyLinkedList) numbers.select(even); PrintList(pen, sevens); System.out.println("odd numbers: " + numbers.select(odd)); System.out.println("sevens: " + numbers.select(seven)); // And we're done pen.close(); */ } // main(String[])
public void testInsertAndRemove() { final DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); // 2 5 8 // 1* b2 5 8 // 1 2a *3 5 8 // 1 2 3 4* b5 8 // 1 2 3 4 5 7* b8 // 1 2 3 4 5 6* b7 8 // 1 2 3 4 5 6 7 8a *9 // 1 2 3 4 5 6 7 8 9a *10 // 0* b1 2 3 4 5 6 7 8 9 10 list.add(2).add(5).add(8); final DoublyLinkedList.LinkNode<Integer> node2 = list.find(2); node2.push(1); final DoublyLinkedList.LinkNode<Integer> node3 = node2.add(3); node3.getNext().push(4).find(8).add(9); list.add(10); list.getFirst().push(0); list.find(8).push(7).push(6); list.getLast().add(12); list.add(11, 11); list.add(13, 13); assertEquals(14, list.size()); for (int i = 0; i <= 13; ++i) { assertEquals("i=" + i, (Integer) i, list.get(i)); final DoublyLinkedList.LinkNode<Integer> node = list.getLinkNode(i); assertEquals(i, node.getIndex()); assertEquals((Integer) i, node.getElt()); } // test findNext final DoublyLinkedList.LinkNode<Integer> secondNode2 = list.add(2); final DoublyLinkedList.LinkNode<Integer> foundSecondNode2 = node2.findNext(); assertEquals(secondNode2, foundSecondNode2); assertNull(secondNode2.findNext()); secondNode2.add(3); // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 2 3 assertNull(secondNode2.findNext()); // test remove assertEquals(16, list.size()); assertEquals((Integer) 2, list.remove(14)); assertEquals(15, list.size()); assertEquals((Integer) 3, list.remove(14)); assertEquals(14, list.size()); for (int i = 0; i <= 13; ++i) { assertEquals("i=" + i, (Integer) i, list.get(i)); final DoublyLinkedList.LinkNode<Integer> node = list.getLinkNode(i); assertEquals(i, node.getIndex()); assertEquals((Integer) i, node.getElt()); } // completely drain the list for (int i = 0; i < 14; ++i) { assertEquals("i=" + i, (Integer) i, list.remove(0)); assertEquals(13 - i, list.size()); } }