0

Most of code works but when I try to delete last element of my list and I print it I see in its place some garbage data, what I'm doing wrong here? Could somebody point my mistake?

void DeleteClient2(struct client *temp,struct client **head)
{   struct client *prev=*head;
    struct client *current = *head;
    struct item *currentitem = (*head)->item_data,*save;
    if(temp== *head)
    {
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        free(temp);
        temp->item_data = NULL;
        (*head) = (*head)->next;
    }
    else
    if(temp->next == NULL)
    {
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
    }
    else
    if(temp != *head && temp->next != NULL)
    {
        while(prev->next != temp)
        {
            prev=prev->next;
        }
        prev->next = temp->next;
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
        temp=temp->next;
    }
}
1
  • 1
    first indent your code correctly. Commented Jan 23, 2014 at 6:14

3 Answers 3

2

Code:

    free(temp);
    temp=temp->next;  // temp->next is invalid

Is undefined behaviour, Once you free a node you can't access it, doing so is illegal.

Sign up to request clarification or add additional context in comments.

2 Comments

Than how should I link previous node and next node ?
@user3209183 keep Save previous address
0
void DeleteClient2(struct client *temp,struct client **head)
{   struct client *prev=*head;
    struct client *current = *head;
    struct item *currentitem = (*head)->item_data,*save;
    if(temp== *head) \\ if head is the node to be deleted
    {
        while(currentitem != NULL)
        {
            save = currentitem;
           *head = currentitem->next;
          \\  currentitem = currentitem ->next;
            free(save);
        }

    }
    else
    if(temp->next == NULL) \\ if the node to be deleted is last node then
    {
    while(currentitem->next != NULL)
        {
           *prev = currentitem;
            currentitem = currentitem ->next;

        }
        prev->next = NULL;
        free(currentitem);
}
else
if(temp != *head && temp->next != NULL)\\ node to be deleted is between head and last node
{while(prev->next != temp)
{   currentitem = *prev;
    prev=prev->next;
}
currentitem->next = temp->next;
free(temp);
\\while(currentitem != NULL)
    \\    {
    \\        save = currentitem;
    \\        currentitem = currentitem ->next;
    \\\        free(save);
    \\    }
      \\  temp->item_data = NULL;
     \\   free(temp);
     \\   temp=temp->next;
}
}

1 Comment

there may some mistake in my code but you can try this idea(the changes i have made
0

Here you check that *head == temp and if yes, you free temp i.e *head cause they equal, you just verified it, and than you say (*head) = (*head)->next; which set *head to garbage most probably

if(temp== *head)
{
    while(currentitem != NULL)
    {
        save = currentitem;
        currentitem = currentitem ->next;
        free(save);
    }
    free(temp);
    temp->item_data = NULL;
    (*head) = (*head)->next;
}

Same mess is here

    temp->item_data = NULL;
    free(temp);
    temp=temp->next;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.