Results 1 to 4 of 4
- 05-12-2011, 03:18 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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 } }
- 05-12-2011, 03:45 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
What have you attempted?
- 05-12-2011, 03:53 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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; } } }
- 05-12-2011, 05:07 AM #4
Similar Threads
-
help me on this: circular doubly linked list
By someone in forum New To JavaReplies: 13Last Post: 04-11-2011, 10:57 AM -
help me with this ! circular doubly linked list
By someone in forum Advanced JavaReplies: 1Last Post: 04-11-2011, 10:35 AM -
Sorted Doubly linked List
By student2889 in forum New To JavaReplies: 1Last Post: 10-14-2010, 10:05 AM -
Doubly Linked List
By matin1234 in forum New To JavaReplies: 0Last Post: 06-02-2010, 05:58 AM -
Help with Doubly linked list
By Dr Gonzo in forum New To JavaReplies: 5Last Post: 12-06-2008, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks