C编程链表的位置N删除节点

C编程链表的位置N删除节点

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

问题描述

编辑:想通了这个问题。此外,如果你发现这个通过谷歌或这里其他搜索引擎是我出了问题,以及如何解决它。

我deleteNode()方法是通过列表中正常移动与正确的温度,保持头部不动。我要去哪里错了是什么,我回国的方法的结果。因为它通过列表,直到找到确定的位置,我回来不是临时或newNode这是不正确。一旦检测到规定的位置,那将重新分配 - >下一个指针指向下一步 - >下一步>指针是正确的,但我再次被返回错误的事情。因为我们已经在列表中使用温度/ NewNode感动我们失去了头,我们正在返回我们找到了位置,一切仍然在列表中的下一个位置。

My deleteNode() method was moving through the list properly with the correct temp and keeping the head untouched. Where I was going wrong was in what I was returning as the result of the method. I was returning either temp or newNode which is incorrect because it goes through the list until it finds defined position. Once it finds that defined position it it would reassign the ->next pointer to point to the next->next> pointer which is correct but again I was returning the wrong thing. Because we had moved through the list using temp/NewNode we lost the header and we were returning the position we found and whatever was still in the next positions of the list.

我们如何解决这个问题将返回头(这是被传递到方法)。为什么这个工作的原因是因为我们要了解LinkedLists是如何工作的。每个节点指向下一个节点的指针。防爆。我们有一个链表| A | | - | B | 的| - | C | | - | D | 的| - | E | | - | F | |

How we fix this is returning the head (which is what is passed into the method). The reason why this works is because we have to understand how LinkedLists work. The pointers of each node point to the next node. Ex. we have a linked list |A|| - |B|| - |C|| - |D|| - |E|| - |F||

如果我们想删除节点C,我们使用移动临时指针到节点B,然后分配B->旁边TEMP->下一步 - >下一因此对C节点跳跃和分配Ð节点。

If we want to delete Node C we move to node B using the temp pointer and then assign the B->next to temp->next->next Thus skipping over C node and assigning D node.

请注意:。(据我所知,这实际上并不免费的C节点的内存,所以它不是最好的做法,因为你可能会导致内存泄漏这样)你应该使用免费的()方法的C节点

NOTE: (From what I know this does not actually free the memory of C node so it isn't best practice because you can cause memory leaks this way) You should use the free() method on the C node.

下面是code最后我用

Here is the code I ended up using

struct node* DeleteNode(struct node* head, int pos) {

     struct node* temp = head;
     int length = LinkedListLength(temp);
     int i;

    if(pos <= 0 || pos > length){
        printf("ERROR: Node does not exist!\n");
    }else{
        if(pos == 1){
            head = head->next; //move from head (1st node) to second node
        }else{
            for(i = 1; i < pos-1; ++i){ //move through list
                    temp = temp->next;
            }
            temp->next = temp->next->next;
        }
    }
    return head;
}

希望帮助明白我怎么出去修复它。

Hopefully that helps understand how I went out fixing it.

//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////

原帖结果
//////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////// ////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
ORIGINAL POST
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

编辑:注意:这是一个家庭作业我已经花了几天时间(估计4小时)编程它,我只是停留在这一个部分。您可以查看我下面尝试

我已经能够插入和从开始时/结束然而删除我似乎无法让我删除节点的位置N在LinkedList的工作。

I've been able to insert and delete from begining/end however I can't seem to get my delete node at position N in linkedlist to work.

我的伪code是这样的:

My psuedocode looks like this:


  1. 的LinkedList:1,3,5,7,9,23

  2. 抓斗的LinkedList

  3. 创建新的结构节点A =头

  4. 通过LinkedList的移动,直到
    位置

  5. 分配节点节点 - >下一步

  6. 返回LinkedList的

输入例

Node structure
int data;
struct node* next;

int values[] = {1,3,5,7,9,23};
struct node* llist = CreateList(values,6);

llist = DeleteNode(llist, 1);
llist = DeleteNode(llist, 5);
llist = DeleteNode(llist, 3);

这应该离开LLIST的价值观3,5,9,一旦code已运行然而,它与替换第一个节点0

Which should leave the llist with the values 3, 5, 9 once the code has been run However, It is replacing the first node with a 0

实际code:

struct node* DeleteNode(struct node* head, int pos) {

struct node* temp = head;
struct node* newNode = head;
int length;
int i;

printf("DeleteNode: position = %d \nBefore: ", pos);
PrintList(temp);

if(pos <= 0){ //node does NOT exist
    printf("ERROR: Node does not exist!\n");
}else{ //node DOES exist
    length = LinkedListLength(temp);

    if(length < pos){ //if length < position Node does not exist
        printf("ERROR: Node does not exist!\n");
    }else{
        if(pos == 0){
            newNode = temp->next;
        }else if(pos == 1){
            newNode = temp->next;
        }else{
            for(i = 1; i < pos; i++){
                printf("i = %d\n", i);
                temp = temp->next;
                newNode->next;
            }
            if(temp->next == NULL){
                newNode = NULL;
            }else{
                newNode = temp->next;
            }
        }
    printf("After: ");
    PrintList(newNode);
    printf("\n");
    }
}
return newNode;
}

编辑#2:code错字

EDIT #2: Code typo

感谢提前任何帮助。从我所总结我的问题是,我没有在列表中移动正常,但我不能确定,为什么我不是。

Thanks for any help in advance. From what I have concluded my problem is that I am not moving through the list properly but I am unsure as to why I am not.

推荐答案

在您的code,你也行

In your code, you have the line

newNode->next;

循环。该操作并不做任何事情。

in your for loop. That operation doesn't do anything.

您也有

newNode-> = NULL;

这是不合法的C,我不知道你是怎么了编译。

which is not valid C, and I have no idea how you got that to compile.

不过说真的,不要使用该循环。链表是最基本的递归数据结构中的一个。其结果是,几乎所有的算法对它们进行处理是最优雅的一个递归解决方案。

But really, don't use that loop. A linked list is one of the most basic recursive data structures. As a result, almost all algorithms manipulating them are most elegant as a recursive solution.

typedef struct node node_t;

node_t* delete_at_index(node_t* head, unsigned i)
{
    node_t* next;

    if(head == NULL)
        return head;

    next = head->next;

    return i == 0
             ? (free(head), next)                                 /* If i == 0, the first element needs to die. Do it. */
             : (head->next = delete_at_index(next, i - 1), head); /* If it isn't the first element, we recursively check the rest. */
}

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

08-22 23:23