Jump to content

DoubleyLinked list

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Hello again, Can anyone explain how to make this more efficient in terms of O(n). Also I keep making a mistake with the delete method in the Doubley class and have deleted it. It is not deleting correctly using the doubly linked list, it would continually throw a null pointer exception or it would not delete the object. Can someone write the method with an explanation, I would appreciate it, this will help me next week when I have to do this in C(ansi) and it will help with a personal project I am building.

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.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
nodeA, nodeB, nodeC
If you want to delete nodeB, you just have to set the next of nodeA to nodeC, and the previous of nodeC to nodeA.
Because nothing is referencing nodeB anymore, garbage collector will eventually get rid of it.
Judging by the rest of your code, you should be perfectly capable of dong this, what's not working?

Edit: oh you'll also have to check that, if the node you're deleting == head then you must set the head to deleteNode.next.
Ditto with tail, if(deleteNode==tail){ tail = deleteNode.prev;}
Also there is a possible nullpointerexception in the lookup method:

if (find.data != null || findRev.data != null) {

    if (find.data.equals(x) || findRev.data.equals(x)) {

imagine if find.data is not null, and findRev.data IS null. This will pass the first if-statement.(true or false -->true)
and if the data in 'find' is not equal to x, it will result in a nullpointer because it's gonna test the data in findRev.
You either want to use &&. Or, possibly better yet longer code, test it separatly.

Other note: why does lookup return Object, and not boolean?
Tiiiny note: i would change the name "lookup" to "contains"

Also, i'm not sure if it's actually faster to go look from left and right. Sure, there are less loops, but you do twice as much in 1 loop as when you would do it 1 by 1.
Its speed will heavily depend on where the object is, the more it is to the end, the faster your method will be compared to doing it 1 by 1. The more the object is to the middle/front, 1by1 would/should be faster.

#3
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Thanks for the help and suggestions.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users