链表删除节点,简单链表 [英] Linked List Delete Node , Simple linked list

查看:65
本文介绍了链表删除节点,简单链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现从链接列表中删除节点的功能.到目前为止,我只可以删除list(3)的第一个节点.

I'm trying to implement a function that deletes nodes from a linked list. So far, I can delete just the first node of the list(3).

我试图从delete转到for循环,我认为内存分配不正确,我已经苦苦挣扎了几天,我听不懂,请帮我一下,这是我收到的主题从大学.

I tried to go to the for loop from delete, I thought that the memory is not well allocated, I have been struggling for a few days and I don't understand, please help me a little, it's the topic I received from college.

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

typedef struct nod
{
    int key;
    struct nod *urm;
} NOD;

NOD *first=0,*last=0;

void add(int x)
{
    NOD *p=(NOD*)malloc(sizeof(NOD));
    p->key=x;
    p->urm=0;
    if(0==first)
    {
        first=p;
        last=p;
    }
    else{
        last->urm=p;
        last=p;
    }
}

void delete(int x)
{
    NOD *q,*p;
    if(first->key==x)
    {
        p=first;
        first=first->urm;
        free(p);

    }
    else{
        for(p=q=first;p=0;q=p,p=p->urm)
        {
            if(p->key==x)
            {
            q->urm=p->urm;
            if(p==last)
            {
                last=q;
            }
            free(p);
            }
        }
    }
}

void show()
{
    for(NOD *p=first;p!=0;p=p->urm)
    {
        printf("%d ",p->key);
    }
    printf("\n");
}
int main()
{
    add(3);
    add(1);
    add(2);
    add(5);
    show();

    delete(2);
    show();

    return 0;
}

推荐答案

对于初学者来说,您显示的代码不是C ++代码.这是C代码.

For starters the code you showed is not a C++ code. It is a C code.

定义全局变量(如 first last )以及函数何时依赖于全局变量是一个坏主意.在这种情况下,您不能在一个程序中创建多个列表.

It is a bad idea to define global variables like first and last and when functions depend on global variables. In this case you can not create more than one list in a program.

对于功能 delete ,通常它具有未定义的行为.可以将其称为空列表.

As for the function delete then in general it has undefined behavior. It can be called for an empty list.

此外,在此循环中

for(p=q=first;p=0;q=p,p=p->urm)

条件表达式中有错字.您正在使用赋值运算符而不是比较运算符.

there is a typo in the condition expression. You are using the assignment operator instead of the comparison operator.

当列表仅包含一个节点时,您可以忽略该情况,因为在这种情况下,列表不会更新最后一个节点.

And you function ignore the case when the list contains only one node because in this case it does not update the last node.

尽管如此,使用函数删除功能的方式还是可以这样.

Nevertheless using your approach the function delete can look the following way.

void delete(int x)
{
    if ( first )
    {
        if ( first->key == x )
        {
            NOD *tmp = first;
            first = first->urm;

            free( tmp );

            if ( first == NULL ) last = NULL;
        }
        else
        {
            NOD *p = first;
            while ( p->urm != NULL && p->urm->key != x )
            {
                p = p->urm;
            }

            if ( p->urm != NULL )
            {
                NOD *tmp = p->urm;
                p->urm = p->urm->urm;

                free( tmp );

                if ( p->urm == NULL ) last = p;
            }
        }
    }
}     

这是一个演示程序.

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

    typedef struct nod
    {

    int key;
    struct nod *urm;
    } NOD;

    NOD *first=0,*last=0;


    void add(int x)
    {

    NOD *p=(NOD*)malloc(sizeof(NOD));
    p->key=x;
    p->urm=0;
    if(0==first)
    {
        first=p;
        last=p;
    }
    else{
        last->urm=p;
        last=p;
    }

    }

void delete(int x)
{
    if ( first )
    {
        if ( first->key == x )
        {
            NOD *tmp = first;
            first = first->urm;

            free( tmp );

            if ( first == NULL ) last = NULL;
        }
        else
        {
            NOD *p = first;
            while ( p->urm != NULL && p->urm->key != x )
            {
                p = p->urm;
            }

            if ( p->urm != NULL )
            {
                NOD *tmp = p->urm;
                p->urm = p->urm->urm;

                free( tmp );

                if ( p->urm == NULL ) last = p;
            }
        }
    }
}  

    void show()
    {
        for(NOD *p=first;p!=0;p=p->urm)
        {
            printf("%d ",p->key);
        }
        printf("\n");
    }
    int main()
    {
        add(10);
        add(20);
        add(30);
        add(40);

        show();

        delete(30);
        show();

        add( 50 );
        add( 60 );
        add( 70 );
        add( 80 );
        show();

        delete(80);
        show();


    return 0;
    }

其输出为

10 20 30 40 
10 20 40 
10 20 40 50 60 70 80 
10 20 40 50 60 70 

这篇关于链表删除节点,简单链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