首页 文章

在循环中插入链表节点

提问于
浏览
0

我正在使用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 回答

  • 1

    像这样更改代码

    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);
         .........
         .........
    
         }
    
  • 1

    这是我实验过的代码 .

    # 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 );
        }
    

相关问题