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

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




Link List find and delete item

 
Reply to this topicStart new topic

Link List find and delete item

realNoName
11 Dec, 2006 - 04:08 PM
Post #1

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
i have been playing with linkedlist and i got most of work but haveing a problem with trying to find an item then delete it

This is what i have but when i call it it errors out
CODE

template<class T>
LinkList<T> & LinkList<T>::removeItem(T item)
{
    node * walker = start;
    node * backStep = start;
    if(walker->data == item)
    {
        start = walker->link;
        delete walker;
        size--;
    //    return *this;
    }
    while(walker != NULL)
    {
        if(walker->data == item)
        {
            backStep->link = walker->link;
            delete walker;
            size--;
    //        return *this;
        }
        backStep = walker;
        walker = walker->link;
    }
    return *this;
}

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Link List Find And Delete Item
12 Dec, 2006 - 12:42 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(realNoName @ 11 Dec, 2006 - 05:08 PM) *

i have been playing with linkedlist and i got most of work but haveing a problem with trying to find an item then delete it

This is what i have but when i call it it errors out
CODE

template<class T>
LinkList<T> & LinkList<T>::removeItem(T item)
{
    node * walker = start;
    node * backStep = start;
    if(walker->data == item)
    {
        start = walker->link;
        delete walker;
        size--;
    //    return *this;
    }
    while(walker != NULL)
    {
        if(walker->data == item)
        {
            backStep->link = walker->link;
            delete walker;
            size--;
    //        return *this;
        }
        backStep = walker;
        walker = walker->link;
    }
    return *this;
}



Hi

Thanks for the code. IN isolation and without the errors, its not enough to give you the bes response. Can you tell us the errors too?

thanks

User is offlineProfile CardPM
+Quote Post

realNoName
RE: Link List Find And Delete Item
12 Dec, 2006 - 10:49 AM
Post #3

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
errors out at this line
if(walker->data == item)

QUOTE
Unhandled exception at 0x004170cd in LinkedListClass.exe: 0xC0000005: Access violation reading location 0xdddddddd.



i was looking at the debug and for some reason it looks like walker does not have any values in it
QUOTE
- walker 0xdddddddd {data=??? link=??? } LinkList<char>::node *
data CXX0030: Error: expression cannot be evaluated char

User is offlineProfile CardPM
+Quote Post

czy1121
RE: Link List Find And Delete Item
13 Dec, 2006 - 01:58 AM
Post #4

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 9


My Contributions
when the walker has no point to a concrete node, you can access the data member.
CODE

    if(walker != NULL && walker->data == item)
    {
        start = walker->link;
        delete walker;
        size--;
    //    return *this;
    }


you should ensure the last node's link member is NULL.

User is offlineProfile CardPM
+Quote Post

realNoName
RE: Link List Find And Delete Item
13 Dec, 2006 - 11:46 AM
Post #5

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
In my add function i do make the last node link NULL .... and i was just playing around with it and found it works if i use the return *this but i dont want them there b/c i want it to delete every time that the item is in the list
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:35PM

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