列表实现中的错误不明确

时间:2015-05-12 08:11:34

标签: c linked-list scanf

我写了一个函数来从文本文件中获取输入,“fileInput”函数, 我的文本文件的内容就像

1 2 4 5
2 4 5 6

主要功能的部分如:case 7 head=fileInput(head);break; 但由于某种原因,当我选择案例7使用“fileInput”,并通过我的list_all(显示所有)进行检查时,除了“空列表”之外什么也没有 这是我的代码的一部分:

#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

1 个答案:

答案 0 :(得分:0)

您的代码确实如下所示,此处添加了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;
}