2012-10-19 7 views
0

다음과 같은 프로그램 http://pastie.org/5081517은 연락처를 알파벳 순으로 정렬하고 사용자가 특정 연락처를 검색 할 수 있도록 링크 된 목록 (의제)입니다.포인터와 함수가있는 C 목록

다음과 같은 기능을 가진 프로그램을 만들려고했습니다. (아래 게시 됨) http://pastie.org/5081533하지만 지금까지 성공하지 못했습니다. 나는 포인터를 처음 사용하고 있으며 내가 뭘 잘못하고 있는지 전혀 모른다. 어떤 도움이라도 대단히 감사합니다. 함수 order()

#define MAX 20 

typedef struct temp 
{ 
int data; 

char name[MAX]; 
char telephone[10]; 
char email[MAX]; 
char address[MAX]; 
char zipcode[10]; 

temp *next; 
} node; 


node* creation1 () 
{  
    node *start= NULL; 
    node *NEW = NULL; 

    node *current = NULL, *aux = NULL, *save = NULL; 

    NEW = (node*)malloc(sizeof(node)); 
    current = start= aux = save = NEW; 

return NEW; 
} 

node* creation2() 
{  
node *start= NULL; 
node *NEW = creation1(); 
start= NEW; 

    return start; 
} 

node* creation3 () 
{  

node *NEW = creation1(); 
node *current = NULL; 
current=NEW; 

    return current; 
    } 

    node* consult() 
    { 

    node *NEW= creation1(); 

    node *start= creation2(); 

    node *current = creation3(); 




int exit; 
printf("How many contacts do you want to add in the agenda? "); 
scanf("%i",&exit); 


for(int i=1; i<=exit; i++) 
{ 

    NEW = (node*)malloc(sizeof(node)); 
    current->next=NEW;     
    current = NEW; 
    fflush(stdin); 
    puts("NAME: "); 
    gets(NEW->name); 
    puts("TELEPHONE: "); 
    gets(NEW->telephone); 
    puts("EMAIL: "); 
    gets(NEW->email); 
    puts("ADDRESS: "); 
    gets(NEW->address); 
    puts("ZIP CODE: "); 
    gets(NEW->zipcode); 
    NEW->next=NULL; 


} 

    current = start->next; 

    return current; 

    } 


node* order() 
{  

    node *NEW=creation1(); 
    node *start=creation2(); 
    node *current =NULL; 
    current=consult(); 
    node *aux = NULL; 
    node *save = NULL; 
    aux=NEW; 
    save=NEW; 



int i = 0; 
do 
{ 
    i++; 
    current = current->next; /* THIS IS WHERE I'M GETTING AN ERROR MS Visual Studio tells me: "Unhandled exception...Access violation reading location..." */ 
} 
while (current != NULL); 

current = start->next; 
aux = current->next; 

for (int j = 1; j < i; j++) 
{ 

    current = start->next; 
    aux = current->next; 

    while(current->next != NULL) 
    { 
     if (strcmp(current->name,aux->name) > 0) 
     { 
      strcpy(save->name, current->name); 
      strcpy(save->telephone, current->telephone); 
      strcpy(save->email, current->email); 
      strcpy(save->address, current->address); 
      strcpy(save->zipcode, current->zipcode); 

      strcpy(current->name, aux->name); 
      strcpy(current->telephone, aux->telephone); 
      strcpy(current->email, aux->email); 
      strcpy(current->address, aux->address); 
      strcpy(current->zipcode, aux->zipcode); 


      strcpy(aux->name, save->name); 
      strcpy(aux->telephone, save->telephone); 
      strcpy(aux->email, save->email);  
      strcpy(aux->address, save->address); 
      strcpy(aux->zipcode, save->zipcode); 
     } 
     current = current->next; 
     aux = current->next; 
    } 
} 

    return current; 

} 


    node* displayorder() 
    { 
     node *NEW=creation1(); 
     node *start=creation2(); 
     node *current = order(); 

     current = start->next; 


    while(current != NULL) 
    { 

    printf("\n********************"); 
    printf("\n NAME: %s",current->name); 
    printf("\n TELEPHONE: %s", current->telephone); 
    printf("\n E-MAIL: %s", current->email); 
    printf("\n ADDRESS: %s ", current->address); 
    printf("\n ZIP CODE: %s ", current->zipcode); 
    current = current->next; 
    } 
    getch(); 


    return current; 


} 



node* displaysearch() 
{  

node *current = displayorder(); 
node *start= creation2(); 

char search[MAX]; 

printf("\n\nGive a name to search: "); 
scanf("%s",search); 




current = start->next; 
while(current != NULL) 
{ 
    if(strcmp(search, current->name)==0) 
    { 
    printf("\n********************"); 
    printf("\n NAME: %s",current->name); 
    printf("\n TELEPHONE: %s", current->telephone); 
    printf("\n E-MAIL: %s", current->email); 
    printf("\n ADDRESS: %s ", current->address); 
    printf("\n ZIP CODE: %s ", current->zipcode); 

} 
    current = current->next; 
    } 

     return current; 


getch(); 

} 


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


    displaysearch(); 


} 
+4

귀하의 게시물에 직접 코드를 추가하십시오. 귀하의 문제를 보여주는 최소한의 예가 나와 있습니다. 코드 블록을 만들려면 줄의 시작 부분에 4 개의 공백을 추가하십시오. –

+0

많은 코드가 있습니다. 뭐가 문제 야? 너 혼란스러워? – nneonneo

+0

잘 함수를 선언하고 또한 포인터를 반환하는지 잘 모르겠습니다. 프로그램이 order() 함수의 첫 번째 루프에 도착하자 마자 오류가 발생합니다. – user1758027

답변

1

로컬 변수 currentconsult() 함수로부터 값을 얻는다. 그 함수는 다시 구조를 가리 키도록 초기화되지 않은 start->next 값을 반환합니다. 따라서 메모리 액세스 위반 예외가 예상됩니다.

+0

Serge의 대답은 좋은 출발이지만 게시 한 코드를 최소화하는 것이 좋습니다 (다른 사람들과 마찬가지로). 그러면 필요한 도움을 얻을 수 있습니다. 코드 섹션을 해당 섹션의 내용을 설명하는 주석으로 대체하십시오. –

관련 문제