我正在使用strtok()来解析输入,将字符串转换为int,然后将此int值插入到while循环中的链接列表中 .
这是我正在尝试做的事情(我没有明确地编写代码,但我打算做一些如下操作):
while(fgets(&string,LMAX,fp) != NULL){
//get first token using strtok
//convert to int
//insert into linked list
while (token != NULL){
//get next token in the line
//do the same as above
}
}
我已经写了一个函数,它应该将一个节点插入到链表中,它如下:
void insert_node(struct Cons *head_pointer, int data){
struct Cons *new = (struct Cons*) malloc(sizeof(struct Cons));
struct Cons *current = head_pointer;
new->head = data;
new->tail = NULL;
if (head_pointer->tail == NULL){
head_pointer->tail = new;
}
else
{
while (current->tail != NULL){
current = current->tail;
}
current->tail = new;
}
free(current);
current = NULL;
}
结构定义如下:
typedef int element_t;
typedef
struct Cons {
element_t head;
struct Cons* tail;
} Cons;
谁能建议我怎么做呢?
2 回答
像这样更改代码
在main()函数调用中;
这是我实验过的代码 .