I have created a queue using a linked list as well as a recursive function that is supposed to search for a specific name in one of the nodes of the list. The search works fine, but after the match I want that node to move to the front of the list. As of right now, the node is moved to the front, but it deletes every node that comes after it. I also have a function which displays the queue. Before the search function is called, the queue displays fine. I'm still new to this so I hope this makes sense.
...
james -- front & ptr and all are here?
john
jimmy
is ptr null? no
is it a match for jimmy? no.
search recurse
john ptr is here
jimmy
is it null? no is it a match, no.. recurse
jimmy ptr is here.
is it null, no. is it a match? yes.
front = jimmy.
list is now jimmy, and holds nothing else.
temp seems to be a total waste of space, its not doing anything that I can tell.
-- recommendation: make search search. Make something else swap or shuffle the nodes.
-- recommendation: if you already have a delete node, search, if you found it, delete it, and after you delete it, insert on top. If you don't have a delete node, write one, and if you don't have an insert on top, write one (its the most efficient insert anyway).
--observation: if you don't care about the order of stuff, swap the data (not the list pointer structures) of the top node and the located node. If their order has meaning, you have to do something else.
Yes, this is bcause of line 11. You make the found item front.
I would not expect that search(...) modifies the Que itself.
I would expect that search returns a kind of path and an indicator that it actually found something. Which could be a vector of strings (or a separate/copy of Que) and a bool as pair.