Linked List

single linked list:

1. Node class
NodeLL.java

 package com.nrs.ds.basic;  
 public class NodeLL {  
 Object value;  
 NodeLL next;  
 public NodeLL(Object value,NodeLL next){  
      this.value=value;  
      this.next = next;  
 }  
 }  
2. LinkedList implementation

LinkedListImpl.java

 package com.nrs.ds.basic;  
 public class LinkedListImpl<T> {  
      NodeLL head;  
      public LinkedListImpl(){  
           head = null;  
      }  
      public void add(Object value){  
           NodeLL newNode= new NodeLL(value,null);  
           if(head == null){  
                head=newNode;  
           }else{  
                newNode.next = head;  
                head = newNode;  
           }  
      }  
      public void delete(){  
           head = head.next;  
      }  
      public void display(){  
           NodeLL n=head;  
           while(n != null){  
                System.out.println((T)n.value);  
                n = n.next;  
           }  
      }  
 }  
3. Linked List Test
LinkedListTest.java

 package com.nrs.ds.basic;  
 public class LinkedListTest {  
      public static void main(String[] args) {  
           LinkedListImpl<String> ll = new LinkedListImpl<String>();  
           ll.add("Dhanraj");  
           ll.add("Shiva");  
           ll.add("Naresh");  
           ll.add("Phani");  
           ll.display();  
           System.out.println("Before delete");  
           ll.delete();  
           System.out.println("After delete");  
           ll.display();  
      }  
 }  
Output:


Comments

Popular posts from this blog

Tournament Tree or Winner Tree

Heaps