链接列表无法正常工作 [英] Linked List Isn't Working Correctly

查看:67
本文介绍了链接列表无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编写了一个双向链表,但是当我告诉迭代器

在列表中向前移动时,它会删除

列表中的所有先前元素。即如果我这样做:


列表清单;


list.AddAtEnd(20);

list .AddAtEnd(30);

list.AddAtEnd(40);

list.AddAtBeginning(10);


迭代器迭代器= list.GetIterator();

iterator.MoveToStart();

iterator.MoveForward();


我希望当我通过列表循环

时仍然得到10,20,30,40的输出但它只输出20,30,40同样,如果我要

再次调用MoveForward()它只会输出30,40。


由于文件的大小,我不会在这里发布代码,但我有

提供以下链接列表的URL:

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

如果有人可以查看我的文件并且想要找出我做错了什么,我将非常感激。


谢谢,


Darrell


ps那我特地把我在操作overloader只能似乎得到

的工作,如果我把一切都放在头文件。是否有可能将它分割成标题和源之间的
像所有其他成员一样

函数?

解决方案

da *********** @ gmail .COM 写道:我特地为刚写了一个双向链表


为什么?正如FAQ推荐的那样
http://www.parashift.com/c++-faq-lit....html#faq-34.5

关于容器:不要滚动你自己从零开始,除非有令人信服的理由这样做[例如,如果它是为了家庭作业或你自己的

edification]"

但是当我告诉迭代器在列表中向前移动时它会删除
列表中的所有先前元素。



[snip]


我没有看到任何明显错误的东西。你有没有在调试器中通过

它?


干杯! --M


哒** *********@gmail.com 写道:

我刚刚写了一个双向链表,但当我告诉迭代器列表中的所有先前元素。即如果我这样做:

列表清单;

list.AddAtEnd(20);
list.AddAtEnd(30);
列表。 AddAtEnd(40);
list.AddAtBeginning(10);

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

当我循环通过列表时,我希望仍能获得10,20,30,40的输出,但它只输出20,30,40。同样,如果我是再次调用MoveForward()它只会输出30,40。

由于文件的大小,我不会在这里发布代码,但我已经提供了
下面我的LinkedList类的URL:

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

我会很感激,如果有人可以看看我的文件d
试图找出I''m做错了。

谢谢

/>附:如果我把所有东西都放在头文件中,我放入的操作符overloader似乎只能工作。是否有可能像所有其他成员
函数一样在标题和源之间分开?




你有充分的理由吗?不使用std :: list类?


亲切问候彼得




< da *********** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

我刚刚编写了一个双向链表,但当我告诉迭代器在列表中向前移动时,它会删除
列表中的所有先前元素。即如果我这样做:

列表清单;

list.AddAtEnd(20);
list.AddAtEnd(30);
列表。 AddAtEnd(40);
list.AddAtBeginning(10);

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

当我循环通过列表时,我希望仍能获得10,20,30,40的输出,但它只输出20,30,40。同样,如果我是再次调用MoveForward()它只会输出30,40。




你是什么意思循环通过列表?也许你只是在当前的时候开始

位置,由MoveForward调用增加?

(在你的列表中,我没有看到上面显示的调用代码,

和我没有看到任何代码在这里循环显示,但我怀疑

就是这种情况。)使用你的调试器,它会告诉你_exactly_ what

它做什么。


-Howard



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?

解决方案

da***********@gmail.com 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


da***********@gmail.com 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



<da***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr 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



这篇关于链接列表无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