在循环中插入链表节点

时间:2015-03-28 02:33:35

标签: c linked-list

我使用strtok()来解析输入,将字符串转换为int,然后在while循环中将此int值插入到链接列表中。

这就是我尝试做的事情(我还没有明确地编写代码,但我打算做以下事情):

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 个答案:

答案 0 :(得分: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 :(得分: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 );
    }