Skip to content

Commit fee6510

Browse files
committed
lecture code for jan 31
1 parent 1e0a5f9 commit fee6510

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

lec4_lists2/SLList.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ public IntNode(int i, IntNode n) {
1616
private IntNode sentinel;
1717
private int size;
1818

19-
public SLList(int x) {
20-
sentinel = new IntNode(12873, null);
21-
sentinel.next = new IntNode(x, null);
22-
size = 1;
23-
}
24-
2519
public SLList() {
2620
size = 0;
2721
sentinel = new IntNode(12873, null);

lec5_lists3/SLList.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lec5_lists3;
2+
3+
/* List of anythings */
4+
public class SLList<Mustard> {
5+
private class MustardNode {
6+
public Mustard item; // first number in the list
7+
public MustardNode next; // rest of the list
8+
9+
public MustardNode(Mustard i, MustardNode n) {
10+
item = i;
11+
next = n;
12+
}
13+
}
14+
15+
16+
// the sentinel node is the dummy node at th front of the list
17+
// and the real first item is sentinel.next (if it exists)
18+
private MustardNode sentinel;
19+
private int size;
20+
21+
public SLList() {
22+
size = 0;
23+
sentinel = new MustardNode(null, null);
24+
}
25+
26+
public void addFirst(Mustard x) {
27+
size += 1;
28+
sentinel.next = new MustardNode(x, sentinel.next);
29+
}
30+
31+
public Mustard getFirst() {
32+
return sentinel.next.item;
33+
}
34+
35+
public void addLast(Mustard x) {
36+
size += 1;
37+
MustardNode p = sentinel;
38+
39+
// advance p until it is the last item
40+
while (p.next != null) {
41+
p = p.next;
42+
}
43+
44+
p.next = new MustardNode(x, null);
45+
}
46+
47+
public int size() {
48+
return size;
49+
}
50+
}

lec5_lists3/SLListUser.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package lec5_lists3;
2+
3+
public class SLListUser {
4+
public static void main(String[] args) {
5+
SLList<String> L = new SLList<>();
6+
L.addFirst("mustard");
7+
L.addFirst("mustard2");
8+
L.addLast("mustard");
9+
L.addLast("ketchup");
10+
L.addLast("dijon");
11+
12+
System.out.println(L.size());
13+
int[] z = {9, 10, 11, 12, 13};
14+
}
15+
}

0 commit comments

Comments
 (0)