在 c 中打印链表值时的奇怪值

strange value when print linked list values in c

我使用链接列表在 C 中创建了一个应用程序,该应用程序逐行从标准输入中获取数据并将每个单词输入链接列表,最后打印所有这些单词而不重复,所以我制作了这段代码

//linked list
typedef struct NODE Node;
struct NODE{
  char *item;
  Node *next;
};
 //insert function
bool insert(Node** head_ref, char *new_string)
{
    /* allocate node */
struct NODE* new_node = (struct NODE*) malloc(sizeof(struct NODE));

/* put in the data  */
new_node->item = new_string;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
return true;
}
// tells us whether or not the given string is in the list
bool search(struct NODE *head, char *target)
{
    struct NODE *current = head;
    while (current != NULL)
    {
        if (current->item == target)
            return true;
        current = current->next;
    }
    return false;
}
// declare of the linked list 
Node *LinkedList = NULL;
//function used to read the stander input from the user
void loadFile()
{
#define LINE_SIZE 256
  char input[LINE_SIZE];
  char *token = NULL;

  while ( fgets( input, LINE_SIZE, stdin ) )
  {
    // parse the data into separate elements
    token = strtok( input, " \t\n" );
    while ( token )
    {
        if (!search(LinkedList, token)) {
            insert(&LinkedList, token);
            //puts("insert");
        }
        else {
            //printf("Not insert\n");
        }


      token = strtok( NULL, " \t\n" );
    }
  }
}

此函数打印列表中的所有单词

void Print(Node* head)
{
    Node *current = head;
    while (current != NULL)
    {
        printf("%s\n", current->item);
        current = current->next;
    }
}

当我在最后打印单词时,它给我一些奇怪的字符,这是我的主要内容

int main()
{
  loadFile();
  Print(LinkedList);

  return 0;
}

我在 windows

上使用 cntrl + Z 停止输入

我认为这是因为您实际上并没有为该项目分配 space,您只是在重复使用同一个缓冲区。 insert(&LinkedList, strdup(token));

你也在比较指针而不是字符串

if (current->item == target)

if (strcmp(current->item, target)==0)

尽管存在当前错误,它实际上可能会起作用!

建议代码如下:

  1. 干净地编译
  2. 更正问题评论中突出显示的所有问题
  3. 正确检查错误
  4. 记录包含每个头文件的原因
  5. 一致地缩进代码,每个缩进级别为 4 个空格
  6. 包含适当的水平间距:括号内、逗号后、分号后、C 运算符周围。
  7. 通过单个空行分隔代码块(for、if、else、whle、do...while、switch、case、default)
  8. 执行所需的功能

现在建议的代码:

//linked list
#include <stdio.h>    // fgets(), printf()
#include <stdlib.h>   // malloc(), exit(), EXIT_FAILURE
#include <stdbool.h>  // bool, true, false
#include <string.h>   // strtok(), strdup()

#define LINE_SIZE 256


struct NODE
{
  char *item;
  struct NODE *next;
};
typedef struct NODE Node;


// declare of the linked list
Node *LinkedList = NULL;


// prototypes
void insert( Node **head_ref, char *new_string );
bool search( Node *head, char *target );
void Print ( Node *head );
void loadFile( void );


 //insert function
void insert( Node **head_ref, char *new_string )
{
    /* allocate node */
    struct NODE* new_node =  malloc( sizeof(struct NODE) );
    if( !new_node )
    {
        perror( "malloc failed" );
        // TODO: cleanup
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    /* put in the data  */
    new_node->item = strdup( new_string );
    if( !new_node->item )
    {
        perror( "strdup failed" );
        // TODO: cleanup
        exit( EXIT_FAILURE );
    }

    // implied else, strdup successful

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref) = new_node;
}


// tells us whether or not the given string is in the list
bool search( Node *head, char *target )
{
    struct NODE *current = head;

    while ( current != NULL )
    {
        if ( strcmp( current->item, target) == 0 )
            return true;

        current = current->next;
    }

    return false;
}


//function used to read 'stdin' from the user
void loadFile()
{
    char input[ LINE_SIZE ];
    char *token = NULL;

    while ( fgets( input, LINE_SIZE, stdin ) )
    {
        // parse the data into separate elements
        token = strtok( input, " \t\n" );

        while ( token )
        {
            if ( !search( LinkedList, token ) )
            {
                insert( &LinkedList, token );
                //puts("insert\n");
            }

            else
            {
                //printf("Not insert\n");
            }

            token = strtok( NULL, " \t\n" );
        }
    }
}


//this function to print all the words in the list
void Print( Node* head )
{
    Node *current = head;

    while ( current != NULL )
    {
        printf( "%s\n", current->item );
        current = current->next;
    }
}


//when i print the words at the end it give me strange characters this my main
int main( void )
{
    loadFile();
    Print( LinkedList );

    return 0;
}

一个简单的 运行 程序结果如下:

1
3
5
5
4
2
0   (at this point, used <ctrl-d> (linux) to end the input
0
2
4
5
3
1

第二个简单的运行程序

first second second first third forth
forth
third
second
first