Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node{
- private String payload;
- private Node next;
- public Node (String payload){
- this.payload = payload;
- this.next = null;
- }
- public Node (String payload, Node next){
- this.payload = payload;
- this.next = next;
- }
- public String getPayload (){
- return this.payload;
- }
- public Node getNext(){
- return next;
- }
- public void setPayload (String payload){
- this.payload = payload;
- }
- public void setNext(Node next){
- this.next = next;
- }
- }
- class LinkedList{
- Node head;
- public LinkedList(){
- head = null;
- }
- public static void main (String [] args){
- Node node1 = new Node("Node 1");
- Node node2 = new Node("Node 2", node1);
- Node node3 = new Node("Node 3", node2);
- Node currentNode = node3;
- while(currentNode.getNext() != null){
- System.out.println(currentNode.getPayload());
- currentNode = currentNode.getNext();
- }
- System.out.println(currentNode.getPayload());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement