update singly linked list
This commit is contained in:
parent
86ac3321e3
commit
d6b9e548ef
@ -14,7 +14,7 @@
|
||||
*/
|
||||
class SinglyLinkedList{
|
||||
/**Head refered to the front of the list */
|
||||
private LinkForLinkedList head;
|
||||
private Node head;
|
||||
|
||||
/**
|
||||
* Constructor of SinglyLinkedList
|
||||
@ -29,9 +29,9 @@ class SinglyLinkedList{
|
||||
* @param x Element to be added
|
||||
*/
|
||||
public void insertHead(int x){
|
||||
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
|
||||
newLink.next = head; //Set the new link to point to the current head
|
||||
head = newLink; //Now set the new link to be the head
|
||||
Node newNode = new Node(x); //Create a new link with a value attached to it
|
||||
newNode.next = head; //Set the new link to point to the current head
|
||||
head = newNode; //Now set the new link to be the head
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,8 +39,8 @@ class SinglyLinkedList{
|
||||
*
|
||||
* @return The element deleted
|
||||
*/
|
||||
public LinkForLinkedList deleteHead(){
|
||||
LinkForLinkedList temp = head;
|
||||
public Node deleteHead(){
|
||||
Node temp = head;
|
||||
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
|
||||
return temp;
|
||||
}
|
||||
@ -58,9 +58,9 @@ class SinglyLinkedList{
|
||||
* Prints contents of the list
|
||||
*/
|
||||
public void display(){
|
||||
LinkForLinkedList current = head;
|
||||
Node current = head;
|
||||
while(current!=null){
|
||||
current.displayLink();
|
||||
System.out.print(current.getValue()+" ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
@ -96,26 +96,26 @@ class SinglyLinkedList{
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class LinkForLinkedList{
|
||||
class Node{
|
||||
/** The value of the node */
|
||||
public int value;
|
||||
/** Point to the next node */
|
||||
public LinkForLinkedList next; //This is what the link will point to
|
||||
public Node next; //This is what the link will point to
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param valuein Value to be put in the node
|
||||
*/
|
||||
public LinkForLinkedList(int valuein){
|
||||
public Node(int valuein){
|
||||
value = valuein;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the value of the node
|
||||
* Returns value of the node
|
||||
*/
|
||||
public void displayLink(){
|
||||
System.out.print(value+" ");
|
||||
public int getValue(){
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user