File tree Expand file tree Collapse file tree 4 files changed +49
-8
lines changed Expand file tree Collapse file tree 4 files changed +49
-8
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Linked List
3
+
4
+ A linked list is a data structure that stores data sequentially by
5
+ linking each item in the list to the next. It has the following properties:
6
+
7
+ - head: the first item in the list
8
+ - tail (optional) : last item in the list
9
+
10
+ It's made up of items called Nodes, which contain 2 pieces of information:
11
+
12
+ - value: any type or you can restrict to specific types
13
+ - next: a reference to the next item in the list
14
+
15
+ Nodes are usually added to the end of the list. Operations include:
16
+
17
+ - Add node
18
+ - Remove node
19
+ - Find a node
20
+ For example, suppose you have a LinkedList of numbers. It might look
21
+ like so:
22
+
23
+ * [] represents the 'value' attribute
24
+ * -> represents the 'next' attribute
25
+ * Together, []-> represents a Node
26
+
27
+ [100]-> [200]-> [300]-> null
28
+
29
+ The Node with value 100 is the head. The Node with value 300 is the
30
+ tail. Note how the tail points to nothing. Therefore null signifies
31
+ the end of the list.
32
+ */
1
33
package com .galvanize ;
2
34
3
35
public class LinkedList {
Original file line number Diff line number Diff line change
1
+ /*
2
+ Node
3
+
4
+ Nodes are the individual units contained in a data structure.
5
+
6
+ A node is a value object, representing any type of value and
7
+ providing a reference to the next node.
8
+ */
1
9
package com .galvanize ;
2
10
3
11
import java .util .Objects ;
Original file line number Diff line number Diff line change 1
1
package com .galvanize ;
2
2
3
3
public class Stack {
4
- public boolean empty ;
5
4
public Node top ;
6
5
private int size ;
7
6
8
7
public Stack () {
9
- this .empty = true ;
10
8
this .size = 0 ;
11
9
}
12
10
13
11
public int count () {
14
12
return size ;
15
13
}
16
14
15
+ public boolean isEmpty () {
16
+ return this .count () == 0 ;
17
+ }
18
+
17
19
public void push (Node item ) {
18
20
if (top == null ) {
19
21
top = item ;
20
- this .empty = false ;
21
22
} else {
22
23
item .setNext (top );
23
24
top = item ;
Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ void setUp() {
13
13
stack = new Stack ();
14
14
}
15
15
16
+ @ Test
17
+ void testStackEmptyTrue () {
18
+ assertTrue (stack .isEmpty ());
19
+ }
20
+
16
21
@ Test
17
22
void testStackPushNode () {
18
23
stack .push (new Node (100 ));
@@ -42,11 +47,6 @@ void testStackPeekReturnsTopNode() {
42
47
assertEquals (topItem , peekItem );
43
48
}
44
49
45
- @ Test
46
- void testStackEmptyTrue () {
47
- assertTrue (stack .empty );
48
- }
49
-
50
50
@ Test
51
51
void testNodesEqual () {
52
52
Node treeNode = new Node ("Tree" );
You can’t perform that action at this time.
0 commit comments