1

コードはコンパイルされていますが、コードに論理エラーがあります。配列内の文字列を比較して、リストに順番にリストしたいのですが、インデックスを使用せずにリスト項目を比較する方法と、現在の名前を次の名前と比較する方法がわかりません。誰でも助けることができますか?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
      "Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};


/* declare your struct for a person here */
typedef struct Record{
    char *name;
    int age;
    struct Record *next;
}   Record;

//set the head pointer at the start of the list
Record *headptr = NULL;

int compare_people( Record *a, Record *b)
{
     return strcmp((*(Record *)a).name, (*(Record *)b).name);
}

static void insert (Record *p, char *s, int n) {

    /* create a new space for the new person */
    Record *ptr = ( Record *) malloc(sizeof(Record));

    /* check if it is succeeded  */ 
    if( ptr == NULL){  
        abort();
        printf("memory allocation fail"); 
        exit(1);  
    }else{
        printf("memory allocation to person  - %s - \n", s);      
    }

    //set the data for the new person
    ptr->name=s;
    ptr->age=n;
    ptr->next= NULL;

    //ptr= NULL; 
    //printf("%i", p->age);


    /*  do not compare when the list is empty*/
    if(headptr==NULL)
    {
        ptr->next=headptr;
        headptr=ptr;
        printf("ok1\n");

    }else{
        Record *tail = headptr;

        /* go through all the list */
        while(tail->next!=NULL)
        {     
            if(compare_people(ptr->name,tail->name)== 1){
            tail = tail->next;
        }else{
            tail->next=headptr;
            }
        }//while

        //tail->next=ptr;
    }  
}  

int main( int argc, char **argv) {

    /* declare the people array here */
    Record *p=headptr;
    headptr = NULL;

    //insert the members and age into the unusage array. 
    for (int i=0; i < 7; i++) {
         insert (p,names[i], ages[i]);
         /* do not dereference the pointer */
    }

    /* print out a line before printing the names and ages */
    printf("\n");

    //set the pointer at the start of the list 
    p = headptr;

    /* print the people array here*/
    for ( int i=0; i < 7; i++, p = p->next ) {
        printf("The name is: %s, the age is:%i\n", p->name, p->age);
    }


    /* This is the third loop for call free to release the memory allocated by malloc */
    /* the free()function deallocate the space pointed by ptr. */
    for( int i=0; i<7; i++){
        free(p->next);
    } 
}
4

4 に答える 4

1

このコードは正しくありません:

Record *tail =headptr;
/* go through all the list */
while(tail->next!=NULL)
{   
  if(compare_people(ptr->name,tail->name)== 1){
    tail = tail->next;
  } else {
    tail->next=headptr;
  }
} //while

の後に何かを挿入したい場合はtail、設定するだけで、(a)tail->next = headptr現在の後に続くものをすべてリークしtail、(b)リンクリストを終わりのないループに変えることができます。

リストに挿入したい場合ptrは、おそらく次のようなことを行う必要があります

ptr->next = tail->next; 
tail->next = ptr; 

...そしてループから抜け出します。

于 2012-11-26T14:26:46.390 に答える
1

最初の大きな問題は次のとおりです。

Record *tail =headptr;
/* go through all the list */
while(tail->next!=NULL) {
...

while()このループに陥ることはありません。最初の反復でこれを行いました:

ptr->next= NULL;  // setting the pointer's next pointer to NULL
...
headptr=ptr;     // have headptr point at what ptr is pointing to

つまり、headptr->nextになりますNULL。次に、上記のコード スニペットで に設定tailするとheadptr、そのループは実行されなくtail->nextなりNULLます。

2番目の大きな問題は次のとおりです。

if(compare_people(ptr->name,tail->name)== 1){

この関数に文字列を渡していますが (record->name は文字列です)、関数自体では次のように設定しています。

int compare_people(Record *a, Record *b)

レコード ( ではないchar *) を入力として受け取ります。最初の問題を修正して実際にこの機能に到達すると、これはあなたを殺します。

于 2012-11-26T14:36:49.897 に答える