Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,288 people online right now. Registration is fast and FREE... Join Now!




Templated Remove Function for Lists

 
Reply to this topicStart new topic

Templated Remove Function for Lists

Mythras
27 Feb, 2007 - 10:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Feb, 2007
Posts: 2


My Contributions
CODE

template <class T>
void remove_each (Node<T>* &head, T const& x)
{
    Node<T> *q = head;
    
    while (q->next) {
        if (head) {
            if (head->value == x) {
                                Node<T> *tempo = head;
                head = head->next;
                                delete temp;
            }
            else {
                while (q->next) {    
                    if (q->next->value == x) {
                        Node<T> *temp = q->next;
                        q->next = q->next->next;
                        delete temp;
                    }
                    else
                        q = q->next;
                }
            }
        }
        else {
            while (q->next) {    
            if (q->next->value == x) {
                    Node<T> *temp = q->next;
                    q->next = q->next->next;
                    delete temp;
                }
                else
                    q = q->next;
            }
        }
    }
}




I'm attempting to construct a single-linked list implementation class for a project and the above function takes head node and a certain value the user desires to remove from the list. As far as Visual Studio is concerned, it works for every case except when the first two elements are values to be removed. The code segment where the error occurs is the following:


CODE

if (q->next->value == x) {    //This line
        Node<T> *temp = q->next;          
        q->next = q->next->next;
        delete temp;
}


However, no error occurs if I allow a memory leak in the first check

CODE

if (head->value == x) {
                Node<T> *tempo = head;
        head = head->next;
                delete temp;
}


by leaving out the delete and having just the redirection of head.


The error I receive is "Access violation reading location ______" and while I know that it means it's reading past the available nodes, I cannot figure out how it does while passing the previous while loop test of while(q->next).

Any input would be appreciated.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Templated Remove Function For Lists
27 Feb, 2007 - 11:13 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
I think I see your problem... course this is just a first look but I see 'A problem' and that is in
CODE
            if (head->value == x)
            {
                Node<T> *tempo = head;
                head = head->next;
                delete temp;
                //Note that q still points to
                //the old value of head which
                //was just deleted...
            }
This bit of code does not reassign q before deleting head... going back to code to look some more
User is offlineProfile CardPM
+Quote Post

Mythras
RE: Templated Remove Function For Lists
28 Feb, 2007 - 09:18 AM
Post #3

New D.I.C Head
*

Joined: 27 Feb, 2007
Posts: 2


My Contributions
Thanks for the response; simply adding a redirect for q solved the problem completely.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month