14
14
*/
15
15
class SinglyLinkedList {
16
16
/**Head refered to the front of the list */
17
- private LinkForLinkedList head ;
17
+ private Node head ;
18
18
19
19
/**
20
20
* Constructor of SinglyLinkedList
@@ -29,18 +29,18 @@ public SinglyLinkedList(){
29
29
* @param x Element to be added
30
30
*/
31
31
public void insertHead (int x ){
32
- LinkForLinkedList newLink = new LinkForLinkedList (x ); //Create a new link with a value attached to it
33
- newLink .next = head ; //Set the new link to point to the current head
34
- head = newLink ; //Now set the new link to be the head
32
+ Node newNode = new Node (x ); //Create a new link with a value attached to it
33
+ newNode .next = head ; //Set the new link to point to the current head
34
+ head = newNode ; //Now set the new link to be the head
35
35
}
36
36
37
37
/**
38
38
* This method deletes an element at the head
39
39
*
40
40
* @return The element deleted
41
41
*/
42
- public LinkForLinkedList deleteHead (){
43
- LinkForLinkedList temp = head ;
42
+ public Node deleteHead (){
43
+ Node temp = head ;
44
44
head = head .next ; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
45
45
return temp ;
46
46
}
@@ -58,9 +58,9 @@ public boolean isEmpty(){
58
58
* Prints contents of the list
59
59
*/
60
60
public void display (){
61
- LinkForLinkedList current = head ;
61
+ Node current = head ;
62
62
while (current !=null ){
63
- current .displayLink ( );
63
+ System . out . print ( current .getValue ()+ " " );
64
64
current = current .next ;
65
65
}
66
66
System .out .println ();
@@ -96,26 +96,26 @@ public static void main(String args[]){
96
96
* @author Unknown
97
97
*
98
98
*/
99
- class LinkForLinkedList {
99
+ class Node {
100
100
/** The value of the node */
101
101
public int value ;
102
102
/** Point to the next node */
103
- public LinkForLinkedList next ; //This is what the link will point to
103
+ public Node next ; //This is what the link will point to
104
104
105
105
/**
106
106
* Constructor
107
107
*
108
108
* @param valuein Value to be put in the node
109
109
*/
110
- public LinkForLinkedList (int valuein ){
110
+ public Node (int valuein ){
111
111
value = valuein ;
112
112
}
113
113
114
114
/**
115
- * Prints out the value of the node
115
+ * Returns value of the node
116
116
*/
117
- public void displayLink (){
118
- System . out . print ( value + " " ) ;
117
+ public int getValue (){
118
+ return value ;
119
119
}
120
120
121
- }
121
+ }
0 commit comments