Double-Linked Lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deanfamily

    #1

    Double-Linked Lists

    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?



  • Neelesh Bodas

    #2
    Re: Double-Linked Lists

    deanfamily wrote:[color=blue]
    > I am re-posting my second problem.
    >
    > I have a double-linked list.[/color]

    Assuming you mean std::list (in context with the remaining part of the
    message where you talk about remove() ) -
    [color=blue]
    >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?[/color]

    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>::e rase() to remove the same.

    Comment

    • Alan Johnson

      #3
      Re: Double-Linked Lists

      deanfamily wrote:[color=blue]
      > 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?
      >
      >
      >[/color]

      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<>::re move()?


      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(numbe rs.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

      Comment

      • deanfamily

        #4
        Re: Double-Linked Lists


        "Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
        news:dnb2uq$nae $1@news.Stanfor d.EDU...[color=blue]
        > deanfamily wrote:[color=green]
        >> 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?
        >>
        >>
        >>[/color]
        >
        > 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<>::re move()?
        >
        >
        > 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(numbe rs.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
        >[/color]

        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_iterato r<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_f ront(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_f ront(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.b egin(), mainList.end(), screen);

        return 0;
        }


        Comment

        • Alan Johnson

          #5
          Re: Double-Linked Lists

          deanfamily wrote:[color=blue]
          > "Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
          > news:dnb2uq$nae $1@news.Stanfor d.EDU...
          >[color=green]
          >>deanfamily wrote:
          >>[color=darkred]
          >>>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?
          >>>
          >>>
          >>>[/color]
          >>
          >>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
          >>intelligent ly 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<>::re move()?
          >>
          >>
          >>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(numbe rs.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
          >>[/color]
          >
          >
          > 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_iterato r<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_f ront(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_f ront(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.b egin(), mainList.end(), screen);
          >
          > return 0;
          > }
          >
          >[/color]


          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>::iter ator i = find(mainList.b egin(), mainList.end(), value) ;
          if (i != mainList.end()) mainList.erase( i) ;

          Comment

          • deanfamily

            #6
            Re: Double-Linked Lists

            "Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
            news:dnb5in$pkn $1@news.Stanfor d.EDU...[color=blue]
            > deanfamily wrote:[color=green]
            >> "Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
            >> news:dnb2uq$nae $1@news.Stanfor d.EDU...
            >>[color=darkred]
            >>>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
            >>>intelligentl y 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<>::re move()?
            >>>
            >>>
            >>>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<in t>::iterator i = std::find(numbe rs.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
            >>>[/color]
            >>
            >>
            >> 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_iterato r<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_f ront(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_f ront(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.b egin(), mainList.end(), screen);
            >>
            >> return 0;
            >> }[/color]
            >
            >
            > 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>::iter ator i = find(mainList.b egin(), mainList.end(), value) ;
            > if (i != mainList.end()) mainList.erase( i) ;[/color]

            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.b egin(), 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>::iter ator 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?


            Comment

            • Alan Johnson

              #7
              Re: Double-Linked Lists

              deanfamily wrote:[color=blue]
              > "Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
              > news:dnb5in$pkn $1@news.Stanfor d.EDU...
              >[color=green]
              >>deanfamily wrote:
              >>[color=darkred]
              >>>"Alan Johnson" <alanwj@stanfor d.dot.nospam_ed u> wrote in message
              >>>news:dnb2uq$ nae$1@news.Stan ford.EDU...
              >>>
              >>>
              >>>>deanfamil y 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
              >>>>intelligent ly 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<>::re move()?
              >>>>
              >>>>
              >>>>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<i nt>::iterator i = std::find(numbe rs.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_iterato r<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_f ront(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_f ront(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.b egin(), mainList.end(), screen);
              >>>
              >>> return 0;
              >>>}[/color]
              >>
              >>
              >>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>::it erator i = find(mainList.b egin(), mainList.end(), value) ;
              >>if (i != mainList.end()) mainList.erase( i) ;[/color]
              >
              >
              > 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.b egin(), 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>::iter ator 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?
              >
              >[/color]

              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

              Comment

              Working...