Thatdude1
Thatdude1

Reputation: 903

Deleting items from a structure

I have the following linked list:

struct scoreentry_node {
    struct scoreentry_node *next;
    int score;
    char name[1];    
}
;

typedef struct scoreentry_node *score_entry;

I'm trying to write a function that removes all nodes that contain a certain name. Here is what I have so far but I'm not sure I'm right:

score_entry disqualify(score_entry a, char* name)
{
 score_entry tmp = a;
 while (tmp != NULL){
    if (strcmp(tmp->name, name) == 0)
    {
     score_entry trash = tmp; 
     tmp = tmp->next;    
     free(trash);
    }
    else { tmp = tmp->next; }
  }
return a;
}

It gives me heap error's .. Any suggestions?

Upvotes: 1

Views: 134

Answers (3)

Yaniv
Yaniv

Reputation: 1001

You're using strcmp on a non-null-terminated string (tmp->name). I'm assuming it's not null-terminated as it's only of length 1. Seems like you're really comparing a character, not a string, so a simple character equality operator would be the right thing to do.

Upvotes: 0

William Morris
William Morris

Reputation: 3684

If you delete a member from the list you must fix the gap this creates by linking the 'next' pointer of the previous entry to the following entry. The code below does that. Note that I have changed score_entry so that the typedef no longer contains the pointer - I prefer not to disguise types. Notice that the function returns the head which might have changed if we deleted the first entry in the list.

typedef struct scoreentry_node score_entry;

static score_entry *
disqualify(score_entry *head, const char *name)
{
    score_entry *se = head;
    score_entry *prev = head;

    while (se) {
        if (!strcmp(se->name, name)) {
            score_entry *next = se->next;

            if (head == se) {
                head = next;
            } else {
                prev->next = next;
            }
            free(se);
            se = next;

        } else {
            prev = se;
            se = se->next;
        }
    }
    return head;
}

Upvotes: 0

iehrlich
iehrlich

Reputation: 3592

score_entry disqualify(score_entry a, char* name)
{
  score_entry new_front = a, tmp;
  // delete "wrong" entries from the front
  while (new_front != NULL){
    if (strcmp(new_front->name, name) == 0)
    {
      score_entry trash = new_front; 
      new_front = new_front->next;    
      free(trash);
    }
    else
    {
      // first list entry is valid
      // delete "wrong" entries from inside the list
      tmp = new_front;
      while ( tmp->next != NULL )
      {
        if ( strcmp(tmp->next->name,name)==0 )
        {
          score_entry trash = tmp->next;
          tmp->next = tmp->next->next;
          free(trash);
        } else
        {
          tmp = tmp->next;
        }
      }
    }
 }
return new_front;
}

You should also obtain some book related to common data structures - you seem to be interested in the stuff, and it could be a great help for you.

Upvotes: 3

Related Questions