링크 된 목록의 끝에 노드를 추가하려고합니다. void 함수를 사용하여 내 구조체를 전달하지만 한 번 추가 함수를 통해 실행 된 내 구조체 여전히 비어 있습니다. 여기 코드가 있습니다. 그것은 처음에 함수에 전달 될 때링크 된 목록의 끝에 노드 추가
struct part {
char* name;
float price;
int quantity;
struct part *next;
};
typedef struct part partType;
void addEnd(partType *item)
{
partType *temp1=NULL, *temp2=NULL;
char temp[100];
temp1=(struct part *)malloc(sizeof(partType));
if (!temp1)
printf("malloc failed\n");
temp1->name = malloc(sizeof(char)*100);
printf("Please enter item name: \n");
fgets(temp, 100, stdin);
strcpy(temp1->name, temp);
printf("Please enter item price: \n");
fgets(temp, 100, stdin);
sscanf(temp, "%f", &temp1->price);
printf("Please enter item quantity: \n");
fgets(temp, 100, stdin);
sscanf(temp, "%d", &temp1->quantity);
// Copying the Head location into another node.
temp2=item;
if(item == NULL)
{
// If List is empty we create First Node.
item=temp1;
item->next=NULL;
printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);
}
else
{
// Traverse down to end of the list.
while(temp2->next != NULL)
temp2=temp2->next;
// Append at the end of the list.
temp1->next=NULL;
temp2->next=temp1;
printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);
}
}
항목은 널 (null)이지만, 어떤 이유가 null 나오는 동안 나는 TEMP1 동일한 항목을 설정하는 경우 문이 비록.
로이 부를 것이다 디버거? 이는 변수가 어떻게 변하는 지, 프로그램이 제어 구조 (명령문 및 루프 인 경우)를 통해 흐르는 방식을 확인하는 좋은 방법입니다. IDE의 내장 된 시각적 디버깅 도구를 사용하거나 명령 줄을 통해 GDB를 사용하십시오. – Cam