c - 在列表开头插入



我正在用c ++编写一个简单的函数。

我的代码出现了一些错误。没有输出。

另外,我需要在记住下一个索引的位置后调用免费。我不知道该怎么做。在printf之后,我需要释放while回路内的电流吗?这是我的代码

#include <stdio.h>
#include<stdlib.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array
*/
#define HOW_MANY 7
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
          "Harriet"};
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef  struct{
  char*  name;
  int   age;
   struct person *next;
}
person;
static void insert( person *headptr, char *name, int age)
{
    person *ptr=malloc(sizeof(person));
            if(ptr==NULL) abort();
            //assign to structure field
    ptr->name = name;
    ptr->age = age;
            //link new object into the list
            ptr->next=headptr;
            headptr=ptr;
}

int main(int argc, char **argv)
{
/* declare the people array here */
person *headptr=NULL;
// Make a copy of the pointer to the head item in the list
for (int index=0;index < HOW_MANY;index=index+1)
    {
  insert(headptr, *(names+index), ages[index]);
}
person *current=NULL;
    // current will be set to NULL when it reaches the end
while(current != NULL)
    {
  // print out the item information
  printf("name: %s, age: %in",current -> name, current-> age);
        // Now move to the next item in the linked list
  current= current -> next;
}


}

您的代码中存在几个问题。

  • 结构的next字段是使用未知类型声明的。

  • 您的insert()函数未更新 main() 中的headptr变量。

  • 你没有得到任何输出,因为main()中的current变量被初始化为NULL而不是headptr,所以循环没有什么可做的。

  • 您正在泄漏分配的内存。

尝试更多类似的东西:

#include <stdio.h>
#include <stdlib.h>
/* these arrays are just used to give the parameters to 'insert', to create the 'people' array */
#define HOW_MANY 7
char* names[HOW_MANY] = {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", "Harriet"};
int ages[HOW_MANY] = {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct person {
    char* name;
    int age;
    struct person *next;
} person;
static void insert(person **headptr, char *name, int age) {
    person *ptr = malloc(sizeof(person));
    if (!ptr) abort();
    //assign to structure fields
    ptr->name = name;
    ptr->age = age;
    //link new object into the list
    ptr->next = *headptr;
    *headptr = ptr;
}
int main(int argc, char **argv) {
    /* declare the people array here */
    person *headptr = NULL;
    // insert items at the head of the list
    for (int index = 0; index < HOW_MANY; ++index) {
        insert(&headptr, names[index], ages[index]);
    }
    person *current = headptr;
    // current will be set to NULL when it reaches the end
    while (current) {
        // print out the item information
        printf("name: %s, age: %in", current->name, current->age);
        // Now move to the next item in the linked list
        current = current->next;
    }
    // free the items
    current = headptr;
    while (current) {
        person *next = current->next;
        free(current);
        current = next;
    }
    return 0;
}

要在链表的前面插入,我建议你将指针传递给列表的头部,而不是头部,结果类似于此示例(从内存编写,但应该可以工作(:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
  struct list *next;
  int value;
} list;
void list_push(list **phead, int value) {
  list *front = malloc(sizeof(struct list));
  front->next = *phead;
  front->value = value;
  *phead = front;
}
bool list_insert(list **phead, int value, int index) {
  list **ptr = phead;
  while (index-- > 0) {
    if (!*ptr) return false;
    ptr = &(*ptr)->next;
  }
  list_push(ptr, value);
  return true;
}
void list_dump(list *head) {
  while (head) {
    printf("%dn", head->value);
    head = head->next;
  }
}
void main(void) {
  list *head = NULL; // empty list
  list_push(&head, 23);
  list_insert(&head, 42, 1);
  list_insert(&head, 13, 0);
  list_dump(head);
}

您需要修改insert方法,如下所示:-

static person* insert(person *headptr, char *name, int age)
{
    if (headptr == NULL) {
        headptr = (person*)malloc(sizeof(person));
        headptr->name = name;
        headptr->age = age;
        headptr->next = NULL; 
    }
else{
    person *ptr = (person*)malloc(sizeof(person));
    if (ptr == NULL) abort();
    //assign to structure field
    ptr->name = name;
    ptr->age = age;
    //link new object into the list
    ptr->next = headptr;
    headptr = ptr;
  }
       return headptr;
}

现在您需要调用上述方法,如下所示:-

// Make a copy of the pointer to the head item in the list
for (int index=0;index < HOW_MANY;index=index+1)
{
   headptr = insert(headptr, *(names+index), ages[index]);
}

现在您需要打印如下列表:-

person *current=headptr;//you need to point to the first node of the list
// current will be set to NULL when it reaches the end
while(current != NULL)
{
  // print out the item information
    printf("name: %s, age: %in",current -> name, current-> age);
    // Now move to the next item in the linked list
  current= current -> next;
}

最新更新