gpt4 book ai didi

c - 跳出这个 while 循环

转载 作者:太空宇宙 更新时间:2023-11-04 08:30:56 24 4
gpt4 key购买 nike

我同样从一个文本文件中读入:

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
William Henry Harrison, 5432109
John Tyler, 4321098

删除名称的函数有效,但是当它成功时,printf 语句只是继续在命令窗口中循环。我尝试在循环末尾使用 break 语句,但这只会导致说找不到该名称。有人可以提供任何见解吗?

#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 *delete_name);


//Main function
int main()
{
//Declare variables
int i, num, delete_id, 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:
getchar();
printf("Enter the name to insert:");
scanf("%[^\n]s", nameInsert);
printf("\nEnter the ID associated with the name: ");
scanf("%d", &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", &delete_id);
}

if(deleteID(delete_id))
printf("%d deleted successfully \n", delete_id);
else
printf("%d not found in the list\n", delete_id);
break;
case 4:
getchar();
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter name to delete: ");
scanf("%[^\n]s", nameDelete);
printf("Checking for name %s...\n", nameDelete);
printf("%s not found in the list\n", nameDelete);
deleteName(nameDelete);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}

//Define the functions
//Function to delete by name
void deleteName(char *delete_name)
{
//Create temporary and helper node
struct node *temp, *helper;

//Set temp equal to head
temp = head;

//Loop until the end of the list
while(temp != NULL)
{
if(strcmp(temp->name, delete_name) == 0)
{
if(temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
}
else
{
helper = temp;
temp = temp->next;
}
}
break;
}

最佳答案

请学习如何制作 MCVE ( How to create a Minimal, Complete, and Verifiable Example? ) 或 SSCCE ( Short, Self-Contained, Correct Example ) — 两个名称和相同基本思想的链接。

这是从您的代码派生的 MCVE。我在 deleteName() 的循环中添加了缺少的 break;return;。我基本上完全重写了 main(),但它运行得很干净:

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

struct node
{
char name[50];
int id;
struct node *next;
} *head;

void deleteName(char *delete_name);

int main(void)
{
struct node *n;

head = NULL;

head = malloc(sizeof(*head));
assert(head != 0);
strcpy(head->name, "Abraham Lincoln");
head->id = 1;
head->next = 0;

n = malloc(sizeof(*n));
strcpy(n->name, "George Washington");
n->id = 2;
n->next = head;
head = n;

n = malloc(sizeof(*n));
strcpy(n->name, "John Adams");
n->id = 3;
n->next = head;
head = n;

deleteName("George Washington");
deleteName("John Adams");
deleteName("Abraham Lincoln");

return 0;
}

void deleteName(char *delete_name)
{
struct node *temp, *helper = 0;

temp = head;

while (temp != NULL)
{
if (strcmp(temp->name, delete_name) == 0)
{
if (temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
return; // The key change!
}
else
{
helper = temp;
temp = temp->next;
}
}
}

这在 valgrind 下运行得很干净和带有 GCC 4.9.1 的 Mac OS X 10.10.2。

Found George Washington!
George Washington deleted successfully
Found John Adams!
John Adams deleted successfully
Found Abraham Lincoln!
Abraham Lincoln deleted successfully

重要的是要学会在创建 MCVE 时如何粗暴地删除不相关的代码。

关于c - 跳出这个 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com