简体   繁体   English

读取数据文件作为链表中的节点的功能

[英]Function to read the data file as nodes in a linked list

My text file reads as this: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 Andrew Jackson, 7654321 Martin Van Buren, 6543210

However, when I run my current code to display I don't get those numbers and end up with random ones, how can I fix this? 但是,当我运行当前代码以显示时,我没有得到这些数字并以随机数结尾,该如何解决呢? You can avoid the other functions for now as I am just trying to make sure the file reads into different nodes. 您现在可以避免使用其他功能,因为我只是在尝试确保文件读入不同的节点。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Creates node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
}*head;

//Create Function Prototypes
void readDataFile ();
void display(struct node *d);
void deleteID(int num);
void deleteName(char dname);


//Main function
int main()
{
//Declare variables
int i, num, intid;
char *name;
char nameDelete [50];
char nameInsert [50];
struct node *n;

//initialize link list
head = NULL;

//Read in file
readDataFile();

//Create list of operations utilized in program
while (1)
{
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Delete by ID\n");
    printf("4.Delete by Name\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");

    if(scanf("%d", &i) <= 0)
    {
        printf("Enter only an Integer\n");
        exit(0);
    }
    else
    {
        switch(i)
        {
            case 1:
                printf("Enter the name to insert: ");
                gets(nameInsert);
                printf("Enter the ID associated with the name: ");
                scanf("%d", &intid);
                break;
            case 2:
                if (head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Elements in the list are:\n");
                }
                display(n);
                break;
            case 3:
                if(head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Enter the ID number to delete: ");
                    scanf("%d", &intid);
                }
                break;
            case 4:
                if(head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Enter name to delete: ");
                    gets(nameDelete);
                }
            case 5:
                return 0;
            default:
                printf("Invalid option\n");
        }
    }
}
return 0;
}

The problem you have is not the reading but the printing . 您遇到的问题不是阅读而是打印

printf("Student %s has ID %d\n", d->name, &d->id);

You use the address-of operator & when reading with scanf (and family), but when you're printing it will print the address of the variable. 在使用scanf (和族)进行读取时 ,可以使用地址运算符& ,但在打印时将打印变量的地址


You also have some other problem with the reading of the file, besides what I mentioned in my comment, and that is your none-linking: 除了我在评论中提到的内容外,文件读取还存在其他一些问题,那就是您没有链接:

temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;

This will create an extra node at the end of the list, which will be uninitialized meaning that when you use the data it will lead to undefined behavior . 这将在列表的末尾创建一个额外的节点,这将是未初始化的,这意味着当您使用数据时,它将导致未定义的行为

Actually, with the problem mentioned in my comment you will have two extra nodes. 实际上,鉴于我的评论中提到的问题,您将有两个额外的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM