Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linked List Isn't Working Correctly

Reply
Thread Tools

Linked List Isn't Working Correctly

 
 
darrell.blake@gmail.com
Guest
Posts: n/a
 
      12-09-2005
I've just written a doubly linked list but when I tell the iterator to
move forward in the list it removes all the previous elements in the
list. i.e. If I were to do:

List list;

list.AddAtEnd(20);
list.AddAtEnd(30);
list.AddAtEnd(40);
list.AddAtBeginning(10);

Iterator iterator = list.GetIterator();
iterator.MoveToStart();
iterator.MoveForward();

I would expect to still get an output of 10, 20, 30, 40 when I cycle
through the list but it only outputs 20, 30 40. Similarly, if I were to
call MoveForward() one more time it would only output 30, 40.

Because of the size of the files I wont post the code here but I have
provided URLs to my LinkedList classes below:

http://www.dunmanifestin.co.uk/LinkedList.cpp
http://www.dunmanifestin.co.uk/LinkedList.h

I would be really grateful if someone could have a look at my files and
try and figure out what I'm doing wrong.

Thanks,

Darrell

p.s. The operator overloader that I've put in I can only seem to get to
work if I put everything in the header file. Is it possible to split it
up between the header and the source like all the other member
functions?

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I've just written a doubly linked list


Why? As the FAQ recommends
(http://www.parashift.com/c++-faq-lit....html#faq-34.5)
regarding containers: "don't roll your own from scratch unless there is
a compelling reason to do so [e.g., if it's for homework or your own
edification]".

> but when I tell the iterator to
> move forward in the list it removes all the previous elements in the
> list.

[snip]

I don't see anything obviously wrong with it. Have you stepped through
it in your debugger?

Cheers! --M

 
Reply With Quote
 
 
 
 
Peter Most
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) wrote:

> I've just written a doubly linked list but when I tell the iterator to
> move forward in the list it removes all the previous elements in the
> list. i.e. If I were to do:
>
> List list;
>
> list.AddAtEnd(20);
> list.AddAtEnd(30);
> list.AddAtEnd(40);
> list.AddAtBeginning(10);
>
> Iterator iterator = list.GetIterator();
> iterator.MoveToStart();
> iterator.MoveForward();
>
> I would expect to still get an output of 10, 20, 30, 40 when I cycle
> through the list but it only outputs 20, 30 40. Similarly, if I were to
> call MoveForward() one more time it would only output 30, 40.
>
> Because of the size of the files I wont post the code here but I have
> provided URLs to my LinkedList classes below:
>
> http://www.dunmanifestin.co.uk/LinkedList.cpp
> http://www.dunmanifestin.co.uk/LinkedList.h
>
> I would be really grateful if someone could have a look at my files and
> try and figure out what I'm doing wrong.
>
> Thanks,
>
> Darrell
>
> p.s. The operator overloader that I've put in I can only seem to get to
> work if I put everything in the header file. Is it possible to split it
> up between the header and the source like all the other member
> functions?


Do you have a good reason not to use the std::list class?

Kind regards Peter

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-09-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I've just written a doubly linked list but when I tell the iterator to
> move forward in the list it removes all the previous elements in the
> list. i.e. If I were to do:
>
> List list;
>
> list.AddAtEnd(20);
> list.AddAtEnd(30);
> list.AddAtEnd(40);
> list.AddAtBeginning(10);
>
> Iterator iterator = list.GetIterator();
> iterator.MoveToStart();
> iterator.MoveForward();
>
> I would expect to still get an output of 10, 20, 30, 40 when I cycle
> through the list but it only outputs 20, 30 40. Similarly, if I were to
> call MoveForward() one more time it would only output 30, 40.


What do you mean by "cycle through the list"? Perhaps you're just starting
at the "current" position, which is incremented by the MoveForward call?
(In your listing, I don't see the code that's making the calls shown above,
and I don't see any code to cycle through the list here, but I'd suspect
that's the case.) Use your debugger, and it will tell you _exactly_ what
it's doing.

-Howard




 
Reply With Quote
 
darrell.blake@gmail.com
Guest
Posts: n/a
 
      12-09-2005
> Perhaps you're just starting
at the "current" position, which is incremented by the MoveForward
call?

Bingo. Thanks, you've sorted out my problem.

As for the std::list class. I didn't realise there was one. I'm writing
a particle system for my final year dissertation at University and I
figured linked lists would be the best way to store the particles. If
there's any better way I'd be glad to accept any suggestions? I'll
certainly take a look at std::list. I don't want to use any STL stuff
though.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) wrote:
> As for the std::list class. I didn't realise there was one. I'm writing
> a particle system for my final year dissertation at University and I
> figured linked lists would be the best way to store the particles. If
> there's any better way I'd be glad to accept any suggestions? I'll
> certainly take a look at std::list. I don't want to use any STL stuff
> though.


std::list is part of the STL, but why not use a tried and true linked
list instead of your own (see the FAQ I previously cited)? You can use
std::list without using all of the STL if you dislike parts of it
(e.g., the strange syntax for some algorithms).

Cheers! --M

 
Reply With Quote
 
Peter Most
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) wrote:

>> Perhaps you're just starting

> at the "current" position, which is incremented by the MoveForward
> call?
>
> Bingo. Thanks, you've sorted out my problem.
>
> As for the std::list class. I didn't realise there was one. I'm writing
> a particle system for my final year dissertation at University and I
> figured linked lists would be the best way to store the particles. If
> there's any better way I'd be glad to accept any suggestions? I'll
> certainly take a look at std::list. I don't want to use any STL stuff
> though.

Any particular reason? It's free and probably pretty bug free

-- Peter

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) wrote:


> As for the std::list class. I didn't realise there was one. I'm
> writing a particle system for my final year dissertation at
> University and I figured linked lists would be the best way to store
> the particles. If there's any better way I'd be glad to accept any
> suggestions? I'll certainly take a look at std::list. I don't want to
> use any STL stuff though.


Why don't you want to use the standard library containers? There are
very few good reasons for that decision. More likely you are
misinformed about some aspects of the standard library.


Brian


 
Reply With Quote
 
darrell.blake@gmail.com
Guest
Posts: n/a
 
      12-09-2005
I don't particularly want to use STL because I know various people who
work in the games idustry (a few of whom are lead developers) and none
of them recommend STL because of speed issues. I have to admit, I have
no opinion of my own about such things because I haven't encoutered
them yet but, obviously, I respect their opinion.

I'm going to have a word with a couple of lecturers at my University
who specialise in games development and see what they recommend. At the
end of the day, I'm more bothered about correct code than speed at this
moment in time because it's only for my dissertation.

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      12-09-2005

(E-Mail Removed) wrote:
> I don't particularly want to use STL because I know various people who
> work in the games idustry (a few of whom are lead developers) and none
> of them recommend STL because of speed issues. I have to admit, I have
> no opinion of my own about such things because I haven't encoutered
> them yet but, obviously, I respect their opinion.


For something like a linked list, I find it difficult to believe you
could write code that was materially faster than the standard library's
linked list. A more credible criticism is that using some standard
library features causes code bloat, but that's just a quality of
implementation issue.

> I'm going to have a word with a couple of lecturers at my University
> who specialise in games development and see what they recommend. At the
> end of the day, I'm more bothered about correct code than speed at this
> moment in time because it's only for my dissertation.


As you should be. The correct thing to do is use the standard
library's features and, if speed of execution truly is an issue,
profile your code and find out where the bottleneck is. I bet it's not
in the standard library's linked list implementation.

Best regards,

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments