Created
July 7, 2021 18:14
-
-
Save Gkemon/b419e9f757668be56dbd619f705d2609 to your computer and use it in GitHub Desktop.
LinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestLinkedListImplementation { | |
public static void main(String args[]) { | |
LinkedList ll = new LinkedList(); | |
ll.addValue(1); | |
ll.addValue(2); | |
ll.addValue(3); | |
ll.addValue(4); | |
ll.addValue(5); | |
ll.addValue(6); | |
ll.printAll(); | |
} | |
} | |
class LinkedList { | |
Node headNode; | |
public void addValue(int value){ | |
if(headNode==null){ | |
headNode = new Node(value); | |
}else { | |
headNode.setValueToTheNext(value); | |
} | |
} | |
public void printAll(){ | |
headNode.print(); | |
} | |
} | |
class Node{ | |
Integer value=null; | |
Node nextNode=null; | |
public Node(int value){ | |
this.value=value; | |
} | |
public void setValueToTheNext(int value){ | |
if(nextNode==null){ | |
nextNode=new Node(value); | |
}else nextNode.setValueToTheNext(value); | |
} | |
public void print(){ | |
System.out.println("value: "+value+"\n\n"); | |
if(nextNode!=null){ | |
nextNode.print(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment