CS 61B Part 2 Linked List
prev: [[61B_Part_1_Java]]
next: [[61B_Part_3_OOP]]
1 | public class IntList { |
SLList
While functional, “naked” linked lists like the one above are hard to use.
Users of this class are probably going to need to know references very well, and be able to think recursively. Let’s make our users’ lives easier.
1 | public class IntNode { |
1:
1 | SLList L = new SLList(15); |
in large software engineering projects, the private keyword is the signal of the programs that needn’t to be understand by users.
also, we can nest the classes:
1 |
|
However, the recursion are really slow, as each time call size would recurse millions of times. So, we should create a varible called size directly.
1 | public class SLList { |
sentinal node
to address the problem of empty list
create the node for the first of the linked list
sentinal.next is the first element of the SLList
1 | public class SLList { |
Lect6 Part 1: DLList
AddLast
the formal addLast method is a little bit slow:
1 | public void addLast(int x) { |
as it walks through the whole list to find the best one.
add the last varible: when deleting, the second last should be iterated to.
add the double-link!
so, a double-linked list(DLList) is in urgent need.(add, get, remove method also get faster!)
Also, it is recommended that the last elements’ next return to the first, and the first elements’ last return to the last.
Therefore, add .last and .prev method to fasten the linked list!
we would build this in project 1A.
SList
how to contain string into the Linked lists?
we can put the parameter as a kind of varible!
1 | public class DLList<BleepBlorp> { // BleepBlorp is a random name |
usage:
1 | DLList<Integer> d1 = new DLList<>(5); |
Lecture 4 part 2: the arrays
final objective: build a AList(using arrays instead of the recursive calls to make the Linked List)
1 | x=new int[]{1, 2, 3, 4, 5}; |
class VS arrays
The differences are: the array can be computed and used at the same time; but class CANNOT!
(reflection API allow class to do so, but it is not recommended)
Lect7 the ALists, resizing
the get(int i) method (Native ALList)
get is really slow in the function! (compared with GetLast)
1. Naive Array Based List
using arrays to build linked list
Accessing the ith element of an array takes constant time on a modern computer.
1 | public class AList { |
2. removeLast
In removeLast, we should see change in our memory box!
E2: Try to write removeLast. Before starting, decide which of size, items, and items[i] needs to change so that our invariants are preserved after the operation, i.e. so that future calls to our methods provide the user of the list class with the behavior they expect.
1 | // lack code here(class required to be viewed) |
3. Naive Resizing Arrays
How to add the number of the arrays? In Java, the only way is to add the arrays that is large enough.
1 | int[] a = new int[size + 1]; |
the process is called resizing, meaning that the array didn’t change its size, but a new array with more members come.
E4: Try to implement the addLast(int i) method to work with resizing arrays.
Reflection
However, when comparing to our previous SLList, the ALList seemed to be very slow.
Creating all those memory boxes and recopying their contents takes time!
In SLList, each insert take the same time. In ALList, however, each insert take the time that linear increasing!
We can fix our performance problems by growing the size of our array by a multiplicative amount, rather than an additive amount.
for example:
the additive one:
1 | public void insertBack(int x) { |
the multiplicative one:
1 | public void insertBack(int x) { |
which make our program faster. Why?
because that the program is faster as the time of revising the size of the array is much smaller.
to build a AList that the same speed as the DLList, we need to adapt the data structure below:
lists in real java code
1 | java.util.List<Integer> L = new java.util.ArrayList<>(); |
another example: (more realistic)
1 | import java.util.List; |
you can look up these words in Java official website!
