HERE IS THE NODE CLASS
public class MyDoubleNode {
public Object data;
public MyDoubleNode next, prev;
}
HERE IS THE INTERFACE
public interface MyDoubleLinkedList {
public void insert(Object x);
public void delete(Object x);
public Object lookup(Object x);
public boolean isEmpty();
public void printList();
public void printListRev();
}// end of interface
HERE IS THE TEST CLASS
public class DoublyTest {
/**
* Main method used for testing
* @param args
*/
public static void main(String[] args){
Doubley odll = new Doubley();
odll.insert("1");
odll.insert("2");
odll.insert("3");
odll.insert("4");
odll.insert("5");
odll.printList();
System.out.println("");
odll.printListRev();
System.out.println("");
System.out.println("\nIs \"How\" in the list: " + odll.lookup("How"));
System.out.println("\nIs \"2\" in the list: " + odll.lookup("2") + "\n");
System.out.println("");
}// end of main
}// end of class
HERE IS THE CLASS WITH THE METHODS most Notably the Object delete(Object x) method
public class Doubley implements MyDoubleLinkedList {
private MyDoubleNode head;
private MyDoubleNode tail;
public Doubley() {
head = null;
tail = null;
}
/**
* Insert item into list from back to front
* @param x an object
*/
public void insert(Object x) {
MyDoubleNode insertIt = new MyDoubleNode();
insertIt.data = x;
// check if list is empty
if (isEmpty()) {
head = insertIt;// first item in list
} else {
// move the line
tail.next = insertIt;// this is the old object at the end of line
insertIt.prev = tail;// this is the new object at the end of line
}// enf of if else
tail = insertIt;
}// end of insert method
/**
* This is a method to delete an object, I cannot fix it
* @param x
*/
public void delete(Object x) {
// I have to redo this method didnt account for null pointer
// and I tried to do a different method.. which failed
}// end of delete
/**
* Lookup an object from the left and right
* @param x
* @return "true" or "false"
*/
public Object lookup(Object x) {
MyDoubleNode find = head;
MyDoubleNode findRev = tail;
do {
if (find.data != null || findRev.data != null) {
// check the ends for object
if (find.data.equals(x) || findRev.data.equals(x)) {
return true;
}// end of inner if
} // end of if
find = find.next; // increment up to the next node
findRev = findRev.prev; // go to prev node
} while (find != null && findRev != null);
return false;//not found
}//end of lookup
/**
* Check if head is empty
* @return null
*/
public boolean isEmpty() {
return head == null;
}// end of isEmpty
/**
* PrintList forwards
*/
public void printList() {
MyDoubleNode print = head;
System.out.println("The list:");
while (print != null) {
System.out.print(print.data + " ");
print = print.next;
}// end of while
}// end of printlist
/**
* Print list in reverse
*/
public void printListRev() {
MyDoubleNode printRev = tail;
System.out.println("\nThe list in reverse: ");
while (printRev != null) {
System.out.print(printRev.data + " ");
printRev = printRev.prev;
}// end of while
}// end of printListRev
}//end of class
Thanks to all.


Sign In
Create Account


Back to top









