Help with deleting node in the middle of a linked list

This is a discussion on Help with deleting node in the middle of a linked list within the C Programming forums, part of the General Programming Boards category; I am trying to delete a node in the middle of my linked list. I am having some trouble...anyone see ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    Help with deleting node in the middle of a linked list

    I am trying to delete a node in the middle of my linked list. I am having some trouble...anyone see where my problem is?

    Code:
    void *cancelFlight(struct llnode **flight, char firstName[], char lastName[])
    {
        struct llnode *newlist; 
        struct llnode *current;
        int stop = 0;
        
        current = (struct llnode*) malloc(sizeof(struct llnode));
        newlist = (struct llnode*) malloc(sizeof(struct llnode));
        
        current = *flight;
        newlist = *flight;
        
        if(strcmp(current->lastName, lastName) == 0 && strcmp(current->firstName,firstName) == 0)
        {
            newlist = newlist->next;
            *flight = newlist;                         
        }
        else
        {
            while(stop == 0)
            {
                current = current->next;
                if(strcmp(current->lastName, lastName) == 0 && strcmp(current->firstName,firstName) == 0)
                {
                    current = current->next;
                    newlist->next = current;
                    *flight = newlist;
                    
                    stop = 1;                        
                }       
            }
                
        }
    
        
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > current = (struct llnode*) malloc(sizeof(struct llnode));
    > newlist = (struct llnode*) malloc(sizeof(struct llnode));
    What are these for?
    Why are you casting malloc (see the FAQ)

    > while(stop == 0)
    All linked list code should have as a minimum
    while ( current != NULL )

    > I am trying to delete a node in the middle of my linked list
    The essence of which is being able to perform the assignment
    prev->next = current->next;
    free( current );

    That is, you make the previous node point past the current node (rather than to the current node), then delete the current node.

    The tricky bits are handling say the first node in the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
            temp = (*HEAD)->link;
            prev = *HEAD;
            while(temp->data != data)
            {
                    temp = temp->link;
                    prev = prev->link;
            }    
                    
            prev->link = temp->link;
            free(temp);
    this is just a sample code which delete the node. You can where u have made the mistake

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21