将数据文件作为链表中的节点读取的函数
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
但是,当我 运行 要显示我当前的代码时,我没有得到这些数字并且最终得到随机数字,我该如何解决这个问题?你现在可以避免使用其他函数,因为我只是想确保文件读入不同的节点。
#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;
}
你的问题不是阅读,而是打印。
printf("Student %s has ID %d\n", d->name, &d->id);
当 reading 与 scanf
(和家人)时,您使用地址运算符 &
,但是当您打印时,它会打印变量的地址。
除了我在评论中提到的问题外,您在读取文件时还有一些其他问题,那就是您的 none-linking:
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
这将在列表末尾创建一个 extra 节点,这将是未初始化的,这意味着当您使用数据时它将导致 undefined behavior。
实际上,由于我评论中提到的问题,您将有 两个 个额外节点。
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
但是,当我 运行 要显示我当前的代码时,我没有得到这些数字并且最终得到随机数字,我该如何解决这个问题?你现在可以避免使用其他函数,因为我只是想确保文件读入不同的节点。
#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;
}
你的问题不是阅读,而是打印。
printf("Student %s has ID %d\n", d->name, &d->id);
当 reading 与 scanf
(和家人)时,您使用地址运算符 &
,但是当您打印时,它会打印变量的地址。
除了我在评论中提到的问题外,您在读取文件时还有一些其他问题,那就是您的 none-linking:
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
这将在列表末尾创建一个 extra 节点,这将是未初始化的,这意味着当您使用数据时它将导致 undefined behavior。
实际上,由于我评论中提到的问题,您将有 两个 个额外节点。