@Test public void findNodeOfSingleLinkedListTest() { List<Object> list = new ArrayList<Object>(); list.add(0); list.add(1); list.add(2); SingleLinkedList linkedList = new SingleLinkedList(list); assertTrue(Integer.parseInt(linkedList.find(1).toString()) == 1); }
@Test public void deleteOneNodeOfSingleLinkedListTest() { List<Object> list = new ArrayList<Object>(); list.add(0); list.add(1); list.add(2); SingleLinkedList linkedList = new SingleLinkedList(list); linkedList.delete(1); assertTrue(linkedList.size() == 2); }
@Test public void testSet() throws Exception { SingleLinkedList<Integer> list = new SingleLinkedList<>(); fill(list); Integer set = list.set(5, 15); assertEquals(5, set.intValue()); assertEquals(15, list.get(5).intValue()); }
@Test public void insertAfterOneNodeOfSingleLinkedListTest() { List<Object> list = new ArrayList<Object>(); list.add(0); list.add(1); list.add(3); SingleLinkedList linkedList = new SingleLinkedList(list); linkedList.insertAfter(1, 2); assertTrue(linkedList.size() == 4); }
public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Create object of class LinkedList SingleLinkedList list = new SingleLinkedList(); System.out.println("Singly Linked List Test\n"); char ch; // Perform list operations do { System.out.println("\nSingly Linked List Operations\n"); System.out.println("1. Insert at beginning"); System.out.println("2. Insert at end"); System.out.println("3. Insert at position"); System.out.println("4. Delete at position"); System.out.println("5. Check empty"); System.out.println("6. Get size"); int choice = scan.nextInt(); switch (choice) { case 1: System.out.println("Enter integer element to insert"); list.insertAtStart(scan.nextInt()); break; case 2: System.out.println("Enter integer element to insert"); list.insertAtEnd(scan.nextInt()); break; case 3: System.out.println("Enter integer element to insert"); int num = scan.nextInt(); System.out.println("Enter position"); int pos = scan.nextInt(); if (pos <= 1 || pos > list.getSize()) System.out.println("Invalid position\n"); else list.insertAtPos(num, pos); break; case 4: System.out.println("Enter position"); int p = scan.nextInt(); if (p < 1 || p > list.getSize()) System.out.println("Invalid position\n"); else list.deleteAtPos(p); break; case 5: System.out.println("Empty status = " + list.isEmpty()); break; case 6: System.out.println("Size = " + list.getSize() + "\n"); break; default: System.out.println("Wrong Entry \n"); break; } list.display(); System.out.println("\n Do you want to continue (Type y or n \n"); ch = scan.next().charAt(0); } while (ch == 'Y' || ch == 'y'); }
@Test public void testRemoval() throws Exception { SingleLinkedList<Integer> list = new SingleLinkedList<>(); fill(list); Integer remove = list.remove(0); assertEquals(0, remove.intValue()); Integer remove2 = list.remove(1); assertEquals(2, remove2.intValue()); Integer remove3 = list.remove(list.size() - 1); assertEquals(9, remove3.intValue()); assertEquals(7, list.size()); int[] arr = new int[] {1, 3, 4, 5, 6, 7, 8}; int index = 0; for (int i : list) { assertEquals(arr[index++], i); } // now add again and see if it worked fill(list); assertEquals(17, list.size()); arr = new int[] {1, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; index = 0; for (int i : list) { assertEquals(arr[index++], i); } }
public static Node findIntersectionNode(SingleLinkedList l1, SingleLinkedList l2) { int length_list1 = l1.length(); int length_list2 = l2.length(); int offset = 0; // check if 1st list is greater in length if (length_list1 > length_list2) { offset = length_list1 - length_list2; return findCommonNode(l1, l2, offset); } // check if second list is greater in length else if (length_list2 > length_list1) { offset = length_list2 - length_list1; return findCommonNode(l2, l1, offset); } else { // case when both the list are of same length return findCommonNode(l1, l2, 0); } }
@Test public void testInserts() throws Exception { SingleLinkedList<Integer> list = new SingleLinkedList<>(); fill(list); for (int i = 0; i < 10; i++) assertEquals(i, list.get(i).intValue()); int index = 0; for (int i : list) { assertEquals(i, index); index++; } // random access list.add(3, 24); list.add(8, 22); list.add(0, 88); int[] arr = new int[] {88, 0, 1, 2, 24, 3, 4, 5, 6, 22, 7, 8, 9}; index = 0; for (int i : list) { assertEquals(arr[index++], i); } }
public static void main(String[] args) { /* TEST 1 - 2 LIST with a common part */ // Common part of the list SingleLinkedList cl = new SingleLinkedList(); cl.insertAtStart(10); cl.insertAtStart(20); cl.insertAtStart(30); // create the first list SingleLinkedList l1 = new SingleLinkedList(); // l1.insertAtStart(4); // l1.insertAtStart(3); l1.insertAtStart(2); l1.insertAtStart(1); // insert the common list at the tail l1.InsertListAtEnd(cl); // create the second list SingleLinkedList l2 = new SingleLinkedList(); l2.insertAtStart(33); l2.insertAtStart(22); l2.insertAtStart(11); // insert the common list at the tail l2.InsertListAtEnd(cl); System.out.println("*********COMMMON LIST*****"); cl.displayList(); System.out.println("*********LIST 1***********"); l1.displayList(); System.out.println("*********LIST 2***********"); l2.displayList(); System.out.println("******COM ELEMENT**********"); Node common_node = findIntersectionNode(l1, l2); if (common_node == null) { System.out.println("No such Node found"); } else { System.out.println(common_node.data); } /* ************************************************** */ /* TEST 1 - 2 LIST with no common part */ // create the first list SingleLinkedList l11 = new SingleLinkedList(); // l1.insertAtStart(4); // l1.insertAtStart(3); l11.insertAtStart(2); l11.insertAtStart(1); // create the second list SingleLinkedList l22 = new SingleLinkedList(); l22.insertAtStart(33); l22.insertAtStart(22); l22.insertAtStart(11); System.out.println("*********LIST 1***********"); l11.displayList(); System.out.println("*********LIST 2***********"); l22.displayList(); System.out.println("******COM ELEMENT**********"); Node common_node2 = findIntersectionNode(l11, l22); if (common_node2 == null) { System.out.println("No such Node found"); } else { System.out.println(common_node2.data); } }
@Test public void getSizeOfSingleLinkedListTest() { SingleLinkedList linkedList = new SingleLinkedList(); assertTrue(linkedList.size() == 0); }
public static void main(String[] args) { SingleLinkedList<Integer> sll = new SingleLinkedList<Integer>(); SingleLinkedList<Integer> sll1 = new SingleLinkedList<Integer>(); sll.insertElement(1); sll.insertElement(2); sll.insertElement(3); sll.insertElement(4); sll.insertAfterElement(4, 5); sll.insertBeforeElement(3, 6); sll.pushBack(0); sll1.insertElement(10); sll1.insertElement(14); sll1.insertAfterElement(10, 11); sll1.insertBeforeElement(14, 13); sll1.pushFront(7); sll1.insertAfterElement(7, 8); sll1.insertBeforeElement(10, 9); sll1.popBack(); sll.concatListAtIndex(sll1, 3); sll.makeEmpty(); sll.printReverse(); }
private void fill(SingleLinkedList<Integer> list) { for (int i = 0; i < 10; i++) list.add(i); }