다음은 제 코드입니다. 사용자가 val이 아닌 다른 필드를 입력하도록해야합니다. 이것은 목록을 테스트하는 데 사용하는 코드 일 뿐이므로 예를 들어 val, name 및 surname을 추가하길 원합니다. 내가 어떻게 해? 일부 용어는 영어가 아니지만 전반적인 코드가 명확하다고 생각합니다.목록에 필드를 추가하는 방법
#include <stdio.h>
#include <stdlib.h>
struct lista{
int val;
struct lista *next;
}Lista;
struct lista* crea(struct lista* head);
void stampa(struct lista* testa);
int main()
{
struct lista *head=NULL;
int insert=0;
while(1){
printf("\n *** MENU ***\n 1.Add in list\n 2.Print\n 3.Exit\n\n Input: ");
scanf("%d", &insert);
switch(insert){
case 1:
head = crea(head);
break;
case 2:
stampa(head);
break;
case 3:
exit(1);
default:
printf("\n Errore: scelta non valida!\n");
break;
}
}
return 0;
}
struct lista* crea(struct lista* head){
struct lista *nuovo=NULL; //sarà la nuova head
int valore=0;
nuovo = (struct lista*)malloc(sizeof(struct lista));
printf("\nValue: ");
scanf("%d", &valore);
nuovo->val=valore;
nuovo->next=head;
head = nuovo;
return nuovo;
};
void stampa(struct lista* head){
struct lista* temp=NULL;
temp = head;
while(temp != NULL){
printf("\nvalore: %d\n", temp->val);
temp = temp->next;
}
}
필드를 추가하고 사용자가 입력 할 수 있도록하십시오. – MikeCAT
나는 그것을했고, 추락했다. 어쩌면 정확한 방법이 아니었을 까?() – ennedes
추락 한 코드를 보여줍니다. 그러면 우리는 그 이유를 말할 수 있습니다. 또한 충돌이 무엇인지 말하고 디버거에서 실행하여 직접 수정하려고 시도했습니다. – pm100