Double-Linked Lists

Discussion in 'C++' started by deanfamily, Dec 9, 2005.

  1. deanfamily

    deanfamily Guest

    I am re-posting my second problem.

    I have a double-linked list. I need to know if it is possible to remove just
    one of an item, instead of all that match the given criteria with the
    remove() command. Any thoughts?
    deanfamily, Dec 9, 2005
    #1
    1. Advertising

  2. deanfamily wrote:
    > I am re-posting my second problem.
    >
    > I have a double-linked list.


    Assuming you mean std::list (in context with the remaining part of the
    message where you talk about remove() ) -

    >I need to know if it is possible to remove just
    > one of an item, instead of all that match the given criteria with the
    > remove() command. Any thoughts?


    Not with remove command, but possible with "find()" and then "erase()".

    If you want to remove the "first " occurance of the item :

    1. use std::find() to find the first occurance of the item
    2. use std::list<T>::erase() to remove the same.
    Neelesh Bodas, Dec 9, 2005
    #2
    1. Advertising

  3. deanfamily

    Alan Johnson Guest

    deanfamily wrote:
    > I am re-posting my second problem.
    >
    > I have a double-linked list. I need to know if it is possible to remove just
    > one of an item, instead of all that match the given criteria with the
    > remove() command. Any thoughts?
    >
    >
    >


    I think that you are not getting many replies because you are not being
    very specific. What is a "double-linked list" to you? We all know what
    that is, but what does your specific implementation look like? Are you
    using std::list? Are you using some list class that you rolled on your
    own? If so, give enough detail about it that we can converse
    intelligently and write code that doesn't make assumptions about what we
    are working with.

    What do you mean by "the remove() command"? Do you mean std::remove()?
    Do you mean std::list<>::remove()?


    If I wanted to remove the first item from a list that matched a
    criteria, I would use a command like the following.

    std::list<int>::iterator i = std::find(numbers.begin(), numbers.end(),
    value) ;
    if (i != number.end()) numbers.erase(i) ;

    Notice how many assumptions I've just made. I've assumed you are using
    std::list. I've assumed that you are storing a list of type int. I've
    assumed your list is called numbers, and a variable called value
    containes the value you want to remove. It is very unlikely that all of
    my assumptions are true. If some of the more fundamental ones (like you
    using std::list) are not true, then my answer can barely even apply to
    your situation.

    The more specific you are with your questions, the more likely you are
    to get help.

    Alan
    Alan Johnson, Dec 9, 2005
    #3
  4. deanfamily

    deanfamily Guest

    "Alan Johnson" <_edu> wrote in message
    news:dnb2uq$nae$...
    > deanfamily wrote:
    >> I am re-posting my second problem.
    >>
    >> I have a double-linked list. I need to know if it is possible to remove
    >> just
    >> one of an item, instead of all that match the given criteria with the
    >> remove() command. Any thoughts?
    >>
    >>
    >>

    >
    > I think that you are not getting many replies because you are not being
    > very specific. What is a "double-linked list" to you? We all know what
    > that is, but what does your specific implementation look like? Are you
    > using std::list? Are you using some list class that you rolled on your
    > own? If so, give enough detail about it that we can converse
    > intelligently and write code that doesn't make assumptions about what we
    > are working with.
    >
    > What do you mean by "the remove() command"? Do you mean std::remove()? Do
    > you mean std::list<>::remove()?
    >
    >
    > If I wanted to remove the first item from a list that matched a criteria,
    > I would use a command like the following.
    >
    > std::list<int>::iterator i = std::find(numbers.begin(), numbers.end(),
    > value) ;
    > if (i != number.end()) numbers.erase(i) ;
    >
    > Notice how many assumptions I've just made. I've assumed you are using
    > std::list. I've assumed that you are storing a list of type int. I've
    > assumed your list is called numbers, and a variable called value containes
    > the value you want to remove. It is very unlikely that all of my
    > assumptions are true. If some of the more fundamental ones (like you
    > using std::list) are not true, then my answer can barely even apply to
    > your situation.
    >
    > The more specific you are with your questions, the more likely you are to
    > get help.
    >
    > Alan
    >


    Here a bit more detail, meaning the code of my list, I hope it helps:

    #include <list>
    #include <iostream>
    #include <time.h> //used for seeding the rand() function

    using namespace std;

    int main()
    {
    int num;
    list<int> mainList;
    ostream_iterator<int> screen(cout, " ");

    cout << "This program will first generate a list of random numbers between
    1 ";
    cout << "and 10,000." << endl;
    cout << "Then, it will generate a new list, and if the number is found ";
    cout << "in the list, it will be deleted." << endl << endl;

    //seed the random number generator
    srand (time(NULL));

    //enter the first number
    num = rand() % 10000; //generate a number
    mainList.push_front(num); //enter the number

    //will loop until the size of the list reaches the number
    while (mainList.size() <= 10)
    {
    //enter the next number
    num = rand() % 10000; //generate a number
    mainList.push_front(num); //enter the number
    mainList.sort(); //sort the list after inserting the number
    }

    //display the list of numbers unaltered
    cout << "Here is the list of numbers before deleting any:" << endl;
    copy(mainList.begin(), mainList.end(), screen);

    return 0;
    }
    deanfamily, Dec 9, 2005
    #4
  5. deanfamily

    Alan Johnson Guest

    deanfamily wrote:
    > "Alan Johnson" <_edu> wrote in message
    > news:dnb2uq$nae$...
    >
    >>deanfamily wrote:
    >>
    >>>I am re-posting my second problem.
    >>>
    >>>I have a double-linked list. I need to know if it is possible to remove
    >>>just
    >>>one of an item, instead of all that match the given criteria with the
    >>>remove() command. Any thoughts?
    >>>
    >>>
    >>>

    >>
    >>I think that you are not getting many replies because you are not being
    >>very specific. What is a "double-linked list" to you? We all know what
    >>that is, but what does your specific implementation look like? Are you
    >>using std::list? Are you using some list class that you rolled on your
    >>own? If so, give enough detail about it that we can converse
    >>intelligently and write code that doesn't make assumptions about what we
    >>are working with.
    >>
    >>What do you mean by "the remove() command"? Do you mean std::remove()? Do
    >>you mean std::list<>::remove()?
    >>
    >>
    >>If I wanted to remove the first item from a list that matched a criteria,
    >>I would use a command like the following.
    >>
    >>std::list<int>::iterator i = std::find(numbers.begin(), numbers.end(),
    >>value) ;
    >>if (i != number.end()) numbers.erase(i) ;
    >>
    >>Notice how many assumptions I've just made. I've assumed you are using
    >>std::list. I've assumed that you are storing a list of type int. I've
    >>assumed your list is called numbers, and a variable called value containes
    >>the value you want to remove. It is very unlikely that all of my
    >>assumptions are true. If some of the more fundamental ones (like you
    >>using std::list) are not true, then my answer can barely even apply to
    >>your situation.
    >>
    >>The more specific you are with your questions, the more likely you are to
    >>get help.
    >>
    >>Alan
    >>

    >
    >
    > Here a bit more detail, meaning the code of my list, I hope it helps:
    >
    > #include <list>
    > #include <iostream>
    > #include <time.h> //used for seeding the rand() function
    >
    > using namespace std;
    >
    > int main()
    > {
    > int num;
    > list<int> mainList;
    > ostream_iterator<int> screen(cout, " ");
    >
    > cout << "This program will first generate a list of random numbers between
    > 1 ";
    > cout << "and 10,000." << endl;
    > cout << "Then, it will generate a new list, and if the number is found ";
    > cout << "in the list, it will be deleted." << endl << endl;
    >
    > //seed the random number generator
    > srand (time(NULL));
    >
    > //enter the first number
    > num = rand() % 10000; //generate a number
    > mainList.push_front(num); //enter the number
    >
    > //will loop until the size of the list reaches the number
    > while (mainList.size() <= 10)
    > {
    > //enter the next number
    > num = rand() % 10000; //generate a number
    > mainList.push_front(num); //enter the number
    > mainList.sort(); //sort the list after inserting the number
    > }
    >
    > //display the list of numbers unaltered
    > cout << "Here is the list of numbers before deleting any:" << endl;
    > copy(mainList.begin(), mainList.end(), screen);
    >
    > return 0;
    > }
    >
    >



    Most of the important assumptions I made when answering turned out to be
    correct. You should be able to remove the first occurence of some value
    with:

    #include <algorithm> // Included for find.

    ....

    list<int>::iterator i = find(mainList.begin(), mainList.end(), value) ;
    if (i != mainList.end()) mainList.erase(i) ;
    Alan Johnson, Dec 9, 2005
    #5
  6. deanfamily

    deanfamily Guest

    "Alan Johnson" <_edu> wrote in message
    news:dnb5in$pkn$...
    > deanfamily wrote:
    >> "Alan Johnson" <_edu> wrote in message
    >> news:dnb2uq$nae$...
    >>
    >>>deanfamily wrote:
    >>>
    >>>>I am re-posting my second problem.
    >>>>
    >>>>I have a double-linked list. I need to know if it is possible to remove
    >>>>just
    >>>>one of an item, instead of all that match the given criteria with the
    >>>>remove() command. Any thoughts?
    >>>>
    >>>>
    >>>>
    >>>
    >>>I think that you are not getting many replies because you are not being
    >>>very specific. What is a "double-linked list" to you? We all know what
    >>>that is, but what does your specific implementation look like? Are you
    >>>using std::list? Are you using some list class that you rolled on your
    >>>own? If so, give enough detail about it that we can converse
    >>>intelligently and write code that doesn't make assumptions about what we
    >>>are working with.
    >>>
    >>>What do you mean by "the remove() command"? Do you mean std::remove()?
    >>>Do you mean std::list<>::remove()?
    >>>
    >>>
    >>>If I wanted to remove the first item from a list that matched a criteria,
    >>>I would use a command like the following.
    >>>
    >>>std::list<int>::iterator i = std::find(numbers.begin(), numbers.end(),
    >>>value) ;
    >>>if (i != number.end()) numbers.erase(i) ;
    >>>
    >>>Notice how many assumptions I've just made. I've assumed you are using
    >>>std::list. I've assumed that you are storing a list of type int. I've
    >>>assumed your list is called numbers, and a variable called value
    >>>containes the value you want to remove. It is very unlikely that all of
    >>>my assumptions are true. If some of the more fundamental ones (like you
    >>>using std::list) are not true, then my answer can barely even apply to
    >>>your situation.
    >>>
    >>>The more specific you are with your questions, the more likely you are to
    >>>get help.
    >>>
    >>>Alan
    >>>

    >>
    >>
    >> Here a bit more detail, meaning the code of my list, I hope it helps:
    >>
    >> #include <list>
    >> #include <iostream>
    >> #include <time.h> //used for seeding the rand() function
    >>
    >> using namespace std;
    >>
    >> int main()
    >> {
    >> int num;
    >> list<int> mainList;
    >> ostream_iterator<int> screen(cout, " ");
    >>
    >> cout << "This program will first generate a list of random numbers
    >> between 1 ";
    >> cout << "and 10,000." << endl;
    >> cout << "Then, it will generate a new list, and if the number is found
    >> ";
    >> cout << "in the list, it will be deleted." << endl << endl;
    >>
    >> //seed the random number generator
    >> srand (time(NULL));
    >>
    >> //enter the first number
    >> num = rand() % 10000; //generate a number
    >> mainList.push_front(num); //enter the number
    >>
    >> //will loop until the size of the list reaches the number
    >> while (mainList.size() <= 10)
    >> {
    >> //enter the next number
    >> num = rand() % 10000; //generate a number
    >> mainList.push_front(num); //enter the number
    >> mainList.sort(); //sort the list after inserting the number
    >> }
    >>
    >> //display the list of numbers unaltered
    >> cout << "Here is the list of numbers before deleting any:" << endl;
    >> copy(mainList.begin(), mainList.end(), screen);
    >>
    >> return 0;
    >> }

    >
    >
    > Most of the important assumptions I made when answering turned out to be
    > correct. You should be able to remove the first occurence of some value
    > with:
    >
    > #include <algorithm> // Included for find.
    >
    > ...
    >
    > list<int>::iterator i = find(mainList.begin(), mainList.end(), value) ;
    > if (i != mainList.end()) mainList.erase(i) ;


    I did my best recreating what you wrote in a for loop (so I could run it
    several times). However, upon compile I get several different errors. My
    compiler isn't very forthcoming with telling me exaclty what is wrong, so
    here is what I put in:

    for (int counter = 0; counter <= mainList.size(); counter++)
    {
    num = rand() % 10000; //generate a number

    value = find(mainList.begin(), mainList.end(), num);
    if (num != mainList.end())
    mainList.erase(value);
    }

    I also declared value (in the area I declared the variables) like this:
    list<int>::iterator value;

    So basically, the main changes I made to the code you suggest are changing i
    to value and value to sum. From what I gather from the errors, the
    compiler doesn't like how I declared the iterator. Any thoughts?
    deanfamily, Dec 9, 2005
    #6
  7. deanfamily

    Alan Johnson Guest

    deanfamily wrote:
    > "Alan Johnson" <_edu> wrote in message
    > news:dnb5in$pkn$...
    >
    >>deanfamily wrote:
    >>
    >>>"Alan Johnson" <_edu> wrote in message
    >>>news:dnb2uq$nae$...
    >>>
    >>>
    >>>>deanfamily wrote:
    >>>>
    >>>>
    >>>>>I am re-posting my second problem.
    >>>>>
    >>>>>I have a double-linked list. I need to know if it is possible to remove
    >>>>>just
    >>>>>one of an item, instead of all that match the given criteria with the
    >>>>>remove() command. Any thoughts?
    >>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>I think that you are not getting many replies because you are not being
    >>>>very specific. What is a "double-linked list" to you? We all know what
    >>>>that is, but what does your specific implementation look like? Are you
    >>>>using std::list? Are you using some list class that you rolled on your
    >>>>own? If so, give enough detail about it that we can converse
    >>>>intelligently and write code that doesn't make assumptions about what we
    >>>>are working with.
    >>>>
    >>>>What do you mean by "the remove() command"? Do you mean std::remove()?
    >>>>Do you mean std::list<>::remove()?
    >>>>
    >>>>
    >>>>If I wanted to remove the first item from a list that matched a criteria,
    >>>>I would use a command like the following.
    >>>>
    >>>>std::list<int>::iterator i = std::find(numbers.begin(), numbers.end(),
    >>>>value) ;
    >>>>if (i != number.end()) numbers.erase(i) ;
    >>>>
    >>>>Notice how many assumptions I've just made. I've assumed you are using
    >>>>std::list. I've assumed that you are storing a list of type int. I've
    >>>>assumed your list is called numbers, and a variable called value
    >>>>containes the value you want to remove. It is very unlikely that all of
    >>>>my assumptions are true. If some of the more fundamental ones (like you
    >>>>using std::list) are not true, then my answer can barely even apply to
    >>>>your situation.
    >>>>
    >>>>The more specific you are with your questions, the more likely you are to
    >>>>get help.
    >>>>
    >>>>Alan
    >>>>
    >>>
    >>>
    >>>Here a bit more detail, meaning the code of my list, I hope it helps:
    >>>
    >>>#include <list>
    >>>#include <iostream>
    >>>#include <time.h> //used for seeding the rand() function
    >>>
    >>>using namespace std;
    >>>
    >>>int main()
    >>>{
    >>> int num;
    >>> list<int> mainList;
    >>> ostream_iterator<int> screen(cout, " ");
    >>>
    >>> cout << "This program will first generate a list of random numbers
    >>>between 1 ";
    >>> cout << "and 10,000." << endl;
    >>> cout << "Then, it will generate a new list, and if the number is found
    >>>";
    >>> cout << "in the list, it will be deleted." << endl << endl;
    >>>
    >>> //seed the random number generator
    >>> srand (time(NULL));
    >>>
    >>> //enter the first number
    >>> num = rand() % 10000; //generate a number
    >>> mainList.push_front(num); //enter the number
    >>>
    >>> //will loop until the size of the list reaches the number
    >>> while (mainList.size() <= 10)
    >>> {
    >>> //enter the next number
    >>> num = rand() % 10000; //generate a number
    >>> mainList.push_front(num); //enter the number
    >>> mainList.sort(); //sort the list after inserting the number
    >>> }
    >>>
    >>> //display the list of numbers unaltered
    >>> cout << "Here is the list of numbers before deleting any:" << endl;
    >>> copy(mainList.begin(), mainList.end(), screen);
    >>>
    >>> return 0;
    >>>}

    >>
    >>
    >>Most of the important assumptions I made when answering turned out to be
    >>correct. You should be able to remove the first occurence of some value
    >>with:
    >>
    >>#include <algorithm> // Included for find.
    >>
    >>...
    >>
    >>list<int>::iterator i = find(mainList.begin(), mainList.end(), value) ;
    >>if (i != mainList.end()) mainList.erase(i) ;

    >
    >
    > I did my best recreating what you wrote in a for loop (so I could run it
    > several times). However, upon compile I get several different errors. My
    > compiler isn't very forthcoming with telling me exaclty what is wrong, so
    > here is what I put in:
    >
    > for (int counter = 0; counter <= mainList.size(); counter++)
    > {
    > num = rand() % 10000; //generate a number
    >
    > value = find(mainList.begin(), mainList.end(), num);
    > if (num != mainList.end())
    > mainList.erase(value);
    > }
    >
    > I also declared value (in the area I declared the variables) like this:
    > list<int>::iterator value;
    >
    > So basically, the main changes I made to the code you suggest are changing i
    > to value and value to sum. From what I gather from the errors, the
    > compiler doesn't like how I declared the iterator. Any thoughts?
    >
    >


    I can't tell precisely what the goal of that loop is. What it looks
    like you are trying to do is just randomly remove a few items from the
    list. What is your goal? What do you expect the list to look like when
    you are done?

    In any case, I can point out at least one syntax error, and one "style"
    error. Find returns an iterator to the first occurance of whatever it
    is you are looking for, and returns an iterator to the "end" of the list
    if it doesn't find anything. If you aren't familiar with iterators, you
    can think of them for the time being as sort of serving the same purpose
    as a pointer, in that it in some way refers to a list element. Anyway,
    the following line contains an error:

    if (num != mainList.end())

    Here you are comparing something of type int (I assume) to something of
    type std::list<int>::iterator. That comparison doesn't have any
    meaningful interpretation. Probably what you intended was:

    if (value != mainList.end())

    That is, you are comparing the iterator returned by find to the end of
    the list to make sure it actually found something.

    The "style" error is simply that I think "value" is an inappropriate
    name for the iterator, as that variable doesn't represent the value.

    Alan
    Alan Johnson, Dec 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    464
    emerth
    Jul 10, 2003
  2. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
  3. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    454
    emerth
    Jul 10, 2003
  4. Little

    Multiple Double Linked Lists

    Little, Jan 26, 2006, in forum: C Programming
    Replies:
    3
    Views:
    336
    James Dow Allen
    Jan 27, 2006
  5. jawdoc
    Replies:
    9
    Views:
    736
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page