- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我同样从一个文本文件中读入:
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/
这个问题刚刚出现:如何打破 if 语句?我有一个很长的 if 语句,但有一种情况我可以尽早摆脱它。 在循环中我可以这样做: while (something ) { last if $some
我有一个 iframe,它显示带有可点击链接的 flash 横幅广告。无论如何强制链接在父窗口中打开,而不将 JS 放在打开或更改 flash 文件的页面上? 例如,我可以对 iFrame 做些什么来
JSHint 告诉我不要在 if 语句中使用标签(就 jshint 而言,标签似乎只用于循环)。 forminjection: if (options.addToForm !== false) {
我正在动态地将文本字段(最多 32 个)添加到我的页面中,我需要遍历所有这些文本字段并检查,如果它们全部为空,我将显示一条警告消息!我不想使用 document.getElementsByTagNam
我正在编写一个程序,其中我使用 if 语句来检查某些条件;如果为真,我会增加一个计数器。问题是,一旦语句为真,变量要么无限递增,要么随机递增。 如果条件满足但我一直在尝试使用一些子句来打破这个语句 我
有一个similar question关于 C++ 中的这个问题,但我在这里使用 JavaScript。我基本上和另一篇文章中的 OP 处于相同的情况。 var input = prompt(); w
我一直在研究 Issue 14在 PascalScript 脚本引擎上,使用 Goto 命令跳出 Case block 会产生编译器错误,即使这是完全有效(如果丑陋)的 Object Pascal 代
我正在使用 VBA 的 While...Wend 循环。 Dim count as Integer While True count=count+1 If count = 10 The
我在这个网站上看到过类似的问题,但我无法让它工作。我正在尝试通过 Javascript 检测输入框中的重复条目 - 但我希望在输入重复项时循环中断。我有那部分工作,但循环继续运行,它创建了一个用户无法
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个问题,我可能需要因为某些值(value)而跳出整个 promise 链,或者根据一个值(value)采取两条路径。 你怎么做最好? 这是我想跳出整个链条的第一个场景。我只想给他们留言。
因此,对于类作业,我必须接受用户输入并分配我的沙鼠对象每天可以使用的“食物”量。在这种情况下,我已经从用户那里获取了每天的最大食物量,如果用户尝试输入超过每日最大值的值,我需要向用户提供错误消息。 如
我正在尝试制作一个导航栏,它看起来像这样 https://imgur.com/a/wf7Pe 是有链接的那个,忽略白色的div。 问题是,如果调整页面大小,文本会超出 div,位于其下方。 像这样:
我正在尝试使用 C 退出 while 循环,但使用中断、返回或更改 while 条件不起作用。 基本上,我想知道为什么在最后一个 else if 中,当我声明 back = true 时,我的代码一直
我正在寻找关键字或干净的方式来不使用选择语句退出finally block 。考虑以下示例: private bool AtteptToDoSomething() { try {
我有一个 python 脚本,它将解析 xml 文件中的序列号并将它们写入文本文件。下面代码的问题是,它正在进行无限循环。如果我在记录到文件后的某个位置添加一条break语句,它只会写入一个序列号。如
我在类 LinkRepository 中有一个方法,我正在检查 vector 数组 Datalinks 中的重复条目,它是该类的一个成员。我遍历数组中的所有元素以检查新条目 Datalink* dat
是否可以在每个循环中跳出下划线……? _.each(obj, function(v,i){ if(i > 2){ break // <~ does not work } // so
我想优化依赖于 filter() 的函数。在某些情况下,我想在它们达到特定元素时打破它们。 (例如,我可能有一个包含不同元素的数组。或者,我只想实现一种 findFirst 功能。)在这种情况下,函数
我是一名优秀的程序员,十分优秀!