列表实现中的不明错误
Unclear error in list implementation
我写了一个函数来从文本文件中获取输入,"fileInput" 函数,
我的文本文件的内容就像
1 2 4 5
2 4 5 6
主要函数部分如下:case 7 head=fileInput(head);break;
但出于某种原因,当我选择案例 7 使用 "fileInput",并通过我的 list_all(显示全部)进行检查时,我除了 "empty list" 什么都没有
这是我的部分代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int id;
char name[100];
char address[100];
char group[100];
struct node * next;
} data;
data * fileInput(data * head) {
char name[100];
char group[100];
char address[100];
int id;
FILE *fp;
fp = fopen("input.txt", "r");
if (!fp)
return head;
while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {
//head = push_sort(head, group, name, id, address);
printf("%99s", name);
}
fclose(fp);
return head;
}
void list_all(data * head) {
data * current;
current = head;
while (current != NULL) {
printf(" \n group:%s\n id:%d\n name:%s\n address:%s\n", current->group,
current->id, current->name, current->address);
current = current->next;
}
if (head == NULL) {
printf("empty list\n");
}
}
int main() {
data * test_list = malloc(sizeof(data));
strcpy(test_list->name,"a");
strcpy(test_list->group, "2");
strcpy(test_list->address, "3");
test_list->id = 4;
test_list->next = NULL;
list_all(fileInput(test_list));
}
并且,输出只包含一个 2 3 4
您的代码确实如下所示,在此处添加了 head =
:
head = push_sort(head, group, name, id, address);
你说你现在已经添加了,所以问题肯定出在你没有显示的列表打印功能上。我在下面的 main()
中包含了一个。我使用基于 name
字段插入头部、尾部和中间位置的示例测试了您的代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node {
int id;
char name[100];
char address[100];
char group[100];
struct node * next;
} data;
data * push_sort(data * head, char group[], char name[], int id,char address[]) {
data * current = head;
data * temp = NULL;
if (head == NULL) {
head = malloc(sizeof(data));
head->id = id;
strcpy(head->name, name);
strcpy(head->group, group);
strcpy(head->address, address);
head->next = NULL;
return head;
}
if (strcmp(head->name, name) > 0) { //add to the top
temp = head;
head = malloc(sizeof(data));
head->next = temp;
strcpy(head->name, name);
strcpy(head->group, group);
strcpy(head->address, address);
head->id = id;
return head;
}
while (current->next != NULL) {
if (0 < strcmp(current->next->name, name)) {
temp = current->next;
current->next = malloc(sizeof(data));
current->next->next = temp;
current->next->id = id;
strcpy(current->next->name, name);
strcpy(current->next->address, address);
strcpy(current->next->group, group);
return head;
}
current = current->next;
}
current->next = malloc(sizeof(data));
current->next->next = NULL;
current->next->id = id;
strcpy(current->next->name, name);
strcpy(current->next->address, address);
strcpy(current->next->group, group);
return head;
}
data * fileInput(data * head) {
char name[100];
char group[100];
char address[100];
int id;
FILE *fp;
fp = fopen("input.txt", "r");
if (!fp)return head;
while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {
//printf ("Push %s, %d, %s, %s\n", group, id, name, address);
head = push_sort(head, group, name, id, address); //added "head ="
}
fclose(fp);
return head;
}
int main(void)
{
data *temp, *head = NULL;
head = fileInput(head);
temp = head;
while (temp) {
printf ("%s, %d, %s, %s\n", temp->group, temp->id, temp->name, temp->address);
temp= temp->next;
}
return 0;
}
我写了一个函数来从文本文件中获取输入,"fileInput" 函数, 我的文本文件的内容就像
1 2 4 5
2 4 5 6
主要函数部分如下:case 7 head=fileInput(head);break;
但出于某种原因,当我选择案例 7 使用 "fileInput",并通过我的 list_all(显示全部)进行检查时,我除了 "empty list" 什么都没有
这是我的部分代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int id;
char name[100];
char address[100];
char group[100];
struct node * next;
} data;
data * fileInput(data * head) {
char name[100];
char group[100];
char address[100];
int id;
FILE *fp;
fp = fopen("input.txt", "r");
if (!fp)
return head;
while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {
//head = push_sort(head, group, name, id, address);
printf("%99s", name);
}
fclose(fp);
return head;
}
void list_all(data * head) {
data * current;
current = head;
while (current != NULL) {
printf(" \n group:%s\n id:%d\n name:%s\n address:%s\n", current->group,
current->id, current->name, current->address);
current = current->next;
}
if (head == NULL) {
printf("empty list\n");
}
}
int main() {
data * test_list = malloc(sizeof(data));
strcpy(test_list->name,"a");
strcpy(test_list->group, "2");
strcpy(test_list->address, "3");
test_list->id = 4;
test_list->next = NULL;
list_all(fileInput(test_list));
}
并且,输出只包含一个 2 3 4
您的代码确实如下所示,在此处添加了 head =
:
head = push_sort(head, group, name, id, address);
你说你现在已经添加了,所以问题肯定出在你没有显示的列表打印功能上。我在下面的 main()
中包含了一个。我使用基于 name
字段插入头部、尾部和中间位置的示例测试了您的代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node {
int id;
char name[100];
char address[100];
char group[100];
struct node * next;
} data;
data * push_sort(data * head, char group[], char name[], int id,char address[]) {
data * current = head;
data * temp = NULL;
if (head == NULL) {
head = malloc(sizeof(data));
head->id = id;
strcpy(head->name, name);
strcpy(head->group, group);
strcpy(head->address, address);
head->next = NULL;
return head;
}
if (strcmp(head->name, name) > 0) { //add to the top
temp = head;
head = malloc(sizeof(data));
head->next = temp;
strcpy(head->name, name);
strcpy(head->group, group);
strcpy(head->address, address);
head->id = id;
return head;
}
while (current->next != NULL) {
if (0 < strcmp(current->next->name, name)) {
temp = current->next;
current->next = malloc(sizeof(data));
current->next->next = temp;
current->next->id = id;
strcpy(current->next->name, name);
strcpy(current->next->address, address);
strcpy(current->next->group, group);
return head;
}
current = current->next;
}
current->next = malloc(sizeof(data));
current->next->next = NULL;
current->next->id = id;
strcpy(current->next->name, name);
strcpy(current->next->address, address);
strcpy(current->next->group, group);
return head;
}
data * fileInput(data * head) {
char name[100];
char group[100];
char address[100];
int id;
FILE *fp;
fp = fopen("input.txt", "r");
if (!fp)return head;
while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {
//printf ("Push %s, %d, %s, %s\n", group, id, name, address);
head = push_sort(head, group, name, id, address); //added "head ="
}
fclose(fp);
return head;
}
int main(void)
{
data *temp, *head = NULL;
head = fileInput(head);
temp = head;
while (temp) {
printf ("%s, %d, %s, %s\n", temp->group, temp->id, temp->name, temp->address);
temp= temp->next;
}
return 0;
}