473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

when list is best

Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Paul Epstein

Sep 18 '06 #1
16 1789
pauldepstein wrote:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Is this for a quiz? Use list when random inserts and deletes must take
constant (minimal) time.

Use a vector if you have not yet characterized your performance needs (and
if you never will), because its most common actions (push, pop, iterate),
are all nearly constant time, with a minimal memory footprint.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Sep 18 '06 #2
pa**********@att.net wrote:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Any situation in which after the container is populated, there are
frequent insertions and deletions other than from either end. Suppose
you have a container of 50 elements. Suppose you want to delete the
twelfth element and replace it with 19 new elements. Suppose you want
to do this repeatedly, and the container changes in this manner
throughout its lifetime. vector and deque would both probably be
ill-suited to this task, whereas list would work fine.

Of course, the only way to know for sure would be to profile your code
and see whether it made a difference.

Best regards,

Tom

Sep 18 '06 #3
Thomas Tutone wrote:
Of course, the only way to know for sure would be to profile your code
and see whether it made a difference.
In general, only profiling will have the last word on performance.

In the specific case of algorithms dealing with large batches of elements,
you can also calculate the O*n performance formula, where O is the overhead
of each operation, and n is the number of elements. Some sorts, for example,
are O log n. That means n input elements cause log n operations, and the
overhead of each one is O, so O * log n is the total Overhead.

The STL templates help by providing assured O*n performance profiles for
their various operations.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Sep 18 '06 #4
In article <11*********************@m73g2000cwd.googlegroups. com>,
pa**********@att.net says...
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Yes and no. In theory, I can see it for something like a text editor,
where you have a current line in the file, and want to be able to
insert/delete lines quickly wherever that current line happens to be.
The 'current line' mostly gets changed by cursor control keys: back one
line, forward one line, back one screen, forward one screen, to the
beginning of the file, or the end of the file. All of those are
reasonable with a linked list (though the forward/backward one screen is
still linear on the number of lines in a screen).

OTOH, I can barely remember the last time I used a linked list in real
code, and I'd put good odds that even then it wasn't really optimum. I
doubt I've ever written real code for which it was the optimum data
structure.

A linked list is constant for insertion and deletion anywhere in the
list, but linear to get to any spot in the list. If you have some sort
of collection that requires linear searching anyway, that's not a real
loss -- but this is rare, at least IME. If you usually maintain a
"current spot" in the list, that's fine too. The linear search usually
loses more than the constant insertion/deletion gains -- especially
since linked lists have relatively poor locality of reference, as a
rule.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #5
In article <QC****************@newssvr12.news.prodigy.com>,
ph******@yahoo.com says...
pauldepstein wrote:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Is this for a quiz? Use list when random inserts and deletes must take
constant (minimal) time.
In that case, a list doesn't work. A random insertion or deletion in a
list is linear, because you have to traverse list to the random spot
(linearly) before you can do the insertion or deletion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #6

Phlip wrote:
Is this for a quiz?
No

Sep 18 '06 #7

<pa**********@att.netwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Paul Epstein
Consider a waiting list for liver transplant patients. A queue sounds like
a good first choice: first in, first out. But consider this:

Suppose you need a liver transplant. You may get placed on the list
according to how soon you're likely to need the transplant, not just at the
end because you're the latest to apply. That requires inserting you in a
specific order with in the list, something a list is good for, because it
doesn't have to shuffle everyone behind you backwards in the line. You just
get inserted in the appropriate place.

Suppose someone else waiting for a transplant dies before getting a
transplant. They need to be removed from the list. Again, that's easy with
a list.

Neither operation is easy (or at least efficient) with a vector or deque.

You might also want to merge your waiting list with one from another region.
The list has a merge operation for that. Or ,you may want to re-sort the
list, according to new regulations regarding patient priority. Again, list
provides that ability.

Basically, any situation which would normally call for a doubly-linked list,
the list container is likely to fit the bill best. (Unless you've got a
whiz-bang third-party container which is suited even better, of course! :-))

-Howard
Sep 18 '06 #8
pa**********@att.net wrote:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Paul Epstein
One important property for list is that an iterator to a list element
remains valid for all insertion/deletion operations except for deleting
the element the iterator is pointing to. For a deque or a vector, any
insertions or deletions can invalidate all iterators.
Sep 18 '06 #9
In article <wd*******************@bgtnsc05-news.ops.worldnet.att.net>,
al*****@hotmail.com says...

[ ... ]
Consider a waiting list for liver transplant patients. A queue sounds like
a good first choice: first in, first out. But consider this:
First of all, I'd argue with your basic premise. First of all, the
number of people in any given list for organ transplants (whether
hearts, livers, or whatever) is not likely to be large enough that
computation speed is likely to matter. Second, this sort of list tends
to be maintained over a matter of months or years so what you're
generally looking at is a database, not an in-memory data structure.

