Arrays, Linked Lists
Exam-Level 02
CS61B Fall 2024
Example Agenda
CS61B Fall 2024
Announcements
CS61B Fall 2024
Content Review
CS61B Fall 2024
GRoE: Golden Rule of Equals
“Given variables y and x:
y = x copies all the bits from x into y.”
Java is pass-by-value: when you call a function and give it some arguments, the function called receives an exact copy of those arguments, tied to its own local variables.
“Copies all the bits” means different things for primitive vs. reference types.
CS61B Fall 2024
Primitive vs. Reference Types
Examples: byte, short, int, long, float, double, boolean, char
Examples: Strings, Arrays, Linked Lists, Dogs, etc.
CS61B Fall 2024
Back to the GRoE
“Given variables y and x:
y = x copies all the bits from x into y.”
CS61B Fall 2024
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
1
2
3
5
5
x
arr
CS61B Fall 2024
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
doSomething(x, arr);
...
public void doSomething(int y, int[] other) {
y = 9;
other[2] = 4;
}
1
2
4
5
5
x
arr
9
y
other
CS61B Fall 2024
Static vs. Instance, Revisited
Static variables and functions belong to the whole class.
Example: Every 61B Student shares the same professor, and if the professor were to change it would change for everyone.
Instance variables and functions belong to each individual instance.
Example: Each 61B Student has their own ID number, and changing a student’s ID number doesn’t change anything for any other student.
CS61B Fall 2024
this vs. static
CS61B Fall 2024
Arrays
Arrays are data structures that can only hold elements of the same (primitive or reference) type of value.
arr[i] holds a value in the ith position of the array (zero-indexed). We can also have n-dimensional
arrays (ie. int[][] a = new int[3][2]; you can index into these like a[2][1])
4
1
8
0
1
2
0
1
2
Cat
2
id
5
age
Cat
4
id
9
age
Cat
8
id
1
age
Arrays have a set length when instantiated, so they cannot be extended / shortened with pointers like a Linked List. To resize, we need to copy over all elements to a new array (ie. System.arraycopy)
CS61B Fall 2024
Linked Lists
Linked Lists are modular lists that are made up of nodes that each contain a value and a pointer to the next node. To access values in a Linked List, you must use dot notation.
Example: intList.get(2)
CS61B Fall 2024
Worksheet
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
1
2
3
null
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
L2
1
2
3
null
4
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
L2
1
13
3
null
4
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
L2
1
13
3
4
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
L3
L2
1
13
3
4
50
null
CS61B Fall 2024
1 Boxes and Pointers
1 IntList L1 = IntList.list(1, 2, 3);
2 IntList L2 = new IntList(4, L1.rest);
3 L2.rest.first = 13;
4 L1.rest.rest.rest = L2;
5 IntList L3 = IntList.list(50);
6 L2.rest.rest = L3;
L1
L3
L2
1
13
3
4
50
null
CS61B Fall 2024
2 Interweave
Implement interweave, which takes in an IntList lst and an integer k, and destructively interweaves lst into k IntLists such that each list has the following properties:
These lists should be put in an array of length k, and this array should be returned.
For instance, if lst=[6, 5, 4, 3, 2, 1], and k=2, then you return an array of 2 linked lists, the first one [6, 4, 2], and the second one [5, 3, 1].
Destructively: instead of creating new IntList instances, you should focus on modifying the pointers in the existing IntList, lst.
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (___________________) {
IntList prevAtIndex = ___________________;
IntList next = ___________________;
__________________________________;
__________________________________;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
6
5
4
3
2
1
null
null
0
1
lst
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
null
0
1
L
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
null
0
1
L
k=1
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
null
0
1
L
k=1
prevAtIndex: null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
null
0
1
L
k=1
next
prevAtIndex: null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
0
1
L
k=1
next
prevAtIndex: null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
0
1
L
k=1
next
prevAtIndex: null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
0
1
L
k=1
next
prevAtIndex: null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
0
1
L
k=0
next
prevAtIndex: null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
null
0
1
L
k=0
next
prevAtIndex: null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=0
next
prevAtIndex: null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=0
next
prevAtIndex: null
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex: null
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=1
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
k=0
next
prevAtIndex:
null
null
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
next
prevAtIndex:
null
null
k=0
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
next
prevAtIndex:
null
null
k=0
CS61B Fall 2024
2 Interweave
1
2
3
4
5
6
0
1
L
next
prevAtIndex:
null
null
k=0
CS61B Fall 2024
2 Interweave
1
2
3
4
6
0
1
L
k=1
null
null
5
CS61B Fall 2024
2 Interweave
1
2
3
4
6
0
1
L
k=0
null
null
5
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = ___________________;
IntList next = ___________________;
__________________________________;
__________________________________;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = ___________________;
__________________________________;
__________________________________;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
__________________________________;
__________________________________;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
__________________________________;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
array[index].rest = prevAtIndex;
L = ___________________;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
array[index].rest = prevAtIndex;
L = next;
index -= 1;
if (______________________) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
array[index].rest = prevAtIndex;
L = next;
index -= 1;
if (index < 0) {
_______________________;
}
}
return array;
}
CS61B Fall 2024
2 Interweave
public static IntList[] interweave(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = k - 1;
IntList L = reverse(lst); // Assume reverse is implemented correctly
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
array[index].rest = prevAtIndex;
L = next;
index -= 1;
if (index < 0) {
index = k - 1;
}
}
return array;
}
CS61B Fall 2024
3 Remove Duplicates
Using the simplified DLList class, implement the removeDuplicates method.
removeDuplicates should remove all duplicate items from the DLList. For example, if our initial list [8, 4, 4, 6, 4, 10, 12, 12], our final list should be [8, 4, 6, 10, 12]. You may not assume that duplicate items are grouped together, or that the list is sorted!
public class DLList {
Node sentinel;
public DLList() {
// ...
}
public class Node {
int item;
Node prev;
Node next;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = _________________________________________________________________;
Node checker;
while (_________________________________________________________________) {
checker = _________________________________________________________________;
while (_________________________________________________________________) {
if (_________________________________________________________________) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
4
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
8
4
6
10
12
sentinel
ref
checker
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (_________________________________________________________________) {
checker = _________________________________________________________________;
while (_________________________________________________________________) {
if (_________________________________________________________________) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = _________________________________________________________________;
while (_________________________________________________________________) {
if (_________________________________________________________________) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (_________________________________________________________________) {
if (_________________________________________________________________) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (_________________________________________________________________) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (ref.item == checker.item) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
_________________________________________________________________;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (ref.item == checker.item) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
checkerPrev.next = checker.next;
_________________________________________________________________;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (ref.item == checker.item) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
checkerPrev.next = checker.next;
checkerNext.prev = checker.prev;
}
checker = _______________________________________________________;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (ref.item == checker.item) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
checkerPrev.next = checker.next;
checkerNext.prev = checker.prev;
}
checker = checker.next;
}
ref = _________________________________________________________________;
}
CS61B Fall 2024
3 Remove Duplicates
Node ref = sentinel.next;
Node checker;
while (ref != sentinel) {
checker = ref.next;
while (checker != sentinel) {
if (ref.item == checker.item) {
Node checkerPrev = checker.prev;
Node checkerNext = checker.next;
checkerPrev.next = checker.next;
checkerNext.prev = checker.prev;
}
checker = checker.next;
}
ref = ref.next;
}
CS61B Fall 2024