Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions exercises/lists1/IntList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ public IntList(int f, IntList r) {

/** Return the size of the list using... recursion! */
public int size() {
return 0;
if (rest == Null){
return 1;
}
return 1+ this.rest.size();
}

/** Return the size of the list using no recursion! */
public int iterativeSize() {
return 0;
int count = 0;
IntList p = this;
while (rest != Null){
count += 1;
p = p.rest;
}
return count;
}

/** Returns the ith value in this list.*/
public int get(int i) {
return 0;
if (i == 0){
return this.first;
}
return rest.get(i-1);
}

public static void main(String[] args) {
Expand All @@ -29,4 +41,4 @@ public static void main(String[] args) {

System.out.println(L.iterativeSize());
}
}
}