GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
Assignment2B
Browse code
Added append(IList<T>).
master
1 parent
d749afb
commit
4171df6a1700b768abb28925786f6899b6aff528
Jonathan Ström
authored
on 31 Aug 2017
Patch
Showing
2 changed files
src/List.java
src/MainTest.java
Ignore Space
Show notes
View
src/List.java
public class List<T> implements IList<T> { /** * Node for handling the list. */ private class Node { private T item; private Node next; public Node(T item, Node next) { this.item = item; this.next = next; } public Node(T item) { this.item = item; this.next = null; } } /** * Contains the root node. */ private Node root; /** * Add an element at a specific position starting at 0. */ public void addAt(int pos, T elem) { if (root == null) { root = new Node(elem); } else if (pos == 0) { root = new Node(elem, root); } else if (pos > 0) { Node walker = root; Node prev = null; while (walker != null && pos > 0) { pos--; prev = walker; walker = walker.next; } walker = new Node(elem, walker); prev.next = walker; } } /** * Remove an element at a position. */ public boolean removeAt(int pos) { boolean isRemoved = false; if (root != null) { if (pos == 0) { root = root.next; isRemoved = true; } else if (pos > 0 && pos < size()) { Node walker = root; Node prev = null; while (walker != null && pos > 0) { pos--; prev = walker; walker = walker.next; } prev.next = walker.next; walker = null; isRemoved = true; } } return isRemoved; } /** * Find an item in the list of type T. */ public int find(T elem) { int returnValue = 0; Node walker = root; while (walker != null && walker.item != elem) { walker = walker.next; returnValue++; } if (walker == null) { returnValue = -1; } return returnValue; } /** * Return the current size of the list. */ public int size() { int returnValue = 0; Node walker = root; while (walker != null) { walker = walker.next; returnValue++; } return returnValue; } /** * Remove all elements. */ public void clear() { while (root != null) { Node temp = root.next; root = null; root = temp; } } /** * Append an IList<T> to this list. */ public void append(IList<T> aList) { @SuppressWarnings("unchecked") T[] elements = (T[]) aList.getAllElementsAsStrings(); int size = size(); for (T element : elements) { addAt(size++, element); } } /** * Returns an array of strings with all elements. */ public String[] getAllElementsAsStrings() { int size = size(); String[] list = new String[size]; Node walker = root; for (int i = 0; i < size; ++i) { list[i] = (String)walker.item; walker = walker.next; } return list; } }
public class List<T> implements IList<T> { /** * Node for handling the list. */ private class Node { private T item; private Node next; public Node(T item, Node next) { this.item = item; this.next = next; } public Node(T item) { this.item = item; this.next = null; } } /** * Contains the root node. */ private Node root; /** * Add an element at a specific position starting at 0. */ public void addAt(int pos, T elem) { if (root == null) { root = new Node(elem); } else if (pos == 0) { root = new Node(elem, root); } else if (pos > 0) { Node walker = root; Node prev = null; while (walker != null && pos > 0) { pos--; prev = walker; walker = walker.next; } walker = new Node(elem, walker); prev.next = walker; } } /** * Remove an element at a position. */ public boolean removeAt(int pos) { boolean isRemoved = false; if (root != null) { if (pos == 0) { root = root.next; isRemoved = true; } else if (pos > 0 && pos < size()) { Node walker = root; Node prev = null; while (walker != null && pos > 0) { pos--; prev = walker; walker = walker.next; } prev.next = walker.next; walker = null; isRemoved = true; } } return isRemoved; } /** * Find an item in the list of type T. */ public int find(T elem) { int returnValue = 0; Node walker = root; while (walker != null && walker.item != elem) { walker = walker.next; returnValue++; } if (walker == null) { returnValue = -1; } return returnValue; } /** * Return the current size of the list. */ public int size() { int returnValue = 0; Node walker = root; while (walker != null) { walker = walker.next; returnValue++; } return returnValue; } /** * Remove all elements. */ public void clear() { while (root != null) { Node temp = root.next; root = null; root = temp; } } public void append(IList<T> aList) { } /** * Returns an array of strings with all elements. */ public String[] getAllElementsAsStrings() { int size = size(); String[] list = new String[size]; Node walker = root; for (int i = 0; i < size; ++i) { list[i] = (String)walker.item; walker = walker.next; } return list; } }
Ignore Space
Show notes
View
src/MainTest.java
public class MainTest { public static void main(String[] args) { System.out.println("The testing is starting..."); IList<String> list = new List<String>(); /*************************************** * Testing to add elements to the list. ***************************************/ list.addAt(0, "1"); // 1 list.addAt(1, "2"); // 1, 2 list.addAt(0, "3"); // 3, 1, 2 list.addAt(1, "4"); // 3, 4, 1, 2 System.out.println("Expected output: 3, 4, 1, 2"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } /*************************************** * Testing to get the size of the list. ***************************************/ System.out.println("Expected output: Size: 4"); System.out.println("Size: " + list.size()); /*************************************** * Testing to remove two elements and a third element that doesn't exist. ***************************************/ System.out.println("Expected output: true, true, false"); System.out.println(list.removeAt(0)); // 4, 1, 2 System.out.println(list.removeAt(2)); // 4, 1 System.out.println(list.removeAt(2)); // 4, 1 /*************************************** * Print out the list. ***************************************/ System.out.println("Expected output: 4, 1"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } /*************************************** * Try to find an item that exists, and one that doesn't. ***************************************/ System.out.println("Expected output: 1, -1"); System.out.println(list.find("1")); System.out.println(list.find("2")); /*************************************** * Testing appending a list to a list. ***************************************/ IList<String> newList = new List<String>(); newList.addAt(5, "5"); newList.addAt(5, "7"); newList.addAt(5, "11"); list.append(newList); System.out.println("Expected output: 4, 1, 5, 7, 11"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } /*************************************** * Testing to clear the list and remove an element from an empty list. ***************************************/ System.out.println("Expected output: Size: 0, false"); list.clear(); System.out.println("Size: " + list.size()); System.out.println(list.removeAt(0)); System.out.println("The testing has ended..."); } }
public class MainTest { public static void main(String[] args) { System.out.println("The testing is starting..."); IList<String> list = new List<String>(); /*************************************** * Testing to add elements to the list. ***************************************/ list.addAt(0, "1"); // 1 list.addAt(1, "2"); // 1, 2 list.addAt(0, "3"); // 3, 1, 2 list.addAt(1, "4"); // 3, 4, 1, 2 System.out.println("Expected output: 3, 4, 1, 2"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } /*************************************** * Testing to get the size of the list. ***************************************/ System.out.println("Expected output: Size: 4"); System.out.println("Size: " + list.size()); /*************************************** * Testing to remove two elements and a third element that doesn't exist. ***************************************/ System.out.println("Expected output: true, true, false"); System.out.println(list.removeAt(0)); // 4, 1, 2 System.out.println(list.removeAt(2)); // 4, 1 System.out.println(list.removeAt(2)); // 4, 1 /*************************************** * Print out the list. ***************************************/ System.out.println("Expected output: 4, 1"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } /*************************************** * Try to find an item that exists, and one that doesn't. ***************************************/ System.out.println("Expected output: 1, -1"); System.out.println(list.find("1")); System.out.println(list.find("2")); /*************************************** * Testing to clear the list and remove an element from an empty list. ***************************************/ System.out.println("Expected output: Size: 0, false"); list.clear(); System.out.println("Size: " + list.size()); System.out.println(list.removeAt(0)); System.out.println("The testing has ended..."); } }
Show line notes below