2012-11-25 70 views
1

在此程序中,我想將元素放入列表中而不是數組中。最後打印出列表。將數組項放入列表中並打印出列表項C

例如。 西蒙22 蘇絲24
...

不過,我真的不知道如何操作的列表,以及如何建立堆和檢索。我做了一些關於如何去做的研究。這就是我所想的。 而一些錯誤出來了,我不知道如何解決。

error: 'ptr' undeclared (first use in this function) 
arrays.c:37:5: note: each undeclared identifier is reported only once for each function  it appears in 
arrays.c: In function 'main': 
arrays.c:62:9: error: expected identifier or '(' before '=' token 
arrays.c:69:5: warning: passing argument 1 of 'insert' from incompatible pointer type 
arrays.c:28:13: note: expected 'struct Record *' but argument is of type 'struct Record **' 

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


/* these arrays are just used to give the parameters to 'insert', 
    to create the 'people' array */ 
char *names[7]= {"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 *names; 
    int ages; 
    struct Record *next; 
} Record; 

char getname(Record *names){ 
    return names; 
} 

int getage(Record *ages){ 
    return ages; 
} 

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

//p[(*)] = malloc(sizeof(person)); 

/*static int nextfreeplace = 0;*/ 
    Record *headptr = NULL; 

    while(!reached_eof(p)){ 

/* allocate heap space for a record */ 
     ptr =(Record*) malloc(sizeof(Record)); 

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

     ptr->name = getname(p); 
     ptr->age = getage(p); 

     /* link new object into the list */ 
     ptr->next = headptr; 
     headptr = ptr; 

    } 
}  


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

    /* declare nextinsert */ 
    int = 0;    

    /* 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"); 


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


    /* 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[i]); 
    } 
} 

回答

3

的所有

下面的代碼首先是奇怪

char getname(Record *names){ 
    return names; 
} 

int getage(Record *ages){ 
    return ages; 
} 

我沒有看到上述功能的實際需要。

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

//p[(*)] = malloc(sizeof(person)); 

/*static int nextfreeplace = 0;*/ 
    Record *headptr = NULL; 

    while(!reached_eof(p)){ 
/* allocate heap space for a record */ 
ptr =(Record*) malloc(sizeof(Record)); 

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

爲什麼你使用while循環:

即使這些線路

ptr->name=getname(p); 
ptr->age=getage(p); 

你可以用

ptr->name=s; 
ptr->age=n; 

下面的函數包含很多錯誤和奇怪的代碼relace他們。並且你錯過了ptr指針的定義,並且你有註釋在函數結尾處傳達了neaw頭。在此之後,你如何解決這個問題:

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

    Record *ptr; 
    ptr =(Record*) malloc(sizeof(Record)); 

    if(ptr == NULL){ 
     abort(); 
     printf("memory allocation fail"); 
     exit(1); 
    }else{ 
     printf("memory allocation to person - %s - \n", s);  
    } 
    ptr->name=s; 
    ptr->age=n; 

    /* link new object into the list */ 
    ptr->next=*header; 
    *headptr=ptr; 
} 

而在你的主要功能:

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


    int i= 0; 
    Record *p, *headptr=NULL; 

for (int i=0; i < 7; i++) { 
insert (&headptr, names[i], ages[i]); 
/* do not dereference the pointer */ 
    } 

for (int i=0; i < 7; i++) { /* this will print from array*/ 
    printf("From array The name is: %s, the age is:%i\n", p[i]->names, p[i]->ages); 
} 

for (p=headptr; p!=NULL; p=p->next) { /* this will print from linked list*/ 
    printf("From linked list The name is: %s, the age is:%i\n", p->names, p->ages); 
} 


} 
+0

代碼是否會讀取列表的下一個元素? – user1851359

+0

固定代碼將inserrt您的元素在列表的頭部 – MOHAMED

+0

代碼更新以讀取和打印鏈接列表的內容 – MOHAMED

0

我想唯一的錯誤是你錯過了定義指針,然後再使用它與malloc函數。在你的代碼定義指針

Record *ptr ; 

而且一個地方有以下文字:

int = 0; 

這是第二個錯誤。

而你傳遞一個NULL指針p在您的for循環

編輯這些插入功能,而且他們現在必須修正錯誤。

+0

歡呼,減少錯誤。 – user1851359

+0

我更新了答案@ user1851359 – cipher