Infinity08:
Mind giving me an example of all that?
Thanks! :o)
David
Main Topics
Browse All TopicsHey all i am in need of some help with getting some nodes deleted. This is the situation:
Right now it only deletes the 1 string i pass to it "deleteItem". However, i wish to delete the next 8 blocks after the "deleteItem".
Say i have this in the list right now.
bob barker is pretty old but still seems to get all the ladies
Now the user would pick "bob" and the code would delete "bob" but leave everything else. I wish to take out "bob, barker, is, pretty, old, but, still, seems, to".
So....
bob <delete>
move to next node..
barker <delete>
move to next node..
is <delete>
...etc etc.
I have attached the 3 txt files of this project along with the code below that is responsible for deleting the node string.
I'm unsure on how to go about doing that. Any help would be great!
David
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Which part are you confused about ?
Your current code seems to correctly delete one node from the linked list. You can repeat this deletion process 8 times (in a loop) to delete 8 consecutive nodes.
Keep trailCurrent pointing to the node before those 8, and use current to move over the 8 nodes, and delete them along the way. At the end, all you have to do is update the next pointer for the trailCurrent node (just like you're already doing), and possibly update the last pointer.
Instead of a loop you also could rename your deleteNode into deleteItem and have a new member function deleteNode which can be called recursively:
pseudocode:
deleteItem(item)
{
NodePtr find = findNode(item)
if find is not null
deleteNode(find)
else
output "empty list"
}
The findNode would iterate from First until "item" was found. The the current node was returned. If not found NULL was returned.
The deleteNode would check whether the next node is not NULL and if so would call deleteNode passing the next. After the call it would delete the node passed.
That way "Bob" would call deleteNode for "Barker", "Barker" would call deleteNode for "is" and so on and after the whole branch after was deleted, it would delete the node itself.
Ok.
I suggest making a drawing of a linked list (each node represented by a box, with arrows between the representing the links).
Then, re-arrange the arrows repeatedly to remove one node at a time from the linked list. Once you understand how it works on paper, you can implement it.
deleting one node from a linked list goes something like this (where 'node' points to the node just before the one we want to delete) :
tmp = node->next; // tmp now points to the node that has to be deleted
node->next = node->next->next; // we bypass the node that will be deleted, and point to the one after that
delete tmp; // and the node is deleted
To delete multiple nodes, these same three lines are just repeated as needed.
And then to finish it all off, you have to take into account the special cases (like the start and the end of the linked list).
>>>> error C2039: 'next' : is not a member of 'nodeType<Type>' with [Type=std::string]
The 'next' member is called 'link' in your node template class nodeType.
So replace
tmp = first->next;
first->next = first->next->next;
delete tmp;
by
tmp = first->link;
first->link = first->link->link;
delete tmp;
A tip: if ever you got such an error message from the compiler: take it literally.
The message above clearly said 'next' is NOT a member of nodeType what is true as the member name is 'link'.
Great! It works now :o) But this time it skips the previous one and just takes the one that is entered and then however many i deside to delete thereafter.
I tried using:
tmp = current->link;
first->link = first->link->link;
delete tmp;
and
tmp = trailCurrent;
first->link = first->link->link;
delete tmp;
but did nothing.
>>>> tmp = current->link;
>>>> first->link = first->link->link;
>>>> delete tmp;
The sequence above is for deleting one single node in the chain and chaining (linking) the previous with the next, e. g. if you have a list with 6 nodes
RN -> N1 -> N2 -> N3 -> N4 -> N5 -> 0
and current is N2, then the above code would unlink the N3 and delete it and would link N2 to N4.
RN -> N1 -> N2 -> N4 -> N5 -> 0
So, if you would like to delete N4 and N5 as well you would need to keep the N2 as current and repeat the above sequence until current->link is NULL.
But actually the deleteNode rarely will/should delete more than one node but only one node. You would iterate from root until you find the node you want to delete, then make the previous of the found node the current (deleting the root node would be a special case to handle) and apply the above statements. You best safe the 'previous' node with each iteration so that the pointer variable already is filled when you found the node to delete.
I thought thats what i was doing with this code, itsmeandnobodyelse:
tmp = trailCurrent;
first->link = first->link->link;
delete tmp;
But i get an error of:
Unhandled exception at 0x0041b6d3 in linkedlists.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
and thats on the line of the next node:
tmp = first->link;
first->link = first->link->link; // <-error on this line
delete tmp;
Right now i have "bob, barker, 1, 2, 3, 4, 5, 6, 7, 8" and when i type in barker it shows this:
bob, 6, 7, 8
However, when the user types in barker, i need it to take away bob as well. Thats where i am stuck and thought the code above was taking the previous node (bob) and deleting that as well..
David
>>>> Access violation reading location 0xfeeeff0a.
Hmm. You are dealing with tmp and trailCurrent but the delete statements operate with first and first->link. If any of those isn't valid it makes kaboom.
Pointers need carefulness. Always check a pointer to be valid before accessing it.
if (first != NULL && first->link != NULL)
{
// now the statement is safe
first->link = first->link->link;
}
Can you tell what trailCurrent is? And why you use 'first' instead of 'previous' as in the code I posted?
Could we go back to http:#25660584, and start again from there, Stealthrt ? Your code worked at that point, except that it didn't delete the right set of nodes. So, all you had to do, is modify your code there to start deleting at a different node ...
Try that again, and post your modified code here if it doesn't work.
To add to above comment.
In order that the following code should not crash
>>>> tmp = current->link;
>>>> current->link = current->link->link;
>>>> delete tmp;
the linked list must contain at least two valid nodes: current and current->link
The situation where a list has only one node left must be handled seperately.
>> trailCurrent = first;
>> current = first->link;
What if the item to be deleted is in the first node ?
>> trailCurrent = current;
>> //current = current-> link;
Why did you comment that second line ?
>> }else
>> {
>> found = true;
>> }
I think you're missing a closing } ... Properly indenting your code will help avoid this kind of mistake.
>> current = first;
>> first = first->link;
>> trailCurrent = first;
Why are you starting over ? You already found the first node that needs to be deleted.
The item will never be the first node. It will go by last name and last name is going to be the second node. I took out the comment slashes and i get this now:
bob
barker
1
2
3
4
5
6
7
8
Enter the string to be deleted: barker
After deleting barker
1
6
7
8
Press any key to continue . . .
notice that it now deletes "bob" but leave "1" now.. gerr! This is so crazy. What do i need to change this:
tmp = first->link;
first->link = first->link->link;
delete tmp;
in order for it to delete the 1 as well?
David
>> Why are you starting over ? You already found the first node that needs to be deleted.
Just think about this question ^
The first while loop was there to find the first node to be deleted.
Once you found that node (after the while loop), you need to start deleting the amount of nodes you want, starting with that node.
>> Ok. This still brings an error:
Don't just guess. Think about it.
What effect do you think this line has :
>> trailCurrent->link = trailCurrent->link;
? None at all.
You want to delete the node that is pointed to by 'current'.
And you want trailCurrent->link to point to the node after the 'current' node.
Do you understand why ? Do you understand how deleting a node in a linked list works ?
Take a look at this image, and that will hopefully make things clearer :
http://en.wikipedia.org/wi
>> Yes it makes since but like i said, i cant seem to code it like that.
Why not ?
Remember these three lines I posted earlier :
>> tmp = node->next; // tmp now points to the node that has to be deleted
>> node->next = node->next->next; // we bypass the node that will be deleted, and point to the one after that
>> delete tmp; // and the node is deleted
? If 'node' points to the node before the one that needs to be deleted, then after these 3 lines, that node will be deleted from the linked list.
So, how does that apply to 'trailCurrent' and 'current', if 'current' is the one you want to delete ? Refer to the image if it isn't clear.
>> current can not be used since it was already deleted....
What do you mean ? The first time, it's pointing to the node that needs to be deleted. After deleting the node, you make it point to the next one, so that it too can be deleted, etc.
>> tmp = current;
>> current->link = current->link->link;
>> delete tmp;
Ok. Again.
trailCurrent points to the node BEFORE the one that needs to be deleted.
current points to the node that needs to be deleted.
Then you need to execute some code that does a few things :
(a) trailCurrent->link needs to point to the node AFTER the one that needs to be deleted (we skip it)
(b) the node to be deleted needs to be deleted
(c) current now needs to point to the following node that needs to be deleted
Repeat as necessary.
I dont see how trailCurrent is used after it has been deleted by current
trailCurrent->link = current->link;
count--;
if (last == current)
{
last = trailCurrent;
}
current = first;
first = first->link;
trailCurrent = first;
if (first == NULL)
last = NULL;
delete current;
tmp = current;
current->link = current->link->link;
delete tmp;
since trailCurrent points right back to the first node..
David
You probably should go back to your initial code which seems to be ok beside that it deletes only one node (the node found) but not the requested number of following nodes.
Then you should add the number of nodes to delete after finding the initial node as an argument:
template <class Type>
void unorderedLinkedList<Type>::d
{
if (numNodesToDelete < 1) // nothing to do
{
cout << "numNodesToDelete should be 1 or greater" << endl;
return;
}
...
}
Then, you should use a little helper function which gets a node pointer as argument and the number of nodes to delete after and would return the node pointer the last of those nodes was linked to:
template <class Type>
nodeType<Type> * unorderedLinkedList<Type>::d
{
if (node == NULL) return NULL;
nodeType<Type> * next = node->link;
int count = 0;
// put here a do while loop that would first set the next to node->link
// and then delete the node and increment the counter
// the loop would stop either if next is NULL or if the counter reached the number of nodes to delete
do
{
...
}
while (...);
// finally return the next which points to the next node not deleted or NULL
return next;
}
The function above could be used both for "case 2" where the first node of list needs to get deleted and for the "else" case where one of the middle nodes was the initial node to be deleted.
In "case 2" the pointer returned from above function would become the new first (and last if list now is empty or has one node only) or would be the pointer the previous before the found node (trailCurrent) would link to.
>> I dont see how trailCurrent is used after it has been deleted by current
Ok. Drawing time ...
This is the linked list before the while loop :
----- ----- ----- ----- -----
first--->| 1 |--->| 2 |--->| 3 |--->| 4 |--->| 5 |--->NULL
----- ----- ----- ----- -----
We want to delete the node 3 (and those after it), so after the while loop, 'current' points to that node, and 'trailCurrent' points to the node before it :
----- ----- ----- ----- -----
first--->| 1 |--->| 2 |--->| 3 |--->| 4 |--->| 5 |--->NULL
----- ----- ----- ----- -----
/\ /\
| |
trailCurrent current
To delete node 3, we first "skip" it :
____________
/ \/
----- ----- ----- ----- -----
first--->| 1 |--->| 2 | | 3 |--->| 4 |--->| 5 |--->NULL
----- ----- ----- ----- -----
/\ /\
| |
trailCurrent current
Then we delete the node :
____________
/ \/
----- ----- ----- -----
first--->| 1 |--->| 2 | | 4 |--->| 5 |--->NULL
----- ----- NULL ----- -----
/\ /\
| |
trailCurrent current
'current' now no longer points to an existing node, so we let it point to the next node we want to delete :
____________
/ \/
----- ----- ----- -----
first--->| 1 |--->| 2 | | 4 |--->| 5 |--->NULL
----- ----- ----- -----
/\ /\
| |
trailCurrent current
So, this time we can delete node 4 in exactly the same way.
Go over these drawings and explanations, and please ask if anything is not clear. As long as you don't understand what's happening, and why, you will not be able to correctly implement this.
First understand it, then write the code.
Well using your examples Infinity08:
----- ----- ----- ----- -----
first--->| 1 |--->| 2 |--->| 3 |--->| 4 |--->| 5 |--->NULL
----- ----- ----- ----- -----
/\ /\
| |
trailCurrent current
Thats what i am trying to do. Where is current = "barker" and trailCurrent = "bob"
I need to start from "bob" and delete down the list til say, the 5th node. Right now, its skipping the node after "barker".
This is how i am reading this code:
found = false; // set to false
trailCurrent = first; // put "bob" into trailCurrent
current = first->link; // set current = to "barker"
while (current != NULL && !found) // make sure there is nodes
{
if (current->info != deleteItem) //check to see if current = "barker"
{
trailCurrent = current; // if so then set "barker" to trailCurrent
current = current-> link; // set current to whatever node is after "barker"
}else
{
found = true; // set it to true
}
if (found)
{
trailCurrent->link = current->link; // set trailCurrent = to current "baker"
count--; // count minus current count
if (last == current) // check to see if "barker" is the last in the node
{
last = trailCurrent; // if it is then set it to last
}
current = first; // I do not know what this does.. would this be "bob"?
first = first->link; // if its "bob" above then this must be "barker"
trailCurrent = first; // sets trailCurrent to "barker" if code above is correct
if (first == NULL) // check to see if first is null
last = NULL; // if so then last must be null
delete current; // delete "bob"
tmp = first->link; // set tmp to "barker"
first->link = first->link->link; // set first to whatever comes next after "barker"
delete tmp; // delete "barker"
etc etc...
David
>> I need to start from "bob" and delete down the list til say, the 5th node.
You're starting off all wrong.
If you want to start deleting from the first node, then that's a special case.
Either way, 'current' should point to the first node (because that's the first node you want to delete).
Instead of starting with a special case, try starting with the basic case - ie. the one I showed. Once you get that to work, you can work on the special cases (involving the start and end of the linked list).
>>>> Alright, i've been working on it and still am not getting anywhere.
What do you expect us to do?
Is it really so difficult to not deleting the *current* but the node after the current and link the current with the next after the node to delete?
current current->link current->link->link
[1] -> [2] -> [3]
deleting [2] ==>
[1] -> [3]
If you understand that, is it really so difficult to find out that in case you want to delete the very first node, you have a special situation as there is no current which can point to the node you want to delete.
root root->link root->link->link
[1] -> [2] -> [3]
So, when deleting [1] you will get a new root and have
[2] -> [3]
where [2] is the new root.
Can you just start over, and write new code ?
Create a linked list with 5 nodes, just like in my earlier post http:#25680543.
Then try deleting node 3, just like I showed.
Once that works, try expanding the code to delete both nodes 3 and 4.
Once that works, try the same with a bigger list, and try deleting 2 nodes starting from anywhere in the linked list (make sure to handle the start of the list as a special case).
Once that works, try the same, but this time let the user specify how many nodes need to be deleted starting from where.
Step by step, you'll get there. But start with the simple part.
Business Accounts
Answer for Membership
by: Infinity08Posted on 2009-10-19 at 15:22:03ID: 25609514
When you have found the target node ("bob" in your example), keep a pointer to the previous node - you'll need it later. Then start following the linked list, and delete every node you traverse, until you've deleted the amount you want (8 in your example). Now all you have to do is connect the node you kept a pointer to earlier with the next node (the node just after the last one that was deleted).