Question

I seem to turn in circles with this task. Even drawing it out doesn't seem to give me a working solution. Could someone help me find where my thought process breaks down here?

// method receives node ins to be inserted
// and node prev in front of which ins should be inserted
public void insertIntermediate(DLLNode ins, DLLNode prev)
{
    ins.pred = prev ; // update node ins' predecessor information
    ins.succ = prev.succ ; // update node ins' successor information 
    prev.succ = ins ; // update list's information
    prev.succ.pred = ins ; // update list's information
}

[EDIT2] (removed edit1 to reduce clutter)

@Andrew, ok i found it: what was wrong with the above was the order of line 3 & 4:

line 3 caused me to lose access to prev.succ.pred.

By swapping the two lines i solved that problem. Thanks for the hint!

ADD-ON QUESTION:

I came across another weird problem though and that's why i lost so much time finding the solution: if i re-insert an already existing element again, for some reason the whole thing goes into a infinite loop when i print it...e.g.

myList.addBeforeFirst(B) ;
myList.addBeforeFirst(B) ;

causes a loop, whereas:

myList.addBeforeFirst(B) ;
myList.addBeforeFirst(C) ;

works fine

here's the method:

public void addBeforeFirst(DLLNode ins)
{
    ins.succ = first ;
    ins.succ.pred = ins ;
    first = ins ;
}

and the nodes:

DLLNode B = new DLLNode("Hi", null, null) ;
DLLNode C = new DLLNode("Salut", null, null) ;

why is that happening?

Was it helpful?

Solution

This is a doubly-linked list, yes? What does that mean? Make sure you are updating all the relevant links, in the right order. Also, make sure the all the relevant nodes exist.

Edit:

Let's go through your statements in order.

Before starting, you have prev, whose property succ points at the successor node. (Or does it? Could there be an exception to that? How would you handle such an exception, should it exist?) prev.succ.pred should be the same as prev provided that prev.succ points at the successor node.

Now, your first two statements set ins.pred and ins.succ. This is basically the correct idea, but see my questions above.

Now, you set prev.succ to ins. Focus on this statement. Is there information you have now lost? What exactly does the following statement do, then? Is it what you intended?

I think this is about the best I can do without giving away the solution entirely.

Edit for infinite loop problem:

The issue you are having is the difference between passing parameters by value and passing parameters by reference. Because you are passing by reference, you passed a reference to the same object (B) twice. That means you ended up doing the following:

First call:

B.succ = first;
B.succ.pred = B; /* why not just first.pred = ins? */
first = B;

Now, first is set to B. So, second call, using B:

B.succ = B;
B.succ.pred = B; /* so, now B is its own pred and succ */
first = B; /* no change here */

So, now, first is B, first.succ is B, first.succ.succ is B, etc., hence the infinite loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top