1
+ package lec16_inheritance4 ;
2
+
3
+ /* Represent a list of stuff, where all the "list" work is delegated
4
+ * to a naked recursive data structure. */
5
+
6
+ public class SLList <Blorp > implements List61B <Blorp > {
7
+ public class Node {
8
+ public Blorp item ; /* Equivalent of first */
9
+ public Node next ; /* Equivalent of rest */
10
+
11
+ public Node (Blorp i , Node h ) {
12
+ item = i ;
13
+ next = h ;
14
+ }
15
+ }
16
+
17
+ private Node sentinel ;
18
+ private int size ;
19
+
20
+ /** Creates an empty list. */
21
+ public SLList () {
22
+ size = 0 ;
23
+ sentinel = new Node (null , null );
24
+ }
25
+
26
+ public SLList (Blorp x ) {
27
+ size = 1 ;
28
+ sentinel = new Node (null , null );
29
+ sentinel .next = new Node (x , null );
30
+ }
31
+
32
+ /** Adds an item of the front. */
33
+ public void addFirst (Blorp x ) {
34
+ Node oldFrontNode = sentinel .next ;
35
+ Node newNode = new Node (x , oldFrontNode );
36
+ sentinel .next = newNode ;
37
+ size += 1 ;
38
+ }
39
+
40
+ /** Gets the front item of the list. */
41
+ public Blorp getFirst () {
42
+ return sentinel .next .item ;
43
+ }
44
+
45
+ /** Puts an item at the back of the list. */
46
+ public void addLast (Blorp x ) {
47
+ size += 1 ;
48
+
49
+ Node p = sentinel ;
50
+
51
+ /* Move p until it reaches the end. */
52
+ while (p .next != null ) {
53
+ p = p .next ;
54
+ }
55
+
56
+ p .next = new Node (x , null );
57
+ }
58
+
59
+ /** Returns the back node of our list. */
60
+ private Node getLastNode () {
61
+ Node p = sentinel ;
62
+
63
+ /* Move p until it reaches the end. */
64
+ while (p .next != null ) {
65
+ p = p .next ;
66
+ }
67
+ return p ;
68
+ }
69
+
70
+ /** Returns last item */
71
+ public Blorp getLast () {
72
+ Node back = getLastNode ();
73
+ return back .item ;
74
+ }
75
+
76
+ /** Deletes and returns last item. */
77
+ public Blorp removeLast () {
78
+ Node back = getLastNode ();
79
+ if (back == sentinel ) {
80
+ return null ;
81
+ }
82
+
83
+ size = size - 1 ;
84
+ Node p = sentinel ;
85
+
86
+ while (p .next != back ) {
87
+ p = p .next ;
88
+ }
89
+ p .next = null ;
90
+ return back .item ;
91
+ }
92
+
93
+ public int size () {
94
+ return size ;
95
+ }
96
+
97
+ /** Gets the positionth item of the list. */
98
+ public Blorp get (int position ) {
99
+ if (position == 0 ) {
100
+ return getFirst ();
101
+ }
102
+ Node currentNode = sentinel .next .next ;
103
+ while (position > 1 && currentNode .next != null ) {
104
+ position -= 1 ;
105
+ currentNode = currentNode .next ;
106
+ }
107
+
108
+ return currentNode .item ;
109
+ }
110
+
111
+ /** Inserts item into given position.
112
+ * Code from discussion #3 */
113
+ public void insert (Blorp item , int position ) {
114
+ if (sentinel .next == null || position == 0 ) {
115
+ addFirst (item );
116
+ return ;
117
+ }
118
+
119
+ Node currentNode = sentinel .next .next ;
120
+ while (position > 1 && currentNode .next != null ) {
121
+ position -= 1 ;
122
+ currentNode = currentNode .next ;
123
+ }
124
+
125
+ Node newNode = new Node (item , currentNode .next );
126
+ currentNode .next = newNode ;
127
+ }
128
+
129
+ /** TODO: Add a print method that overrides List61B's inefficient print method. */
130
+
131
+ }
0 commit comments