在列表的开头插入

时间:2017-11-12 18:46:12

标签: c linked-list

我正在用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: %i\n",current -> name, current-> age);
        // Now move to the next item in the linked list
  current= current -> next;
}




}

3 个答案:

答案 0 :(得分:0)

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

  • 您的struct的next字段是使用未知类型声明的。

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

  • 由于current中的main()变量初始化为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: %i\n", 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;
}

答案 1 :(得分:-1)

要在链接列表的前面插入,我建议您将指针传递给列表的头部,而不是头部,从而产生类似于此示例的内容(从内存中写入,但应该有效):

#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("%d\n", 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);
}

答案 2 :(得分:-2)

您需要修改以下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: %i\n",current -> name, current-> age);
    // Now move to the next item in the linked list
  current= current -> next;
}