次のプログラムhttp://pastie.org/5081517を作成しました。これは、連絡先をアルファベット順に並べ、ユーザーが特定の連絡先を検索できるようにするリンク リスト (議題) です。
次に、関数を使用して同じプログラムを作成しようとしました: (以下に投稿) http://pastie.org/5081533しかし、これまでのところ成功していません。私はポインターを初めて使用し、何が間違っているのかわかりません。どんな助けでも大歓迎です。
#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();
}