こんにちは、私は 3 フィールドを取り、それを構造体に格納するこのコードを書いていますが、ノードを作成する方法がわからず、関数 print_planet_list() がリンクされたリストをウォークスルーして出力を出力する必要があります。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* the struct */
typedef struct{
char name[128];
double dist;
char description[1024];
} planet_t;
/* have to write a definition here */
typedef struct planet_node {
???; /* what to write here? */
} planet_node;
/* Print a list of planets pointed to by ptr */
void print_planet_list(planet_node *ptr){
while(ptr != NULL){
printf("%15s %10.2f %s\n",
new[i].name,
new[i].dist,
new[i].description);/* not sure if its correct */
}
printf("\n");
}
int main(){
char buf[64];
planet_node *head = NULL;
int quit = 0;
do {
printf("Enter planet name (q quits): ");
scanf("%s",buf);
if((strcmp(buf,"q")==0)){
quit = 1;
}
else{
planet_node *new = (planet_t *) malloc(sizeof(planet_t)*? );
strcpy(???);
printf("Enter distance and description: ");
scanf(" %lf ", new[i].dist);
gets(new[i].description);
ptr->head; /* Link new node to head */
???; /* Set the head to the new node */
}
} while(!quit);
printf("Final list of planets:\n");
print_planet_list(head);
while(head != NULL){
planet_node *remove = head;
head = (*head).next;
free(remove);
}
}
私の希望する入力は次のとおりです:
惑星の名前を入力してください (q 終了): 地球 距離と説明を入力してください: 0.9 友好的で親切 惑星の名前を入力してください (q 終了してください): 火星 距離と説明を入力してください: 1.05 非常に国際的です
出力:
火星 1.05 非常にコスモポリタンな地球 0.90 フレンドリーで親切