Recursion on a LinkedList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chetah
    New Member
    • Sep 2008
    • 10

    Recursion on a LinkedList

    Code:
    public class LinkedList<T>{
        private Node head;
        
        
        public LinkedList()
        {
            head = null;
        }
        
        public boolean isEmpty()
        {
            return (head==null);
        }
        
        public void addToHead(T val )
        {
            Node n = new Node(val);
            n.next = head;
            head   = n;
        }
        
        
        public T getHeadData(){
            if(head == null){
                throw new RuntimeException("attempt to get data from an empty list !");
            }
            else{
                return head.data;
            }
        }
        
        public void deleteHead(){
            if(head ==  null){
                throw new RuntimeException("Deleting from an empty list !");
            }else{
                head= head.next;
            }
        }
    
        public void addToTail(T val)//show no result
        {
            Node n = new Node (val);    
            if(head==null)
                head = n;
            else{
                Node curr = head;
                curr = curr.next;
                curr.next = n;
                addToTail(val);
            }    
        }//end addtail
    
    
        
        public void delete(T val){//does not return a result (shows no errors)
            Node curr=null;
            if(head !=null){
                if(head.data==val)
                    head = head.next;
                else{
                    curr = head;
                    curr = curr.next;
                    delete(curr.data);
                }
            }
        }
        
        public void insertSorted(T val){
            Node y = new Node(val);
            
            if(head==null || val < head.data)//operator < cannot be applied to T, T
                head = y;
            else
                head = head.next;
                insertSorted(head.data);
        }
        
        
        public boolean contains(T val)
        {
                
            if(head!=null){
                if(head.data==val)
                    return true;
                    head = head.next;
                return contains(val);
            }
            return false;
        
        }//end of contains
        
            
        public String toString(){
            String str = " ";
            Node curr = head;
            while(curr !=null){
                str = str + curr.data +" ";
                curr = curr.next;
            }
            return str + "\n";
        }
        
        
        public class Node{
            public T data;
            public Node next;
            
            public Node(T val){
                data = val;
                next = null;
            }
        }//End of node class
    }//Linked List
    
    
    class TestLinkedList{
        public static void main(String[] args){
            
            LinkedList<Integer>ST = new LinkedList<Integer>();
            
            for(int i = 0; i < 10; i++){  //correctly load linkedlist
                ST.addToHead(i);
            }
            
            
                System.out.println(ST.toString()); //print list
                
                        
                boolean y = ST.contains(12);
               System.out.println(y);
    
           
               ST.addToTail(200); //returns nothing
                   System.out.println(ST.toString());
               
                          
                ST.delete(5);
                System.out.println(ST.toString());
    
                insertSorted(11);
                System.out.println(ST.toString());
            
        }//end main
    }//end class
    
    Need help.
    public void addToTail(T val)
    public void delete(T val)
    public void insertSorted(T val)
    I need help to get the above functions to work recursively.  I am able to get them work iteratively, no problem.  When I try to write the functions re cursively, I am not able to see any results. I am at a lost understanding why.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    public void addToTail(T val)
    public void delete(T val)
    public void insertSorted(T val)

    You need to create functions pointing to the Nodes.
    Each Node does not point to a LinkedList, it points to another Node!

    eg create a function
    private void addToTail(T val, Node n)

    Code:
    public void addToTail(T val){
      base case // need to check if there exists a node..otherwise
      addtoTail(val, head);
    }
    private void addToTail(T val, Node n){
      if (n.next = null){
        n.next = new Node(val);
      } else {
        addToTail(val, n.next)
      }
    }
    Do something similar for the rest and you should be set.

    Comment

    • chetah
      New Member
      • Sep 2008
      • 10

      #3
      Call from main

      Thanks you very much for your response. However, I still have a problem, when I am call a function from the main program.

      public static void main(etc){

      ST.addToTail(No de x, 12) // I am unable to access the Node class from LinkedList.
      where am I going wrong?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Are you calling it from the main program? It should just be:
        ST.addToTail(12 )

        The class will then call addToTail(head, 12) on itself

        Comment

        Working...