简体   繁体   English

减去链表的2个连续节点时出现分段错误

[英]segmentation fault while subtracting 2 consecutive nodes of a linked list

my aim is to create a function to subtract 2 consecutive numbers and then add that result to the front of those numbers我的目标是创建一个函数来减去 2 个连续的数字,然后将该结果添加到这些数字的前面

eg:例如:
input: 2->4->1->6->9->8->7输入:2->4->1->6->9->8->7
output: -2->2->4->-5->1->6->1->9->8->7输出:-2->2->4->-5->1->6->1->9->8->7

So this is my logic so far:所以这是我到目前为止的逻辑:

struct node
{
    int data;
    node *next;
};
void SubtractConsecutiveNodes()
{
    if(head==NULL || head->next==NULL)
    {
        return;
    }

    node *first =head,*prev;
    node *newNode;
    while(first!=NULL&&first->next!=NULL)
    {
        newNode->data=first->data-first->next->data;
        newNode->next = first;

        if(head== first)
            head = newNode;
        else
            prev->next=newNode;

        prev = first->next;
        first=first->next->next;
    }
}


but the problem is after doing first Iteration loop seems to go into a segmentation fault and then crash.但问题是在执行第一次迭代循环后似乎进入分段错误然后崩溃。

Link to my whole code: Assignment-3链接到我的整个代码: Assignment-3

In

void SubtractConsecutiveNodes()
{
    if(head==NULL || head->next==NULL)
    {
        return;
    }

    node *first =head,*prev;
    node *newNode;
    while(first!=NULL&&first->next!=NULL)
    {
        newNode->data=first->data-first->next->data;
        newNode->next = first;

        if(head== first)
            head = newNode;
        else
            prev->next=newNode;

        prev = first->next;
        first=first->next->next;
    }
}

newnode is not pointed at anything before it is dereferenced during newnode在它被取消引用之前没有指向任何东西

newNode->data=first->data-first->next->data;

A crash is one of the more polite things that can result from using an uninitialzed pointer, so a崩溃是使用未初始化的指针可能导致的更礼貌的事情之一,因此

node *newNode = new node;

is in order.一切顺利。

Addendum附录

I hate prev variables.我恨prev变量。 I'm not sure why.我不知道为什么。 Maybe I was murdered in a previous life or something.也许我前世被谋杀之类的。 Anyway, There's neat trick with a pointer to a pointer you can use to get rid of prev s and, at the same time, a lot of head -specific logic.无论如何,有一个指向指针的巧妙技巧,您可以使用它来摆脱prev ,同时摆脱许多特定于head逻辑。

A lot of the time multiple indirection (pointers to pointers) is frowned on, but pointers are like fire: A powerful servant, but a terrible master.很多时候多重间接(指向指针的指针)是不受欢迎的,但指针就像火:一个强大的仆人,但一个可怕的主人。 Master the pointer and by Crom, you can write some cool software!掌握指针并通过Crom,您可以编写一些很酷的软件!

void SubtractConsecutiveNodes()
{
    // if first is a pointer to a pointer to a node, we can point it at a `next`
    // and eliminate the need for prev. This has the added bonus of turning head
    // into just another next pointer, eliminating all of the head-specific logic.
    // if(head==NULL || head->next==NULL) is identical to the while exit condition 
     // once head is hidden behind first. Chuck it. You don't need it. 
    node **first = &head;
    while ((*first) != NULL && (*first)->next != NULL) 
        // extra level of indirection, so extra dereferencing required.
    {
        node *newNode = new node;
        newNode->data = (*first)->data - (*first)->next->data;
        newNode->next = *first;
        (*first) = newNode; // places newnode directly into prev->next or head, 
                            // whichever happens to be there.

        first = &(*first)->next->next->next; 
        //since we added another node in we need an extra ->next
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM