2012-11-25 4 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);  
} 

:

도이 라인은

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

고정 코드는 목록의 머리에 요소를 삽입합니다. – MOHAMED

+0

링크 된 목록의 내용을 읽고 인쇄하도록 코드가 업데이트되었습니다. – MOHAMED

0

malloc 함수와 함께 사용하기 전에 포인터를 정의하지 못한 유일한 오류라고 생각합니다. 두 번째 오류의

int = 0; 

: 당신의 코드에서

같은 곳
Record *ptr ; 

및 포인터를 정의하면 다음과 같은 텍스트가 있습니다.

는 그리고 당신은 당신의 루프

편집이에 기능을 삽입 할 NULL 포인터 p를 전달하고, 그들은 지금 오류를 수정해야합니다. 모든

다음 코드의

+0

환호, 그 오류를 줄일 수 있습니다. – user1851359

+0

나는 @ user1851359 – cipher

관련 문제