By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,368 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,368 IT Pros & Developers. It's quick & easy.

Double-Linked Lists

P: n/a
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?

Dec 9 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

Dec 9 '05 #2

P: n/a
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

Dec 9 '05 #3

P: n/a

"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
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;
}
Dec 9 '05 #4

P: n/a
deanfamily wrote:
"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
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) ;
Dec 9 '05 #5

P: n/a
"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
deanfamily wrote:
"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
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?
Dec 9 '05 #6

P: n/a
deanfamily wrote:
"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
deanfamily wrote:
"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:dn**********@news.Stanford.EDU...
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
Dec 9 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.