-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSList_sample.java
More file actions
69 lines (54 loc) · 1.43 KB
/
Copy pathSList_sample.java
File metadata and controls
69 lines (54 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class SList {
public IntNode front;
public int size;
public SList() {
size = 0;
front = new IntNode(89724, null);
}
public SList(int x) {
front = new IntNode(98712, null);
front.next = new IntNode(x, null);
size = 1;
}
public void insertBack(int x) {
IntNode p = front;
// step p along until p.next == null
// because this means that p is the last
// item.
while (p.next != null) {
p = p.next;
}
p.next = new IntNode(x, null);
size = size + 1;
}
// start with:
// [???] -> [53]
// want to insert: 16
// a new node is created!! [16] --> [53]
public void insertFront(int x) {
IntNode oldFrontItem = front.next;
IntNode newFrontItem = new IntNode(x, oldFrontItem);
front.next = newFrontItem;
size = size + 1;
}
/** Returns number of items in this SList. */
public int size() {
return size;
}
/** Returns front item in list. */
public int getFront() {
return front.next.item;
}
/** Returns back item in list. */
public int getBack() {
IntNode p = front;
// step p along until p.next == null
// because this means that p is the last
// item.
while (p.next != null) {
p = p.next;
}
return p.item;
}
//SizeExercise.java
}