c - 跳出这个 while 循环

标签 c function loops while-loop 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
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/

相关文章:

c++ - 带指针的基于范围的循环

Java - 打乱元素并输出特定数量

C++ #include 外部函数问题

c - Windows 上的堆损坏操作 C 中的结构指针数组

python - 如何绘制给定积分的面积?

function - 创建自定义 XSLT 函数

python - 按增量器对文本列进行排序并枚举

c - 如何在shell程序中实现&>和&>>?

c++ - __PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什么区别?

c++ - 在 C++ 中对来自全局函数的结构成员使用方法