单独链接列表结构插入/删除多个免费

时间:2011-04-23 04:49:17

标签: c struct malloc free

看了这段代码已经太久了,我很郁闷任何把自己弄清楚的机会已经丢失了:(任何人都可以告诉我我在哪里傻了?我只是不明白我在哪里双重释放或者可能分配不正确(我必须这样做但是是的)。我一直在检测 * glibc * free():下一个尺寸无效

我实际上是否释放了超过我需要的东西,或者我只是不分配我需要分配的东西。 - 不良缩进的吸收无法让这个编辑器正确缩进

我有结构:

typedef int boolean;
typedef char * String;

typedef struct {
    char name[30];
    long ID;
    char address[40];
    char city[20];
    int age;
}Employee;

typedef struct node {
   Employee *anEmployee;
   struct node *next;
}NODE;

typedef struct {
   NODE *head, *tail;
}SLL;

插入功能 - SLL(单链表)

void insert(SLL *list, Employee e){
  printf("insert");

   NODE *temp, *current;

   temp = (NODE *)malloc(sizeof(NODE));
   assert(temp != NULL);

   temp -> anEmployee = (Employee *)malloc(sizeof(Employee *));
   assert(temp -> anEmployee != NULL);

   strcpy(temp -> anEmployee -> name, e.name); 

   temp -> anEmployee -> ID = e.ID;

   strcpy(temp -> anEmployee -> address, e.address); 

   strcpy(temp -> anEmployee -> city, e.city);

   temp -> anEmployee -> age = e.age;

 if (list -> head == NULL) {     /* list is empty */
  list -> head = list -> tail = temp;
  return;
   }
   else { // list is not empty
       list -> tail -> next = temp;
       list -> tail = temp;
          }
 }

删除和释放删除功能中的内存

boolean delete(SLL *list, String str){
  printf("delete");
  NODE *temp, *temp2;
  if (list -> head == NULL) return FALSE;  // list is empty

temp = list -> head;

while ((temp != NULL) && (strcmp(temp -> anEmployee -> name , str) != 0))
   temp = temp -> next;

if (temp == NULL) return FALSE;  // str is not found in the list

// now temp points to the NODE with str in it.  Let us delete it
// from the list

    if ( list -> head == temp) { // temp points to the first NODE
       if (temp -> next == NULL) {  // temp points to the only NODE
           list -> head = list -> tail = NULL;
           free(temp -> anEmployee);
           free(temp);
           return TRUE;
     }
     // temp points to the first NODE but it is not the only NODE
     list -> head = temp -> next;
     free(temp -> anEmployee);
     free(temp);
     return TRUE;
  }

  if (temp -> next == NULL) {  // temp points to the last NODE
      temp = list -> head;
      temp2 = list -> head -> next;
      while(temp2 - > next != NULL){
        temp = temp->next;
        temp2 = temp2 ->next;
    }


       list -> tail = temp ;    
       list -> tail -> next = NULL;
       free(temp2 -> anEmployee);
       free(temp2);
       return TRUE;
  }
       // temp points to some NODE in the middle of the list
      temp2 = temp -> next;
 while(temp2 - > next != NULL){

    temp ->anEmployee = temp2 - > anEmployee // 
    temp = temp->next;
    temp2 = temp2 ->next;
}
    temp ->anEmployee = temp2 - > anEmployee

   list -> tail = temp ;    
   list -> tail -> next = NULL;
   free(temp2 -> anEmployee);
   free(temp2);
   return TRUE;
 }

3 个答案:

答案 0 :(得分:1)

首先,在insert中,您正在分配

temp -> anEmployee = (Employee *)malloc(sizeof(Employee *));

只分配足够的内存来保存Employee 指针,而不是整个Employee结构。您应该为sizeof(Employee)

分配大小为temp->anEmployee.的块

如果您想要释放freesomeNode->anEmployee以完全清理单个节点占用的内存,那么您对someNode的来电是有意义的。

您可以按如下方式简化delete实施:

boolean delete(SLL* list, String str)
{
    NODE* temp = list->head, *prev = NULL;
    while(temp != NULL && strcmp(temp->name, str) != 0) {
        prev = temp;
        temp = temp->next;
    }
    if(temp == NULL)
        return FALSE;

    if(prev != NULL)
        prev->next = temp->next;

    if(list->head == temp)
        list->head = temp->next;

    if(list->tail == temp)
        list->tail = temp->next;

    free(temp->anEmployee);
    free(temp);
    return TRUE;
}

通过跟踪查找之前的节点(如果有),您可以避免所有令人讨厌的特殊情况,并将核心列表更新减少为三个简单的条件分配。

答案 1 :(得分:0)

没有阅读全部内容,delete应该做的第一行是什么?

  NODE *temp, *temp2; // temp is uninitialized

    if ( list -> head == temp) { // temp points to the first NODE

我无法分辨delete应该删除的内容。似乎str参数未使用。是否要根据str搜索特定记录,将temp设置为指向该记录,然后继续显示所示代码?

答案 2 :(得分:0)

在insert()中,当你分配temp->anEmployee时,你只为指针分配足够的空间,而不是完整的Employee。

这一行:

   temp -> anEmployee = (Employee *)malloc(sizeof(Employee *));

应该是:

   temp -> anEmployee = (Employee *)malloc(sizeof(Employee));