C链表头指针不断变化

ivqmmu1c  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(119)

我做了一个程序,从文本文件中读取输入并创建一个链表,但做起来有困难。我还是一个初学者。问题是添加新项时头指针一直在移动。

#include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include <stdbool.h>
//Visitor information
    struct visitData
    {
    char *SrNo;
    char *ticketNo;
    char *Name;
    char *ID;
    };

    //Ticket linked list
    struct ticket
    {
    struct visitData visitor;
    struct ticket *nextPtr;
    };

    int main() { //main

    FILE *fPtr = NULL;
    fPtr = fopen("Tickets.txt", "a+");
    if (fPtr == NULL)
    {
        printf("Error, please try again.\n");
        return 0;
    }

    FILE *outfPtr = NULL;
    outfPtr = fopen("output.txt", "w");
    if (outfPtr == NULL)
    {
        puts("Error, could not create output file, please try again.");
        return 0;
    }

    else {

        
        struct ticket *head = NULL;

        struct ticket *new = NULL;

        struct visitData *nextPtr = NULL;

        char sr[2];
        char ticketNumber[20];
        char name[30];
        char id[24];

        

        
            rewind(fPtr);

            if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
            {

                new = malloc(sizeof(struct ticket));
                
                new->visitor.SrNo = strdup(sr);
                new->visitor.ticketNo = strdup(ticketNumber);
                new->visitor.Name = strdup(name);
                new->visitor.ID = strdup(id);
                new->nextPtr = NULL;
                head = new;
                
                printf("%s\n", head->visitor.SrNo);

                while (!feof(fPtr))
                {

                                    
                    if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)

                        new->visitor.SrNo = sr;                       
                        new->visitor.ticketNo = ticketNumber;
                        new->visitor.Name = name;
                        new->visitor.ID = id;
                        
                        new->nextPtr = malloc(sizeof(struct ticket));
                        new = new->nextPtr;
                        new->nextPtr = NULL;
                    }
                    

                }
                
                
            }
            else
            {
                printf("%d",fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id));
                new->visitor.SrNo = "0";
            }

            if (head == NULL)
            {
                printf("List is empty\n");
            }
            struct ticket *Ptr = NULL;
            Ptr = head;
            printf("%s\n", Ptr->visitor.SrNo);
            while (Ptr != NULL)
            {
                printf("%s %s\n", Ptr->visitor.SrNo, Ptr->visitor.ticketNo);
                Ptr = Ptr->nextPtr;
            }
    }
    }
I did try to add a line which is `
new = malloc(sizeof(struct ticket));
`, but it became another problem, that the head pointer cannot link to the second node.
#include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include <stdbool.h>
//Visitor information
    struct visitData
    {
    char *SrNo;
    char *ticketNo;
    char *Name;
    char *ID;
    };

    //Ticket linked list
    struct ticket
    {
        struct visitData visitor;
        struct ticket *nextPtr;
    };

    int main() { //main

    FILE *fPtr = NULL;
    fPtr = fopen("Tickets.txt", "a+");
    if (fPtr == NULL)
    {
        printf("Error, please try again.\n");
        return 0;
    }

    FILE *outfPtr = NULL;
    outfPtr = fopen("output.txt", "w");
    if (outfPtr == NULL)
    {
        puts("Error, could not create output file, please try again.");
        return 0;
    }

    else {

        
        struct ticket *head = NULL;

        struct ticket *new = NULL;

        struct visitData *nextPtr = NULL;

        char sr[2];
        char ticketNumber[20];
        char name[30];
        char id[24];

        

        
            rewind(fPtr);

            if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
            {

                new = malloc(sizeof(struct ticket));
                
                new->visitor.SrNo = strdup(sr);
                new->visitor.ticketNo = strdup(ticketNumber);
                new->visitor.Name = strdup(name);
                new->visitor.ID = strdup(id);
                new->nextPtr = NULL;
                head = new;
                
                printf("%s\n", head->visitor.SrNo);

                while (!feof(fPtr))
                {

                                    
                    if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
                    {
                        new = malloc(sizeof(struct ticket));
                        new->visitor.SrNo = sr;                       
                        new->visitor.ticketNo = ticketNumber;
                        new->visitor.Name = name;
                        new->visitor.ID = id;
                        
                        new->nextPtr = malloc(sizeof(struct ticket));
                        new = new->nextPtr;
                        new->nextPtr = NULL;
                    }
                    

                }
                
                
            }
            else
            {
                printf("%d",fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id));
                new->visitor.SrNo = "0";
            }

            if (head == NULL)
            {
                printf("List is empty\n");
            }
            struct ticket *Ptr = NULL;
            Ptr = head;
            printf("%s\n", Ptr->visitor.SrNo);
            while (Ptr != NULL)
            {
                printf("%s %s\n", Ptr->visitor.SrNo, Ptr->visitor.ticketNo);
                Ptr = Ptr->nextPtr;
            }
    }
    }
bsxbgnwa

bsxbgnwa1#

向列表添加节点的典型方法是:

new = malloc(size);    // Create a new node
new->nextPtr = head;   // Save the existing list
head = new;            // Add the new element at the front of the list

您在保存旧值之前更改了头,因此丢失了列表。

相关问题