2017-11-12 16 views
-1

私はC++で簡単な関数を書いています。リストの先頭に挿入

私のコードでエラーが発生します。出力はありません。

また、私は次のインデックスが記憶された後に無料で電話する必要があります。 私はそれを行う方法を知らない。 printfの後にwhileループ内で電流を解放する必要がありますか?

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]); 
} 

ここにあなたが以下のようなあなたのinsert方法を変更する必要が私のコード

#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; 
} 




} 
+1

「自分のコードでエラーが発生します。 – DimChtz

+0

現在の値にnullを割り当て、whileループでそれをテストします。 –

+0

警告:互換性のないポインタ型からの割り当て[デフォルトで有効] ptr-> next = headptr; ^ lists.c:関数 'main': lists.c:52:14:警告:互換性のないポインタ型からの割り当て[デフォルトで有効] current = current - > next; –

答えて

-2

あなたのリストを以下のように印刷する必要があります: -

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; 
} 
+0

こんにちは、出力はありません。mainメソッドでは、forループ内のinsertメソッドinsert(headptr、*(names + index)、age [index])をリスト全体に渡します。次に、条件付きのforループで印刷します(current = headptr; current!= NULL; current = current-> next)。たぶんheadptrがこの時点でヌルなので、forループはまったく動かないでしょうか? –

+0

@ P.Olandland、あなたの印刷ロジックが正しくありませんでした。私はそれを修正する。今私に知らせてみてください。 –

+0

代わりに 'insert()'の第1引数を 'person **'として宣言し、 '&headptr'に呼び出し元を渡す必要があります。これにより、 'insert()'が呼び出し元の変数を変更できるようになります。 –

0

コードにはいくつかの問題があります。

  • 構造体のnextフィールドは、不明な型を使用して宣言されています。

  • insert()機能がmain()headptr変数を更新していません。

  • main()current変数は代わりにheadptrのNULLに初期化し、そうするためのforループは何もありませんので、あなたは何も出力を取得していません。

  • 割り当てられたメモリがリークしています。

より代わりにこのような何か試してみてください:リンクリストの先頭に挿入するには

#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

を、私は、あなたがリストの先頭へのポインタではなく、頭を渡すことを示唆していますこの例のようになります(メモリから書かれていますが、うまくいくはずです):

#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); 
} 
関連する問題