我需要将输入文本文件读入链接列表,如下所示:
Greenbelt 1 Green 0
College_Park 1 Green 125
Prince_George_Plaza 1 Green 300
West_Hyatsville 1 Green 450
Fort_Totten 3绿色700黄色0红色241
Georgia_Ave_Petworth 2绿色820黄色100
Columbia_Heights 2 Green 1000 Yellow 280
U_St 2绿色1180黄色460
Shaw_Howard_U 2绿色1300黄色580
Mt_Vernon_Sq 2绿色1420黄色700
Gallery_Place 3绿色1540黄色820红色1612
档案2绿色1630黄色910
L' Enfant_Plaza 5绿色1800黄色1080蓝色1300银色1320橙色1400
Navy_Yard_Ballpark 1 Green 2200
但我遇到的问题是,当有多条彩色地铁线路时,我无法弄清楚要做什么。我需要输入它,以便站名后的所有内容仍然连接到该站。我的结构如下:
#define NEW(x) (x*)malloc(sizeof(x))
typedef struct{
char * name;
char * color;
int number;
int time;
} STATION;
typedef STATION DATA;
typedef struct node
{ void * data;
struct node * next;
} NODE;
typedef struct
{ long num;
NODE * head;
NODE * tail;
}ROOT;
我成功使用的代码只输入每行中的前四个单词/数字。
int main(){
char c, s1[100], *num, *time;
DATA *data;
ROOT *root;
NODE *temp;
FILE *input, *lines, *report;
input = fopen(argv[1], "r");
lines = fopen(argv[2], "w");
report = fopen(argv[3], "w");
root = make_root();
while (fscanf(input, "%c", &c) != EOF) {
data = NEW(DATA);
if (data == NULL) {
return -1;
}
data->name = (char *) malloc(sizeof(char));
if (c == '\n') {
fscanf(input, "%c", &c);
}
i = 0;
do {
data->name[i] = c;
i++;
data->name = (char *) realloc(data->name, (i + 1) * sizeof(char));
fscanf(input, "%c", &c);
} while (c != ' ');
data->name[i] = '\0';
i = 0;
num = (char *) malloc(sizeof(char));
do {
fscanf(input, "%c", &c);
num[i] = c;
i++;
num = (char *) realloc(num, (i + 1) * sizeof(char));
} while (c >= '0' && c <= '9');
data->number = atoi(num);
i = 0;
data->color = (char *) malloc(sizeof(char));
do {
fscanf(input, "%c", &c);
data->color[i] = c;
i++;
data->color = (char *) realloc(data->color, (i + 1) * sizeof(char));
} while (c != ' ');
data->color[i - 1] = '\0';
i = 0;
time = (char *) malloc(sizeof(char));
do {
fscanf(input, "%c", &c);
time[i] = c;
i++;
time = (char *) realloc(time, (i + 1) * sizeof(char));
} while (c >= '0' && c <= '9');
data->time = atoi(time);
insert_at_position(root, data);
}
return 0;
}
ROOT * make_root (void)
{ ROOT * temp;
temp = NEW(ROOT);
if (temp != NULL)
{ temp->num = 0;
temp->head = NULL;
temp->tail = NULL;
}
return temp;
}
int insert_at_position(ROOT *r, DATA *d)
{ NODE * temp, *this;
temp = make_node(d);
if (temp == NULL){
return -1;
}
if (r == NULL) {
r = make_root();
if (r == NULL) {
return -1;
}
}
if (r->head == NULL)
{ insert_at_head(r, temp->data);
return 0;
}
this = r->head;
if (((DATA *)(this->data))->time >= ((DATA *)(temp->data))->time)
insert_at_head(r, temp->data);
else
{ while (this->next != NULL &&
((DATA *)(this->next->data))->time > ((DATA *)(temp->data))->time)
this = this->next;
if (this->next == NULL) // update tail if insert at tail
{ r->tail = temp;
this->next = temp;
(r->num)++;
}
else
{ temp->next = this->next;
this->next = temp;
(r->num)++;
}
}
return 0;
}
我的问题是如何更改我的代码/结构,以便我可以将所有站信息放入单个节点,以便我可以使用该信息在我的代码中稍后显示城域信息?我应该将char * color2,int time2,char * color3,int time3等添加到我的结构中以适应具有额外行的站点吗?
我读到我可以读取电台名称和该电台上的线路数,然后为此创建一个额外的STATION结构,但我找不到有关如何操作的信息。任何信息都会有帮助!谢谢!