我使用Linked List在C中创建了一个应用程序,应用程序逐行从stander输入数据并输入每个单词到链表,最后打印所有这些单词没有任何重复,所以我做了这个代码
//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停止输入
答案 0 :(得分:0)
我认为这是因为你实际上没有为项目分配空间,你只是重复使用相同的缓冲区。 insert(&LinkedList, strdup(token));
你也在比较指针而不是字符串
if (current->item == target)
if (strcmp(current->item, target)==0)
虽然目前的错误可能确实有效!
答案 1 :(得分:0)
以下提议的代码:
现在建议的代码:
//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