Doubly Linked List insertion function not working

I am working on a function to insert a new node before a node I had previously returned from another function(complex). However I can't quite get it to function properly. I get how I'm supposed to do it but I can't figure out where I went wrong. This is for school FYI so answer accordingly. If you'd like more snippets of my code just ask. Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool TextClass::insertLast(char data)
{
    if(forFindNext->getNext() != nullptr && forFindNext->getPrevious() != nullptr)
    {
        Link* newLink;
        newLink->setValue(data);
        newLink->setPrevious(forFindNext->getPrevious());
        newLink->setNext(forFindNext);
        forFindNext->getPrevious()->setNext(newLink);
        forFindNext->setPrevious(newLink);
        return true;
    }else
    {
        return false;
    }
}
what does it not do?

line 5 new, ok
line 7 old previous <- new
line 8 old previous <- new -> old
line 9 old previous -> <- new -> old
line 10 old previous -> <- new -> <- old

I don;t see anything wrong, but that assumes everything does what its name implies.
Last edited on
This is for school FYI so answer accordingly
I'm not sure what that implies.

The most obvious issue with your code is that you're dereferencing an uninitialized (junk) pointer -- the newLink variable.

Since you're inserting, I assume you should call something like:
Link* newLink = new Link;
that would do it. Somehow I got into the logic and missed that, nice find.
> insert a new node before a node I had previously returned from another function
your function only takes a `data' parameter so I don't see how are you using that "returned node"
¿does that function work correctly? ¿may you get an invalid node?

> I get how I'm supposed to do it
and yet you neither explain it nor provide pseudocode
¿is the problem in your implementation or in your logic?

> can't figure out where I went wrong.
you didn't even tell what's the problem
¿do you get a crash? ¿a freeze? ¿does it blow the power in the whole neighbourhood? ¿is the output pure garbage? ¿or the values are slightly incorrect?

> This is for school FYI so answer accordingly
not a damn idea what you mean

> If you'd like more snippets of my code just ask.
I'm sick of the guessing game, post enough code to reproduce your issue.
ne555 I'm not sure what your problem is I'm literally just trying to figure out my homework. If you want clarification a " could you please clarify" is more than enough. I don't need the belligerent comments.:)

Anyway, main() is a driver provided by my teacher and the 'data' in the parameter is a char that is provided by the driver. The returned node I was referring to is forFindNext. That is a pointer that points to a node I specified in an earlier function. It is declared in the class's private.

From what I understand, I need to insert a new node in between to already existing nodes, so I need to create that node and change the pointers for previous and next on not only the new node but for the nodes before and after it.(hopefully that makes sense)

Everything runs fine until I hit insertLast() in main. Once it reaches that point, nothing is displayed and it actually wont stop running. If I hit the stop button in my IDE I get this exit code: -1073741510 (0xC000013A) if that means anything.

This is homework for school. Some people would think it's important for a student to learn and I wanted to provide that opportunity instead of hearing the answer out right. Sorry for the confusion.

I thought I had provided enough to reproduce the problem but, based of others comments I guess not. It's possible a previous function I had written isn't working correctly, so my bad I guess. I figured it was this function sense that's where it decides to stop.

If you'd like to look at something specific in my code, just ask.

> The returned node I was referring to is forFindNext.
> That is a pointer that points to a node I specified in an earlier function.
> It is declared in the class's private.
weird interface, ¿are you sure that's how supposed to work?
¿what if the client doesn't call that previous function?

going by the name, `insertLast()' I guess that it should «Add a new element at the end of the list container» (copied from list::push_back)
but your implementation requires `forFindNext' to be an inner node (and the list to have at least three elements)


> I need to create that node and change the pointers for previous and next on
> not only the new node but for the nodes before and after it.
yes, otherwise you lose properties like a->next->prev = a = a->prev->next
it seems that your `after' nodde is `forFindNext', ¿why then you care about `forFindNext->getNext()'?o


> Everything runs fine until I hit insertLast() in main.
¿are you executing step-by-step?
as Ganado pointed out, `newLink' is not initialised, ¿did you fix that?
but `forFindNext' could also be invalid
or the other links are fucked up and pointed anywhere
that's why we need enough code to reproduce your issue


> If you'd like to look at something specific in my code, just ask.
enough code to reproduce your issue
it should compile, run and crash like your code.
need to know how your list is created, how is filled with element, how do you set `forFindNext', etc
that's only possible by running your code.
Topic archived. No new replies allowed.