Even though I doubt any of the details really means much under the
specific circumstances, let's look at them for a moment anyway, in case
they were applied to different circumstances.
Suppose you need a liver transplant. You may get placed on the list
according to how soon you're likely to need the transplant, not just at the
end because you're the latest to apply. That requires inserting you in a
specific order with in the list, something a list is good for, because it
doesn't have to shuffle everyone behind you backwards in the line. You just
get inserted in the appropriate place.
All of these operations require that you find the right place in the
list before you can do the insertion or deletion -- and in a linked
list, that search will be linear.
Suppose someone else waiting for a transplant dies before getting a
transplant. They need to be removed from the list. Again, that's easy with
a list.
Maybe -- if the record for that person contains an iterator to the right
item in the list, it can be efficient. Otherwise it also requires a
(linear) search for the correct item in the list before the item can be
deleted.
Neither operation is easy (or at least efficient) with a vector or deque.
That depends on what you call "efficient". I'd agree that neither a
vector nor a deque is probably ideal for the situation, but they're
probably both going to be faster than a linked list anyway.

Under the circumstances, what you probably want is map, with the
priority as the key and a pointer to the patient's data as the
associated data. This allows your insertions and deletions to be
logarithmic instead of linear.
You might also want to merge your waiting list with one from another region.
The list has a merge operation for that. Or ,you may want to re-sort the
list, according to new regulations regarding patient priority. Again, list
provides that ability.
Both set and map allow merging to be done quickly as well. Changing
regulations takes long enough that optimizing for it is almost certain a
mistake. Nonetheless, it should be O(N lg N) for just about any data
structure you use.
Basically, any situation which would normally call for a doubly-linked list,
the list container is likely to fit the bill best. (Unless you've got a
whiz-bang third-party container which is suited even better, of course! :-))
Yes, but what situation does call for a doubly-linked list? So far, I've
seen few or none, and based on your example, I have to guess you haven't
either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #10
Jerry Coffin wrote:
....
>
Yes, but what situation does call for a doubly-linked list? So far, I've
seen few or none, and based on your example, I have to guess you haven't
either.
I have used list<>s for:

a) work queues - i.e. items of work that may be removed before being done.

b) message queues - i.e. a list of messages that need to be diced and
spliced.

c) client objects - i.e. each client of a service is an item in a list
Sep 19 '06 #11
Jerry Coffin <jc*****@taeus.comwrote:
Under the circumstances, what you probably want is map, with the
priority as the key and a pointer to the patient's data as the
associated data.
There is also std::priority_queue which may have been a better choice
for this example.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 19 '06 #12
In article <ee**********@news-int2.gatech.edu>,
ri******@gehennom.invalid says...
Jerry Coffin <jc*****@taeus.comwrote:
Under the circumstances, what you probably want is map, with the
priority as the key and a pointer to the patient's data as the
associated data.

There is also std::priority_queue which may have been a better choice
for this example.
I thought of that, and for all but one point, it would be the clear
choice -- priority_queue doesn't (directly) support removal from the
middle of the list. You could still do it, but not particularly cleanly
-- since priority_queue is an adapter, usually instantiated over dequeue
or vector, you can still remove from the middle of the underlying
container, and then re-build the priority_queue. It's comparitively ugly
though...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #13
In article <450fd5b5$0$13674$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, gi*******@mariani.ws says...
Jerry Coffin wrote:
...

Yes, but what situation does call for a doubly-linked list? So far, I've
seen few or none, and based on your example, I have to guess you haven't
either.

I have used list<>s for:

a) work queues - i.e. items of work that may be removed before being done.
Okay this might be reasonable -- but I'm left wondering: do really
remove unfinished items often enough to optimize for that at the expense
of virtually everything else?

Most times I build something like this, it ends up holding pointers to
some sort of structure that describes each work item. In that case, it's
usually perfectly reasonable to simply set the pointer to NULL, and not
worry about removing it until it gets to the head of the queue (of
course, the structure it points at can be destroyed immediately).
b) message queues - i.e. a list of messages that need to be diced and
spliced.

c) client objects - i.e. each client of a service is an item in a list
Without a bit more information, it's hard to say with certainty, but
nothing in your description of either of these seems to give a strong
indication that a list would be particularly optimum for them either.
I'm not saying they _couldn't_ be, but I don't see anything in your
description to indicate that they actually are.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #14
pa**********@att.net writes:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Mergesort can be implemented in-situ on lists, but not on deques and
vectors.

Not sure if this counts as a realistic example though.

Jens
Sep 19 '06 #15
Gianni Mariani wrote:
pa**********@att.net wrote:
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Paul Epstein

One important property for list is that an iterator to a list element
remains valid for all insertion/deletion operations except for deleting
the element the iterator is pointing to. For a deque or a vector, any
insertions or deletions can invalidate all iterators.
To me, this is perhaps the most compelling justification to use a list.
As is the case with iterators, pointers and references are also not
invalidated when inserting or deleting an inner element - in contrast
to vectors and deques.

Best regards,

Tom

Sep 19 '06 #16
Jens Theisen wrote:
pa**********@att.net writes:
>Can anyone give a realistic example of a task for which the list
container works better than vector or deque?

Mergesort can be implemented in-situ on lists, but not on deques and
vectors.

Not sure if this counts as a realistic example though.
It does. In fact, it generalizes quite a bit: with std::list<reordering
elements does not involve copying them around. That can translate into a
performance gain for objects that are costly to copy or move. [This is to
be taken with a grain of salt: many standard reordering procedures can be
expressed in terms of swap operations which usually can be implemented
efficiently even for big objects.]
Best

Kai-Uwe Bux
Sep 19 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
35
by: bonono | last post by:
Hi, I am wondering if there is such a thing, as python is moving away from FP functions like dropwhile/takewhile etc.
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
14
by: Ben | last post by:
I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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,...

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.