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 IList, List and a testing file. Now prints out all items added.
master
1 parent
185cc57
commit
76f3e00f647e324ed3576a68508ab2411cb3b1c0
Jonathan Ström
authored
on 31 Aug 2017
Patch
Showing
4 changed files
.gitignore
src/IList.java
src/List.java
src/MainTest.java
Ignore Space
Show notes
View
.gitignore
0 → 100644
bin
Ignore Space
Show notes
View
src/IList.java
0 → 100644
public interface IList<T> { public void addAt(int pos, T elem); public boolean removeAt(int pos); public int find(T elem); public int size(); public void clear(); public void append(IList<T> aList); public String[] getAllElementsAsStrings(); }
Ignore Space
Show notes
View
src/List.java
0 → 100644
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
0 → 100644
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