Insert before *this realocation.

Pages: 123
I've managed to work my way around this issue myself again. This is my new function:

1
2
3
4
5
6
7
8
9
10
11
	void Insert(ListElement &temp)
	{
		ListElement* newNode = &temp;

		newNode->mPrev = Previous();
		newNode->mNext = Previous()->Next();
		
		if(Previous())
			Previous()->mNext = newNode;
			
		Previous()->mNext = newNode;


To anyone who was interested.

However, I do have an additional query. If I wanted to insert the entirety of an additional list, how would I continue inserting until I reached the end? At the moment this method only inserts the first value.
Your last version looked better to me.
Here are my comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	void Insert(ListElement &temp)
	{
		ListElement* newNode = &temp;

		newNode->mPrev = Previous();
                // why don't you use "this" in the next line anymore?
		newNode->mNext = Previous()->Next();
		
                // guard against the previous being NULL
		if(Previous())
			Previous()->mNext = newNode;

                // but then perform the action anyway?
		Previous()->mNext = newNode;
        }
I didn't notice that, for my assignment at the moment it's not particularly important, so I just put an assert instead and removed the extra statement.

At the moment I've made some progress by removing newNode->mNext = Previous()->Next();

This now adds and prints the entirety of the new list from it's start to finish, but the issue now is that last node of the new list doesn't have the correct next value due to it being reassigned with Previous()->mNext = newNode;. Any tips on how to correct this error?
Again, I've managed to solve this newest issue through rigorous debugging.

Thank you all for your patience and motivation. Especially you Simeonz. I'm continuing on to the next functions now, but will be sure to post here if I have any queries.
Last edited on
Well, it looks like this hasn't helped much, even though I've managed to circumnavigate the issue of nth not updating to the new inserting value, it now appears my next task is to remove the nth node, and as a result, I need nth to change in value. Anyone have any tips on how to achieve this? My problem is the same, since I'm unable to reassign *this, how would I get around the issue?
Last edited on
Your implementation of Insert from Jan 12, 2011 at 9:27pm seems ok to me. I mean, something is probably wrong with the way you use it in the calling code.
It should have been enough, but the list I'm inserting into is simply a pointer to the nth value of the root list. I need to change the value of *this pointer pointing to the nth value after each insertion. Since I'm unable to achieve this due to not being able to reassign the *this pointer, I've had to think of ways around this.

All of this being said though, I still need to update that value somehow in order for remove to function correctly, if that were the case I would be able to use the insert you just referenced.

***Edit***

I was considering adding a holder value for the nth value in the class members then when it is found simply updating accordingly and then again after every insertion. I'm wondering if that is a bit of a hack to get around my issue though...
Last edited on
You are inserting new node before the nth element, right?

Anyways, what I can figure out from your explanation is that you seek some element with position n in the list and start inserting before it.

If the list is:
1   2   3   4
a - b - c - d

Now, you seek element no. 3 and insert two elements e and f. You should now have, after the first insertion (e):
1   2   3   4   5
a - b - e - c - d

and then, after the second insertion (f):
1   2   3   4   5   6
a - b - e - f - c - d

Is that so?
Yes, that is essentially what is happening, however the pointer in the main function ListElement *nth = root->Nth(3); Still points to position 3rd position in the root after my modifications, due to the fact that I'm unable to alter the *this pointer.

When I've been inserting, I've been inserting into nth not root.
Last edited on
So, in the above example:
1   2   3   4   5   6
a - b - e - f - c - d
Do you want nth to point to element no.5 or to element no.3 after the insertions?
At the moment it points to element number 5, I'd like it to point to 3, but have no idea how to achieve this being unable to modify the *this pointer. The only way I'm able to get the correct Nth value is if I call the Nth function with the root node again in main.
Last edited on
Why don't you try
1
2
nth->Insert(..);
nth = nth->Prev();
every time.
I could very well add that, but the brief has a predefined main(), so I'd prefer to find a correct solution and not deviate around it...
Are you sure that the Insert is supposed to be insert-before like in your case, and not insert-after?

With insert-after the result changes to:
1   2   3   4   5   6
a - b - c - f - e - d

And then nth will point to element no. 3.
Yes, very sure. The list is a list of strings, if I had to insert the new list after, it would make no sense.
Last edited on
Ok. Here's the deal, to the best of my knowledge. The options are:

1) insert-before: nth will become n+1th, then n+2nd, etc. This is simply because you insert before, and you technically have no way to alter nth from within the method, and you are not permitted alter nth from outside the method. So nth is pushed forward to succeeding positions.
2) insert-after: nth will stay nth. Naturally, because the new elements are placed after it.
3) insert-after and then swap the contents of data for the new node and the current nth node from within the insert method. But there are undesirable side-effects.

Basically, suppose you have:
 1   2   3   4
'a' 'b' 'c' 'd'
 p - q - r - s
, where 'a'..'d' are the values and p..s are the node objects that store those values.

Now you want to insert object (node) t holding the value 'e' at position 3 in the list.

The Insert method will be implemented in two steps:

1 insert-after operation resulting in
 1   2   3   4   5
'a' 'b' 'c' 'e' 'd'
 p - q - r - t - s

2 swap contents of the new inserted node and the current (i.e. *this) node
 1   2   3   4   5
'a' 'b' 'e' 'c' 'd'
 p - q - r - t - s

Notice that as a side effect the method changes the value of the inserted node (in this case t) and the current nth node (in this case r) and therefore may not be desirable depending on the requirements.

Well, I guess, you can also create a wrapper class that contains pointer to node object and present this to the user. Then you will be able to alter the pointer within the Insert method. But the wrapper class will not be a node class, and the wrapper and node objects will be in many-to-one relationship (several wrappers can internally hold pointer to the same node).

EDIT: In response to your edit. Actually, if the insertion simulates inserting characters in a string like a text editor does, then fine. But still, when you insert characters the cursor remains after the inserted characters - at least in text editors. I mean, the cursor moves to forward positions like in your case, it doesn't return back to the original position before the insertion, like what you want to achieve. Just analyzing the rationale.
Last edited on
Im still going to suggest a current pointer. Then in your insert method
current = current->prev;

If ListElement *nth = root->Nth(3); is in main and you cant change it, maybe
it should be root = root->prev; in insert.
I've decided to implement a current after all, I know it's a bit hackish, but it serves my purposes, I considered swapping the nodes data, but the output wouldn't be suitable.

At the moment, I've implemented my remove node list, which works well in combination with the use of a current pointer, however I now need to actually delete the node.

My remove function looks like this:

1
2
3
4
5
		assert(mCurrent->Previous() != NULL);
		mCurrent->mPrev->mNext = mCurrent->mNext;

		assert(mCurrent->Next() != NULL);
		mCurrent->mNext->mPrev = mCurrent->mPrev;


Now that the node has been removed from the list, what sort of example would I use in my destructor to clean it up correctly when delete is used in main?

***EDIT***

Just to be a bit more specific, the said node has been allocated with new.
Last edited on
Are you resetting mCurrent? I'd use a temp Node

in remove
1
2
3
4
5
ListElement* temp = mCurrent->mPrev;//or mNext depending on how you want it to function
temp->next = current->mNext;
mCurrent->mNext->mPrev = temp;
delete mCurrent;
mCurrent = temp; 


In destructor I'd iterate through the nodes calling remove then delete any other
allocated memory, ie first and last if necessary.
Last edited on
Yeah, I'm going to iterate through all the nodes in a function I'm going to make to remove an entire list, my particular query was what code should I put in the destructor if I have a function that will perform those duties and a separate remove function?
Last edited on
Pages: 123