因此,我正在使用插入排序对单词的数据文件按字母顺序进行排序。 示例数据文件: 瑞安 约翰 凯西 .... 等等
当我到达第83行时。我得到的空白行应该是我的正确数据。 最初用于整数,但决定将其切换为字符串以练习即将来临的决赛。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Link list node */
struct Node
{
char data[10];
struct Node* next;
};
void sortedInsert(struct Node** head_ref, struct Node* new_node)
{
struct Node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* A utility function to create a new node */
struct Node *newNode(char new_data[])
{
/* allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data == new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%s\n", temp->data);
temp = temp->next;
}
}
/* Drier program to test count function*/
int main()
{
char word[10];
/* Start with the empty list */
struct Node* head = NULL;
FILE *data;
if((data = fopen("./hw11.data","r"))==NULL){
printf("error-hw11.data could not be opened.\n");
return 0;
}
//insert words into linked list
while(1){
if(feof(data)) break;
fgets(word,10,data);
printf("%s\n",word);
struct Node *new_node = newNode(word);
sortedInsert(&head,new_node);
}
printf("\n Created Linked List\n");
printList(head);
return 0;
}
答案 0 :(得分:3)
不是分配=,而是在newNOde()中使用==运算符
struct Node *newNode(char new_data[])
{
/* allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data == new_data; // **it should = (single) NOT == to assign**
new_node->next = NULL;
return new_node;
}