473,463 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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?

Dec 9 '05 #1
6 2216
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
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

"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
3
by: BlueTrin | last post by:
I am using a DLL written in C, it uses some pointers on functions, I have defined a wrapper around it in C# which uses some delegates: #region Delegates and Marshalling to call solvopt public...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
1
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ...
2
by: dj10fld | last post by:
I am getting a (cannot convert double to double in assignment errors) here is a part of my code #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define MaxSize...
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.