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 so that the function addAt(int, T) can use position now.
master
1 parent
76f3e00
commit
6f531a475777a800fbedb0b27a88b5c215045166
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> { 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; } } private Node root; 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; } } public boolean removeAt(int pos) { return false; } 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; } public int size() { int returnValue = 0; Node walker = root; while (walker != null) { walker = walker.next; returnValue++; } return returnValue; } public void clear() { } public void append(IList<T> aList) { } 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> { private class Node { private T item; private Node next; public Node(T item) { this.item = item; this.next = null; } } private Node root; public void addAt(int pos, T elem) { if (root == null) { root = new Node(elem); } else { Node walker = root; while (walker.next != null) { walker = walker.next; } walker.next = new Node(elem); } } public boolean removeAt(int pos) { return false; } 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; } public int size() { int returnValue = 0; Node walker = root; while (walker != null) { walker = walker.next; returnValue++; } return returnValue; } public void clear() { } public void append(IList<T> aList) { } 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("Testing starting..."); IList<String> list = new List<String>(); // Should give the output: 3, 4, 1, 2 list.addAt(0, "Item 1"); list.addAt(1, "Item 2"); list.addAt(0, "Item 3"); list.addAt(1, "Item 4"); System.out.println("Size: " + list.size()); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } System.out.println("Testing ended..."); } }
public class MainTest { public static void main(String[] args) { IList<String> list = new List<String>(); list.addAt(0, "item 1"); list.addAt(0, "item 2"); for (int i = 0; i < list.size(); ++i) { System.out.println(list.getAllElementsAsStrings()[i]); } } }
Show line notes below