Skip to content

Commit d6b9e54

Browse files
authored
update singly linked list
1 parent 86ac332 commit d6b9e54

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

data_structures/Lists/SinglyLinkedList.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
class SinglyLinkedList{
1616
/**Head refered to the front of the list */
17-
private LinkForLinkedList head;
17+
private Node head;
1818

1919
/**
2020
* Constructor of SinglyLinkedList
@@ -29,18 +29,18 @@ public SinglyLinkedList(){
2929
* @param x Element to be added
3030
*/
3131
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
3535
}
3636

3737
/**
3838
* This method deletes an element at the head
3939
*
4040
* @return The element deleted
4141
*/
42-
public LinkForLinkedList deleteHead(){
43-
LinkForLinkedList temp = head;
42+
public Node deleteHead(){
43+
Node temp = head;
4444
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
4545
return temp;
4646
}
@@ -58,9 +58,9 @@ public boolean isEmpty(){
5858
* Prints contents of the list
5959
*/
6060
public void display(){
61-
LinkForLinkedList current = head;
61+
Node current = head;
6262
while(current!=null){
63-
current.displayLink();
63+
System.out.print(current.getValue()+" ");
6464
current = current.next;
6565
}
6666
System.out.println();
@@ -96,26 +96,26 @@ public static void main(String args[]){
9696
* @author Unknown
9797
*
9898
*/
99-
class LinkForLinkedList{
99+
class Node{
100100
/** The value of the node */
101101
public int value;
102102
/** 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
104104

105105
/**
106106
* Constructor
107107
*
108108
* @param valuein Value to be put in the node
109109
*/
110-
public LinkForLinkedList(int valuein){
110+
public Node(int valuein){
111111
value = valuein;
112112
}
113113

114114
/**
115-
* Prints out the value of the node
115+
* Returns value of the node
116116
*/
117-
public void displayLink(){
118-
System.out.print(value+" ");
117+
public int getValue(){
118+
return value;
119119
}
120120

121-
}
121+
}

0 commit comments

Comments
 (0)