Results 1 to 4 of 4
  1. #1
    CMAC21 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Question recursive toString, copy, and equals methods for Doubly Linked List

    So I am having trouble writing the following methods:
    recursive toString
    recursive BackwardsToString
    equals
    copy
    reversed copy.

    This is the program i'm working on. When you get closer to the end, you'll see where my unfinished methods are. PLEASE HELP!
    Java Code:
    public class DoubleLinkedList<T> implements DoubleLinkedListADT<T> { 
        //Double linked list node class 
        public class DoubleLinkedListNode<T>  { 
            T info; 
            DoubleLinkedListNode<T> next; 
            DoubleLinkedListNode<T> back;
    
            public DoubleLinkedListNode() { 
                info = null; 
                next = null; 
                back = null; 
            }
    
            public String toString() { 
                return info.toString(); 
            } 
        }
    
        protected int count;  //number of nodes 
        protected DoubleLinkedListNode<T> first; //reference to first node 
        protected DoubleLinkedListNode<T> last;  //reference to last node 
        
        public DoubleLinkedList(){
          first = null;
          last = null;
          count = 0;
        }
        
        public void initializeList(){
          first = null;
          last = null;
          count = 0;
        }
        
        public boolean isEmptyList(){
          return (first == null);
        }
        
        public T front(){
          return first.info;
        }
        
        public T back(){
          return last.info;
        }
        
        public int length(){
          return count;
        }
        
        public void print(){
          DoubleLinkedListNode<T> current;
          current = first;
          while(current != null){
            System.out.print(current + " ");
            current = current.next;
          }
        }
        
        pubic void reversePrint(){
          DoubleLinkedListNode<T> current;
          current = last;
          while (current != null){
            System.out.print(current.info + " ");
            current = current.back;
          }
        }
        
        public boolean search(T searchItem){
          boolean found;
          DoubleLinkedListNode<T> current;
          found = false;
          current = first;
          while(current != null && !found){
            Comparable<T> temp = (Comparable<T>) current.info;
            if(temp.compareTo(searchItem) >= 0)
              found = true;
            else
              current = current.next;
          }
          if(found)
            found = current.info.equals(searchItem);
          
          return found;
        }
        
        public void insertCode(T insertItem){
          DoubleLinkedListNode<T> current;
          DoubleLinkedListNode<T> trailCurrent = null;
          DoubleLinkedListNode<T> newNode;
          boolean found;
          newNode = new DoubleLinkedListNode();
          
          newNode.info = insertItem;
          newNode.next = null;
          newNode.back = null;
          
          if(first == null){
            first = newNode;
            last = newNode;
            count++;
          }
          else{
            found = false;
            current = first;
            while(current != null && !found){
              Comparable<T> temp = (Comparable<T>) current.info;
              if(temp.compareTo(insertItem) >= 0)
                found = true;
              else{
                trailCurrent = current;
                current = current.next;
              }
            }
            if(current == first){
              first.back = newNode;
              newNode.next = first;
              first = newNode;
              count++;
            }
            else{
              if(current != null){
                trailCurrent.next = newNode;
                newNode.back = trailCurrent;
                newNode.next = current;
                current.back = newNode;
              }
              else{
                trailCurrent.next = newNode;
                newNode.back = trailCurrent;
                last = newNode;
              }
              count++;
            }
          }
        }
        
        public void deleteNode(T deleteItem){
          DoubleLinkedListNode<T> current;
          DoubleLinkedListNode<T> trailCurrent;
          boolean found;
          
          if(first == null)
            System.out.println("Cannot delete from an empty list.");
          else
            if(first.info.equals(deleteItem)){
              current = first;
              first = first.next;
              
              if(first != null)
                first.back = null;
              else
                last = null;
              count --;
            }
            else{
              found = false;
              current = first;
              
              while(current != null && !found){
                Comparable<T> temp = (Comparable<T>) current.info;
                if(temp.compareTo(deleteItem) >= 0)
                  found = true;
                else
                  current = current.next;
                
              }
              
              if(current == null)
                System.out.println("The item to be deleted is not in the list.");
              else
                if(current.info.equals(deleteItem)){
                  trailCurrent = current.back;
                  trailCurrent.next = current.next;
                  
                  if(current.next != null)
                    current.next.back = trailCurrent;
                  if(current == last)
                    last = trailCurrent;
                  
                  count--;
              }
              else
                System.out.println("The Item to be deleted is not in the list.");
              
            }
        }
        
        public String toString(){
          DoubleLinkedListNode temp = getFirst();
          
          if (temp == null)
            return null;
          
          String myString = (String) temp.getValue();
          temp = temp.getNext();
          
          while (temp != null) {
            myString = myString + " " + (String) temp.getValue();
            temp = temp.getNext();
          }
          return myString;
        }
        
        public String recursiveToString(){
          //NEED HELP
        }
        
        public String backwardsString(){
          DoubleLinkedListNode temp = getLast();
          
          if (temp == null)
            return null;
          String myString = (String) temp.getValue();
          temp = temp.getPrev();
          
          while (temp != null) {
            myString = myString + " " + (String) temp.getValue();
            temp = temp.getPrev();
          }
          return myString;
        }
        
        public String recursiveBackwardsString(){
          //NEED HELP
        }
        
        public boolean equals(Object o){
          //NEED HELP
        }
        
        public void copy(DoubleLinkedList<T> otherList){
          //NEED HELP
        }
        
        public void reversedCopy(DoubleLinkedList<T> otherList){
          //NEED HELP
        }
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    What have you attempted?

  3. #3
    CMAC21 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    I haven't come up with anything for the 2 recursive toString methods...
    this is what i tried, but don't think its right, for equals & copy:

    Java Code:
        public boolean equals(Object o){
          if (this == o)
            return true;
          if (! (o instanceof DoubleLinkedList) )
            return false;
          
          if (size != ((DoubleLinkedList)o).size)
            return false;
          
          for (int n1 = front(), n2 = ((DoubleLinkedList)o).front(); n1 != null; n1 = n1.next, n2 = n2.next)
            if (n1.info != n2.info)
            return false;
          
          return true;
        }
        
        public void copy(DoubleLinkedList<T> otherList){
          DoubleLinkedListNode<T> newNode;
          DoubleLinkedListNode<T> current;
          
          first = null;
          
          if(otherList.first == null){
            first == null;
            last == null;
            count = 0;
          }
          else{
            count = otherList.count;
            current = otherList.current;
            first = new DoubleLinkedListNode();
            first.info = current.info.getCopy();
            first.next = null;
            last = first;
            current = current.next;
            
            while(current != null){
              newNode = new DoubleLinkedListNode();
              newNode.info = current.info.getCopy();
              newNode.next = null;
              last.next = newNode;
              last = newNode;
              current = current.next;
            }
          }
        }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    This is not a code service. We will help you with problems in the code you have written. For example:
    Java Code:
    if (n1.info != n2.info)
    99% of the time you will want to use the equals method instead of ==/!= logical operators when comparing objects.

Similar Threads

  1. help me on this: circular doubly linked list
    By someone in forum New To Java
    Replies: 13
    Last Post: 04-11-2011, 10:57 AM
  2. help me with this ! circular doubly linked list
    By someone in forum Advanced Java
    Replies: 1
    Last Post: 04-11-2011, 10:35 AM
  3. Sorted Doubly linked List
    By student2889 in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 10:05 AM
  4. Doubly Linked List
    By matin1234 in forum New To Java
    Replies: 0
    Last Post: 06-02-2010, 05:58 AM
  5. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •