Linked list deleting nodes


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void Que::search(QueueNode ptr*, string str)
{
    QueNode *temp = front;
    if(ptr = nullptr)
    {
        cout<< str<< "not found"<<endl;
    }
    else if(prt->nameValue == str)
    {
        temp = temp->next;
        front = ptr;
    }
    else
    {
        search(ptr->next, str);
    }
}


Before search queue is displayed as:

James
John
Jimmy


After Search
Name searched: Jimmy
If search is matched, output should be:

Jimmy
James
John


Actual display when search is called

Jimmy
Last edited on
...
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.
Last edited on
Actual display when search is called
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.
Topic archived. No new replies allowed.