删除节点-链接列表-C [英] Delete Node - Linked List - C

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

问题描述

我试图从链表中删除一个节点,但是我对双指针的概念还是陌生的,所以我尝试使用一个全局变量来代替头部指针.但是,当我删除中间节点后尝试打印列表时,得到了错误的结果.

我看到了这个问题删除链接列表中间的节点,我不知道我的删除节点功能与答案有何不同.

这是我的代码:

  #include< stdio.h>#include< stdlib.h>typedef unsigned char u8;typedef struct Node节点;void addfirstnode(u8);void addnode(u8);void print(void);void deletenode(u8键);void deleteonlynode();void deletefirstnode();结构节点{u8 x;节点* next;};节点* head;u8长度= 0;void main(void){u8 x;printf("\ n要添加节点,请输入0 \ n要打印链接列表,请输入1 \ n要退出,请按2 \ n要删除节点,请按3 \ n您的选择:");scanf(%d",& x);如果(x == 2){printf("\ n谢谢\ n再见");}而(x!= 2){开关(x){u8 n;u8键;案例0://添加节点printf("\ n请输入第一个值:");scanf(%d",& n);如果(长度== 0){addfirstnode(n);//printf(%d",head-> x);}别的{addnode(n);}printf("\ nNode已添加,谢谢\ n");休息;情况1://打印打印();休息;情况3://DeleteNodeprintf("\ n请输入要删除的值:");scanf(%d",& key);deletenode(key);//deletefirstnode();休息;默认:printf("\ n无效的选择,请重试\ n");}printf("\ n要添加节点,请输入0 \ n要打印链接列表,请输入1 \ n要退出,请按2 \ n要删除节点,请按3 \ n您的选择:");scanf(%d",& x);如果(x == 2){printf("\ n谢谢\ n再见");}}//我在哪里使用free();}void addfirstnode(u8 n){头=(节点*)malloc(sizeof(节点));head-> next = NULL;头-> x = n;长度++;}void addnode(u8 n){节点* last = head;while((last-> next)!= NULL){last = last-> next;}last-> next =(node *)malloc(sizeof(node));(last-> next)-> next = NULL;(last-> next)-> x = n;长度++;}无效打印(void){节点* last = head;u8计数= 1;printf("\ n ---------------------");如果(最后== NULL){printf("\ nList为空");}while(last!= NULL){printf("\ nNode Number%d =%d",count,last-> x);last = last-> next;数++;}printf("\ n ---------------------");printf("\ n");}无效deletenode(u8键){节点* last = head;//node * postlast = NULL;if((last-> x ==键)&&(last-> next == NULL)){deleteonlynode();}别的{while(last!= NULL){如果((last-> x)== key){printf(找到要删除的值");节点* temp = last-> next;last-> next = last-> next-> next;free(temp);长度 - ;}last = last-> next;}}}无效deleteonlynode(){printf("\ n删除唯一的节点");free(head);头= NULL;长度 - ;}无效deletefirstnode(){printf("\ n删除第一个节点");节点* temp = head;头=头->下一个;free(temp);长度 - ;} 

解决方案

代码从链接列表中删除了错误的项目:

