C 在循环中插入链表节点

C 在循环中插入链表节点,c,linked-list,C,Linked List,我使用strtok()解析输入,将字符串转换为int,然后将这个int值插入链表中,所有这些都在while循环中 这就是我试图做的(我没有明确地编写代码,但我计划做如下事情): 我已经编写了一个函数,用于将节点插入链表,如下所示: while(fgets(&string,LMAX,fp) != NULL){ //get first token using strtok //convert to int //insert into linked list while (token !=

我使用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;
结构定义也如下所示:

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;

有人能建议我怎么做吗

像这样更改代码

   Cons *insert_node(Cons *head_pointer, int data){
        Cons *new = (struct Cons*) malloc(sizeof(struct Cons));
        Cons *current = head_pointer;
        new->head = data;
        new->tail = NULL;

         if (head_pointer== NULL){
             head_pointer->tail = new;
         }
      else
       {

            while (current->tail != NULL){
               current = current->tail;
       }
           current->tail = new;
    }
 //free(current);
  //current = NULL; 
    return head_poiner;
  }
在main()函数调用中,如下所示

    main()
     {
      ..........
      ..........
      while(fgets()!=NULL){

       head_pointer=insert_node(head_pointer,data);
     .........
     .........

     }

这是我试验过的代码

    # include <stdio.h>
     # include <stdlib.h>
    struct node
    {
    int data;
    struct node *link;
   };
   struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    /*  if the existing list is empty then insert a new node as the
     *  starting node */
    if(p==NULL)
    {
            if((p=(struct node *)malloc(sizeof(struct node)))==NULL)
            {
                    perror("Error");
                    exit(0);
            }
            p-> data = n;
            p-> link = p; /*  makes the pointer pointing to itself because it
                              is a circular list*/
    }
    else
    {
            temp = p;
            /*  traverses the existing list to get the pointer to the last node of
             *     it */
            while (temp-> link != p)
                    temp = temp-> link;
            if((temp-> link = (struct node *)malloc(sizeof(struct node)))==NULL)
            {
                    perror("Error\n");
                    exit(0);
            }
            temp = temp-> link;
            temp-> data = n;
            temp-> link = p;
    }
    return p;
   }
    void printlist ( struct node *p )
    {
    struct node *temp;
    temp = p;
    printf("The data values in the list are\n");
    if(p!= NULL)
    {
            do
            {
                    printf("%d\t",temp->data);
                    temp=temp->link;
            } while (temp!= p);
            printf("\n");
    }
             else
            printf("The list is empty\n");
      }
  void main()
    {
    int n;
    int x;
    struct node *start = NULL ;
    char buf[BUFSIZ];
    while(fgets(buf,BUFSIZ,stdin)!=NULL){
            x=atoi(buf);
            start = insert ( start, x );
    }
    printlist ( start );
    }
#包括
#包括
结构节点
{
int数据;
结构节点*链接;
};
结构节点*插入(结构节点*p,int n)
{
结构节点*temp;
/*如果现有列表为空,则插入一个新节点作为
*起始节点*/
if(p==NULL)
{
if((p=(结构节点*)malloc(sizeof(结构节点)))==NULL)
{
佩罗(“错误”);
出口(0);
}
p->data=n;
p->link=p;/*使指针指向自身,因为
这是一份循环清单*/
}
其他的
{
温度=p;
/*遍历现有列表以获取指向的最后一个节点的指针
*它*/
同时(临时->链接!=p)
温度=温度->链接;
if((temp->link=(结构节点*)malloc(sizeof(结构节点)))==NULL)
{
perror(“错误”);
出口(0);
}
温度=温度->链接;
温度->数据=n;
温度->链接=p;
}
返回p;
}
无效打印列表(结构节点*p)
{
结构节点*temp;
温度=p;
printf(“列表中的数据值为\n”);
如果(p!=NULL)
{
做
{
printf(“%d\t”,临时->数据);
温度=温度->链接;
}while(temp!=p);
printf(“\n”);
}
其他的
printf(“列表为空\n”);
}
void main()
{
int n;
int x;
结构节点*start=NULL;
char buf[BUFSIZ];
while(fgets(buf,BUFSIZ,stdin)!=NULL){
x=原子指数(buf);
开始=插入(开始,x);
}
打印列表(开始);
}

我觉得插入代码没问题。不过有一件事。如果使用的是
typedef
,则无需每次都编写
struct Cons
。您只需编写
Cons
。至于解析输入,因为您没有在代码中给出太多关于它的细节,我不能说太多。但是,我如何使用
insert\u node
返回的内容来实际插入节点呢?您能否更新代码以显示示例或示例implementation@cp101020304每次调用
insert_node()函数
,只需将
head_指针作为callby value传递
,添加节点时,不会影响
main()函数中的原始指针
,这就是为什么我们返回指针。我不太明白,你能举一个例子吗?@ CP101020304 -考虑你有一个节点,比如当调用“代码>插入子节点函数 <代码> 1”->>3>>NUL/<代码>时,你只需将代码< > 1的指针发送到插入节点函数这是按值调用,这将作为该函数的局部变量,然后添加值
4,如| 1 |->| 2 |->| 3 |->| 4 |->NULL
,它不会影响
main()函数的值
,因此我们将返回值。假设我有一个int,我想插入到链接列表中。在'head_pointer=insert_节点(head_pointer,data);`代码中的行?还是会自动将int添加到链接列表中?