CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Exclamation doubly linked list insert HElP

    i need help with the insert method, i can't get it working if the timestamp is lesser than the current

    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().getTimestamp()) < 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().getTimestamp()) == 0)
    {
    // remove the node
    result = true;
    }
    else
    {
    current = current.getNext();
    }
    }
    return result;
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: doubly linked list insert HElP

    Please use code tags when posting code and please explain exactly what your code is supposed to do and what it is actually doing.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured