Thread: Deleting nodes from a linked list

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Deleting nodes from a linked list

    Hello! I'm struggling a bit with the function of deleting a node from a linked list. I've made an attempt, and it works fine if the node being deleted is the first node. However, if I want to delete any other node, the function hangs and just starts printing off whatever I input into they keyboard. I've included my node definition and the two functions I was trying to implement.

    Thanks in advance!

    Code:
    typedef struct node
    {
           char last[1024];
           char first[1024];
           char position;
           int value;
           struct node *link;
    }Node;
    Code:
    Node* deleteNode(Node *head)
    {
    char lastName[MAX_LENGTH+1];
    safegets(lastName,MAX_LENGTH+1);
    bool found = false;
    if (head == NULL)
    	return NULL;
    
    if (strcmp((head->last),lastName)==0)
    	{
    	Node *temp = head->link;
    	free(head);
    	return temp;
    	}
    Node *current = head;
    
    while (!found && (current->link)!=NULL)
    	{
    	if (strcmp((current->link->last),lastName)==0)
    		found==true;
    	else 
    		current=current->link;
    	}
    
    if (current->link!=NULL)
    	{
    	Node *temp = current->link;
    	(current->link)=(current->link->link);
    	free(temp);
    	return head;
    	}
    
    
    else if (current->link==NULL)
    	{
    	if(strcmp((current->last),lastName)==0)
    		deleteLast(head);
    	}
    	return head;
    }
    
    void deleteLast(Node *head)
    {
    Node *current = head;
    while (current->link->link != NULL)
    	current=current->link;
    free(current->link);
    current->link=NULL;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For starters, lastName is a local variable and you don't ever prompt the user to enter one, so it is left uninitialized. Your comparisons will fail since there's garbage data in lastName, thus it will never find the person to delete. Make lastName a parameter to be passed into delete, so you call deleteNode(list_of_people, lastname_of_person_to_delete).

    Your delete code looks more or less fine, if a bit unconventional. But it's hard to say without enough code to compile and test your function. A main with some test data or input routines and an insertNode would be great.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    when you found the node to be delete, you should break out the while,
    Code:
    while (!found && (current->link)!=NULL)
    	{
    	if (strcmp((current->link->last),lastName)==0)
                    {
    		found==true;
                                    break;
                    }
    	else 
    		current=current->link;
    	}

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Jesius View Post
    when you found the node to be delete, you should break out the while,
    Code:
    while (!found && (current->link)!=NULL)
    	{
    	if (strcmp((current->link->last),lastName)==0)
                    {
    		found==true;
                                    break;
                    }
    	else 
    		current=current->link;
    	}
    The !found in the while condition should take care of that, no need to break.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, but I just realized, you need to remove one of the equals signs from:
    Code:
    		found==true;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 04:13 AM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM