473,786 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ 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 2244
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>::e rase() 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<>::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

Dec 9 '05 #3

"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn******** **@news.Stanfor d.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<>::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


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;
}
Dec 9 '05 #4
deanfamily wrote:
"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn******** **@news.Stanfor d.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
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

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;
}

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) ;
Dec 9 '05 #5
"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn******** **@news.Stanfor d.EDU...
deanfamily wrote:
"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn******** **@news.Stanfor d.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
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

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;
}

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) ;


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?
Dec 9 '05 #6
deanfamily wrote:
"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn******** **@news.Stanfor d.EDU...
deanfamily wrote:
"Alan Johnson" <al****@stanfor d.dot.nospam_ed u> wrote in message
news:dn***** *****@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;
}

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) ;

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?


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
9893
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 (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
20
17836
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
31
6651
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
10
8662
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 func. as one of the following:
10
18774
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
2884
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 delegate double funCallback(double x); public delegate double funcCallback(double x); public delegate void gradCallback(double x, double v); public delegate void gradcCallback(double x, double v);
67
9927
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 quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
1
8229
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(); ^ perimeter(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^ setSides(double,double) in Rectangle cannot be applied to (double)...
2
5597
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 1000 double loancalc (double *months, double *intrat, double *princ, double calcprnc, double calcpay, double calcbal, double calcnew, double calcInt); double display (double *months, double calcprnc, double calcpay, double calcbal, double...
2
5805
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 _granx1;
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.