Doubly Linked List removeCurrent quest.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Doubly Linked List removeCurrent quest.

  1. #1
    Join Date
    Oct 2010
    Posts
    10

    Doubly Linked List removeCurrent quest.

    I have a doubly linked list I need to remove current from. I've been staring for hours; I cant seem to remove the element & reestablish the links; any help would be great.
    I hope I did the 'tags' right?

    Code:
        public void removeCurrent()
            {
                if (isCurrent() == false)
                throw new IllegalStateException
                    ("removeCurrent: isCurrent() is null");
                
                else if ((isCurrent() == true) && (cursor.getNextNode() == null))
                {
                tail = cursor;
                cursor = null;
                }
                else
                {
                cursor.getPrevNode().setNextNode(getNextNode()); //error here? don't know if this is
                //correct either
                
                manyNodes--;   
                }   
                }
        public DoubleNode getPrevNode(){ //these two things are in my other file
            return prev;
        }
        public void setPrevNode(DoubleNode dn){
            prev = dn;
        }

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

    Re: Doubly Linked List removeCurrent quest.

    For a start it's bad coding style to test if a boolean value is equal to true or false you should do:
    Code:
    if ( isCurrent() ) { ... }
    or
    Code:
    if ( !isCurrent() ) { ... }
    To remove a node you set the previous node to this node's next node reference and the next node to this node's previous node reference. For instance assume we have 3 nodes numbered 1 to 3 and we want to remove node 2.

    Code:
    node 1
      .next = 2
      .prev = null
    
    node 2
      .next = 3
      .prev = 1
    
    node 3
      .next = null
      .prev = 2
    To do this we get node 2's previous node (node 1) and set its next reference to nodes 2's next reference (node 3) ie
    Code:
    node 1
      .next = 3
      .prev = null
    
    node 2
      .next = 3
      .prev = 1
    
    node 3
      .next = null
      .prev = 2
    Then we get node 2's next node (node 3) and set its prev reference to node 2's prev reference ie
    Code:
    node 1
      .next = 3
      .prev = null
    
    node 2
      .next = 3
      .prev = 1
    
    node 3
      .next = null
      .prev = 1
    Node 2 has now been removed from the linked list. If you have a current node marker you must move this either to the prev or next node depending on your design and whether or not the removed node is at one end of the list (in which case you must also move the tail and head markers if you have them). If any other code has a reference to this node (which if your design is good won't be the case) you must also null the removed node's next and prev references otherwise the code can use this removed node to illegally access/alter the list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center