Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Unhappy doubly linked list insert

    public class DoubleList
    {
    private DoubleNode head;
    private DoubleNode tail;

    public DoubleList()
    {
    head = null;
    tail = null;
    }

    public boolean isEmpty()
    {
    return head == null;
    }

    public DoubleNode getHead()
    {
    return head;
    }

    public void insertHead(DoubleNode node)
    {
    if (isEmpty())
    {
    node.setPrev(null);
    node.setNext(null);
    head = node;
    tail = node;
    }
    else
    {
    head.setPrev(node);
    node.setPrev(null);
    node.setNext(head);
    head = node;
    }
    }

    public void insertTail(DoubleNode node)
    {
    if (isEmpty())
    {
    node.setNext(null);
    node.setPrev(null);
    head = node;
    tail = node;
    }
    else
    {
    tail.setNext(node);
    node.setNext(null);
    node.setPrev(tail);
    tail = node;
    }
    }

    public void deleteHead()
    {
    if (head.getNext() == null)
    {
    head = null;
    tail = null;
    }
    else
    {
    head.getNext().setPrev(null);
    head = head.getNext();
    }
    }

    public void deleteTail()
    {
    if (head.getNext() == null)
    {
    head = null;
    tail = null;
    }
    else
    {
    tail.getPrev().setNext(null);
    tail = tail.getPrev();
    }
    }

    public boolean insert(Event event)
    {
    DoubleNode newNode = new DoubleNode(event);
    String timestamp = event.getTimestamp();
    DoubleNode current = head;
    boolean result = false;

    while (current != null)
    {
    if (timestamp.compareTo(current.getEvent().getTimesta mp()) < 0)
    {
    if (current == head)
    {
    insertHead(newNode);
    }
    else if (current == tail)
    {
    insertTail(newNode);
    }
    else
    {
    // insert the new node before the current node
    newNode.setNext(current);
    newNode.setPrev(current.getPrev());
    current.getPrev().setNext(newNode);
    current.setPrev(newNode);
    }
    result = true;
    }
    else
    {
    current = current.getNext();
    }
    }

    if (current == null)
    {
    insertTail(newNode);
    result = true;
    }

    return result;
    }

    public boolean delete(Event event)
    {
    DoubleNode current = head;
    boolean result = false;
    String timestamp = event.getTimestamp();

    while (current != null)
    {
    if (timestamp.compareTo(current.getEvent().getTimesta mp()) == 0)
    {
    // remove the node
    result = true;
    }
    else
    {
    current = current.getNext();
    }
    }
    return result;
    }
    }

    please i need help with the insert method, i can't get it working

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Taking a brief look, I don't see how you exit the while loop once you insert the node. Given that all your if's are flattened (use [ code ]...[ /code ]), I really can't see the logic there, either.

    While a lot of people hate this, I suggest using while(true) and break. It eliminates a lot of nesting, and you know that you are leaving the loop.

Similar Threads

  1. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  2. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  3. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 08:45 AM
  4. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:30 PM
  5. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 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
  •