请参阅:

  if((last-&x; x)==键){printf(找到要删除的值");节点* temp = last-> next;//last-> next?不,就在最后.last-> next = last-> next-> next;free(temp);长度 - ;} 

last 指向要删除的元素.但是然后代码将 temp 分配给 last-> next (不是 last ),然后从列表中删除.

因此,通过查看 node-> next 而不是当前节点,可以对它进行修剪,因为您是从要删除的指针开始的.基本上,您的代码已经差不多了.

  void deletenode(u8键){节点* ptr =头;如果((ptr-> x == key)){//删除first/head元素节点* temp = ptr;头=头->下一个;free(temp);长度 - ;}别的{while(ptr-> next!= NULL){if((ptr-> next-> x)==键){printf(找到要删除的值");节点* temp = ptr-> next;ptr-> next = ptr-> next-> next;free(temp);长度 - ;}ptr = ptr-> next;}}} 

我还随意将 last 重命名为 ptr ,因为这使我感到困惑.

已更新,也可以干净地取下头部.

I am trying to delete a node from a linked list but I am still new to the concept of double pointers so I tried using a global variable to hold the head pointer instead. However, I get the wrong results when I try to print my list after deleting the middle node.

I saw this question deleting a node in the middle of a linked list and I don't know how is my delete node function different from the answer.

Here is my code:

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

typedef unsigned char u8;
typedef struct Node node;
void addfirstnode( u8 );
void addnode( u8 );
void print( void );
void deletenode( u8 key );
void deleteonlynode();
void deletefirstnode();

struct Node
{
    u8 x;
    node *next;
};
node *head;
u8 length = 0;


void main( void )
{
    u8 x;
    printf( "\nTo add node enter 0\nTo print linked list enter 1\nTo exit press 2\nTo delete node press 3\nYour Choice:" );
    scanf( "%d", &x );
    if ( x == 2 )
    {
        printf( "\nThank You\nGood Bye" );
    }
    while ( x != 2 )
    {
        switch ( x )
        {
            u8 n;
            u8 key;
            case 0:            //Add node
                printf( "\nPlease enter first value:" );
                scanf( "%d", &n );
                if ( length == 0 )
                {
                    addfirstnode( n );
                    //printf("%d",head->x);
                }
                else
                {
                    addnode( n );
                }
                printf( "\nNode added , Thank you\n" );
                break;

            case 1:            //Print
                print();
                break;

            case 3:            //DeleteNode
                printf( "\nPlease enter value to be deleted:" );
                scanf( "%d", &key );
                deletenode( key );
                //deletefirstnode();
                break;

            default:
                printf( "\nInvalid Choice please try again\n" );
        }
        printf( "\nTo add node enter 0\nTo print linked list enter 1\nTo exit press 2\nTo delete node press 3\nYour Choice:" );
        scanf( "%d", &x );
        if ( x == 2 )
        {
            printf( "\nThank You\nGood Bye" );
        }
    }
    //where do I use free();
}

void addfirstnode( u8 n )
{
    head = ( node * ) malloc( sizeof( node ) );
    head->next = NULL;
    head->x = n;
    length++;
}

void addnode( u8 n )
{
    node *last = head;
    while ( ( last->next ) != NULL )
    {
        last = last->next;
    }
    last->next = ( node * ) malloc( sizeof( node ) );
    ( last->next )->next = NULL;
    ( last->next )->x = n;
    length++;
}

void print( void )
{
    node *last = head;
    u8 count = 1;
    printf( "\n---------------------" );
    if ( last == NULL )
    {
        printf( "\nList is empty" );
    }
    while ( last != NULL )
    {
        printf( "\nNode Number %d = %d", count, last->x );
        last = last->next;
        count++;
    }
    printf( "\n---------------------" );
    printf( "\n" );
}

void deletenode( u8 key )
{
    node *last = head;
    //node*postlast = NULL;
    if ( ( last->x == key ) && ( last->next == NULL ) )
    {
        deleteonlynode();
    }
    else
    {
        while ( last != NULL )
        {
            if ( ( last->x ) == key )
            {
                printf( "value to be deleted is found" );
                node *temp = last->next;
                last->next = last->next->next;
                free( temp );
                length--;
            }
            last = last->next;
        }
    }
}

void deleteonlynode()
{
    printf( "\n Deleting the only node" );
    free( head );
    head = NULL;
    length--;
}

void deletefirstnode()
{
    printf( "\n Deleting the first node" );
    node *temp = head;
    head = head->next;
    free( temp );
    length--;
}

解决方案

The code is removing the wrong item from the linked list:

See:

        if ( ( last->x ) == key )
        {
            printf( "value to be deleted is found" );
            node *temp = last->next;     // last->next? No, just last.
            last->next = last->next->next;
            free( temp );
            length--;
        }

last is pointing at the element to be removed. But then the code assigns temp to point at last->next (NOT last), and then cuts that from the list.

So by looking at node->next rather than the current node, it's possible to trim it out since you're starting from the pointer before the one to remove. Basically your code was almost there already.

void deletenode( u8 key )
{
    node *ptr = head;

    if ( ( ptr->x == key ) )
    {
        // Delete the first/head element
        node *temp = ptr;
        head = head->next;
        free( temp );
        length--;
    }
    else
    {
        while ( ptr->next != NULL )
        {
            if ( ( ptr->next->x ) == key )
            {
                printf( "value to be deleted is found" );
                node *temp = ptr->next;
                ptr->next = ptr->next->next;
                free( temp );
                length--;
            }
            ptr = ptr->next;
        }
    }
}

Also I took the liberty of renaming last to ptr because it was confusing me.

EDIT: Updated to remove the head cleanly too.

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

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