c - 使用 "while loop"而不是使用 "for loop"删除链表中的节点时出现错误

标签 c for-loop while-loop

我编写了一个程序来创建、删除、插入链表,如下所示:

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

struct node
{
    int value;
    struct node *link;

};
struct node *head;

int menu();
int create_insert();
int delete();
int print();


int main()
{
    head = malloc(sizeof(struct node));
    menu();
    return 0;
}

int menu()
{
    int n;
    while(1)
    {
        printf("\nMENU\n");
        printf(" 1.Create & Insert\n 2.Search\n 3.Display\n 4.Delete \n 5.Exit\n");
        printf("?: ");
        scanf("%d",&n);

        switch(n)
        {
            case 1:create_insert();print();break;
            case 4:delete();print();break; 
            default : printf("Enter correct choice.");
        }
    }


    return 0;
}
int delete()
{
    struct node *temp,*temp1;
    temp1 = NULL ;
    int val;
    temp = head ;

    printf("\nEnter the value to be deleted: ");
    scanf("%d", &val);
    while ( temp->link != NULL || temp -> value == val  )
    {
        //printf("%d->", temp->value) ;
        temp1 = temp;
        temp = temp->link ;
        //printf("I am in while \n");
    }
    //printf("I am outside while \n");
    if ( temp == NULL )
    {
        printf("%d is not found.\n",val);
    }
    else
    {
        if ( temp1 == NULL )
        {
            head = head -> link ;
        }
        else
        {
            temp1->link = temp -> link ;
        }

    }
    //printf("NULL\n");
    return 0;
}

int create_insert()
{
    int val,check;

    printf("Enter the first value to be inserted: ");
    scanf("%d",&head->value);

    head->link = NULL ;

    printf("Want to insert more? ( 0 to break ): ");
    scanf("%d",&check);

    while( check != 0 )
    {
        printf("Enter the value to be inserted: ");
        scanf("%d", &val);
        struct node *new_node = malloc(sizeof(struct node));

        new_node -> value = val ;
        new_node -> link = head ;

        head = new_node ;

        printf("Want to insert more? ( 0 to break ): ");
        scanf("%d",&check);

    }
    return 0;

}

int print()
{
    struct node *temp;

    temp = head ;

    printf("\nLinked list is: ");
    while ( temp != NULL )
    {
        printf("%d->", temp->value) ;
        temp = temp->link ;
    }
    printf("NULL\n");
    return 0;
}

抱歉,代码有点长。在这里,我在删除函数中有一个 while 循环,如下所示:

while ( temp->link != NULL || temp -> value == val  )
{
    //printf("%d->", temp->value) ;
    temp1 = temp;
    temp = temp->link ;
    //printf("I am in while \n");
}
//printf("I am outside while \n");
if ( temp == NULL )
{
    printf("%d is not found.\n",val);
}
else
{
    if ( temp1 == NULL )
    {
        head = head -> link ;
    }
    else
    {
        temp1->link = temp -> link ;
    }

}

这个循环在这里不起作用,只删除最后一个元素。现在,我将其替换为 for 循环,如下所示:

for ( temp = head , temp1 = NULL ; temp != NULL && temp->value != val ; temp1 = temp , temp = temp->link)
    {}

    if ( temp1 == NULL )
    {
        head = head -> link ;
    }
    else
    {
        temp1 -> link = temp -> link ;
        free(temp);
    }

运行良好。请告诉我 while 循环哪里错了。

最佳答案

循环不相同!!!

while循环中的条件:

temp->link != NULL || temp->value == val

for 循环中的条件:

temp->link != NULL && temp->value != val

关于c - 使用 "while loop"而不是使用 "for loop"删除链表中的节点时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685446/

相关文章:

c - avr gcc 内联 asm 变量输入操作数

javascript - 使用 .each() 处理 jQuery 函数时遇到问题

c - 释放函数内部的指针,并在 main 中使用它

c - 在 C 程序中传递数组作为参数并打印数组项

ios - 从解析查询 block 返回 UIImage 数组

arrays - 跳到字符串中的下一个元音和辅音

Python Black Jack 游戏变种

python - 需要帮助指定结束 while 条件

Codeforces 输出不同的东西

javascript - Javascript 中奇怪的 FOR 循环行为