将名称和编号插入链表的末尾
Inserting a name and number to the end of linked list
我的文本文件是这样的:
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
有人可以提供有关我如何使用插入函数将姓名和 ID 号添加到链表末尾的见解吗?当我 运行 代码 select 选项 1 时,它会跳过添加名称,只要求输入整数。之后什么也没有发生。
#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 insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char dname);
//Main function
int main()
{
//Declare variables
int i, num, id;
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:\n");
scanf("%[^\n]s", nameInsert);
getchar();
printf("Enter the ID associated with the name: ");
scanf("%d", &id);
insert(nameInsert, id);
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", &id);
}
if(deleteID(id))
printf("%d deleted successfully \n", id);
else
printf("%d not found in the list\n", id);
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;
}
//Define the functions
//Function to read in the data file
void readDataFile()
{
//Initialize the link the list
struct node *temp;
temp = malloc(sizeof(struct node));
temp->next = NULL;
head = temp;
//Open up the file
FILE *fp;
fp = fopen("AssignmentOneInput.txt", "r");
//Use memset function to copy the characters in string
char string[100];
memset(string, 0, 100);
//Create while loop scan in the contents of the file
while(fgets(string, 100, fp) != NULL)
{
sscanf(string, "%20[^,], %d", temp->name, &temp->id);
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
}
fclose(fp);
}
//Function to insert a name and ID number
void insert(char *inName, char *inID)
{
//Create temporary and helper nodes
struct node *temp, *helper;
//Allocate the memory for the temporary node
temp = (struct node *)malloc(sizeof(struct node));
//Convert character into integer value
int intID = atoi(inID);
//Set the data in the node
strcpy(temp->name, inName);
temp->id = intID;
temp->next = NULL;
//Create new node and add to it
helper = (struct node *)head;
while(helper->next != NULL)
{
helper = helper->next;
}
helper->next = temp;
helper = temp;
helper->next = NULL;
}
在选择选项中输入 1 后,标准输入同时具有 1 和换行符,您希望在下一个 scanf 语句中立即出现。
将scanf("%[^\n]s", nameInsert);
改为scanf("%s", nameInsert);
或在获得选择选项后立即使用 getchar()
。
我的文本文件是这样的:
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
有人可以提供有关我如何使用插入函数将姓名和 ID 号添加到链表末尾的见解吗?当我 运行 代码 select 选项 1 时,它会跳过添加名称,只要求输入整数。之后什么也没有发生。
#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 insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char dname);
//Main function
int main()
{
//Declare variables
int i, num, id;
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:\n");
scanf("%[^\n]s", nameInsert);
getchar();
printf("Enter the ID associated with the name: ");
scanf("%d", &id);
insert(nameInsert, id);
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", &id);
}
if(deleteID(id))
printf("%d deleted successfully \n", id);
else
printf("%d not found in the list\n", id);
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;
}
//Define the functions
//Function to read in the data file
void readDataFile()
{
//Initialize the link the list
struct node *temp;
temp = malloc(sizeof(struct node));
temp->next = NULL;
head = temp;
//Open up the file
FILE *fp;
fp = fopen("AssignmentOneInput.txt", "r");
//Use memset function to copy the characters in string
char string[100];
memset(string, 0, 100);
//Create while loop scan in the contents of the file
while(fgets(string, 100, fp) != NULL)
{
sscanf(string, "%20[^,], %d", temp->name, &temp->id);
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
}
fclose(fp);
}
//Function to insert a name and ID number
void insert(char *inName, char *inID)
{
//Create temporary and helper nodes
struct node *temp, *helper;
//Allocate the memory for the temporary node
temp = (struct node *)malloc(sizeof(struct node));
//Convert character into integer value
int intID = atoi(inID);
//Set the data in the node
strcpy(temp->name, inName);
temp->id = intID;
temp->next = NULL;
//Create new node and add to it
helper = (struct node *)head;
while(helper->next != NULL)
{
helper = helper->next;
}
helper->next = temp;
helper = temp;
helper->next = NULL;
}
在选择选项中输入 1 后,标准输入同时具有 1 和换行符,您希望在下一个 scanf 语句中立即出现。
将scanf("%[^\n]s", nameInsert);
改为scanf("%s", nameInsert);
或在获得选择选项后立即使用 getchar()
。