CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Threaded View

  1. #1
    Join Date
    Jan 2004
    Posts
    131

    Linked List search() and delete() trouble

    Got a bit of trouble figuring out where my code is going wrong for a Linked List in C (not C++)...

    Both removefirst() and freeNode() work correctly. There's also a few lines of stderr output as debugging, so I can tell how far the methods get before the error. It seems to error when trying to delete a node other than the first node in the list.

    nodePtr is a typedef of a nodeType *, and nodeType is a struct: {int info; nodeType * right}.

    Can anyone shed some light on what pointer or variable I've got screwed up? I know I've got the algorithm right, it's just a bit of confusion over which pointer isn't getting the correct value... it seems as though the lptr (lead pointer) and tptr (trailing pointer) aren't being assigned correctly in the search() method, as they seem to always return with their original values (lptr = front, tptr = NULL).

    Code:
    boolean search(nodePtr front, nodePtr * lptr, nodePtr * tptr, infoType target)
    /* return true if target is in the list, false otherwise */
    /* lptr should point to node where target found, and */
    /* tptr should point to the trailing node or be NULL */
    {
        *lptr = front;
        *tptr = NULL;
    
        while ((*lptr != NULL) && (front->info != target))
        {
            tptr = lptr;
            *lptr = (*lptr)->right;
        }
    
        return (lptr != NULL);
    }
    
    void deleteNode (nodePtr * front, infoType target)
    /* delete the node containing target */
    {
        nodePtr tptr, lptr;
        
        if (search(*front, &lptr, &tptr, target) == true)
        {
            fprintf(stderr, "FOUND!\n");
            
            if ((tptr == NULL) && (lptr != NULL))
            {
                fprintf(stderr, "Deleting first node!\n");
                removefirst(front);
            }
            else
            {
                fprintf(stderr, "Deleting node other than first!\n");
                tptr->right = lptr->right;
                freeNode(lptr);
            }
        }
    }
    Last edited by Judas1012; October 21st, 2004 at 09:44 PM.
    Power Macintosh G4/500 PCI
    OS X 10.5.4 • 1024MB RAM • 120GB x 3 • Pioneer DVR-111D CD-RW/DVD-RW • Radeon 7000 PCI x 2, dual 17" Displays

    http://www.jeffhoppe.com

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