Recursion on a LinkedList

Discussion in 'Java' started by cyrow, Feb 23, 2010.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    ublic 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.
     
    
    
    
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice