我的链表代码有什么问题? [英] What is wrong with my linked list code?

查看:70
本文介绍了我的链表代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个链表。我正在使用new和delete,因为我应该在将来更新此代码到C ++。但是我添加或删除代码功能的东西是不对的,我已粘贴在下面。



非常感谢您的帮助。我尝试使用双向链表或单个链表删除节点,但它没有发生。我做错了什么?



我的加密码:



  void  add( int  number)
{
if (!head){
head = new tracker;
head-> i = number;
head-> next = 0 ;
head-> prev = 0 ;
tail = head;
} 其他 {
tail-> next = 跟踪器;
tail-> prev = tail;
tail = tail-> next;
tail-> i = number;
tail-> next = 0 ;
}
}



我的删除代码:

 {
struct tracker * tmp;
struct tracker * bla;

tmp = head;
int rcount = 0;

while(tmp){
if(tmp-> i == number){
cout<<> PS while 循环需要遍历整个列表 生产代码将有一个标志 for 每个需要删除的记录。但无论我做什么,我的打印列表功能都会继续打印 out 3 记录,尽管使用 删除功能。

感谢 你的帮助。

我尝试过:

非常感谢您的帮助。我尝试使用双向链表或单个链表删除节点,但' 没有发生。我做错了什么?

解决方案

嗯...

 tail-> next = 跟踪器; 
tail-> prev = tail;

所以每个元素都有一个指向自身的前一个元素指针...前一个元素永远不会指向它之后的那个...

然后你这样做:

 tail = tail-> next; 

哪些设置 tail 到一个未定义的值然后尝试在后续操作中使用...

找到你的调试器,用它来完成你的代码正在做的事 - 你我会很快发现这种问题。



创建一个新节点并在完成后只将它添加到列表中是个好主意 - 这样两者都是效果更好,因为你可以分别访问你感兴趣的两个节点,并且在多线程环境中工作得更好。



Quote:

是的,但我只在创建新的尾记录时设置了prev指针,所以tail-> prev设置为tail,我很快就会更新。



停下来想一想。

在插入之前,它看起来像这样:

 Head-- Tail-- 
| |
v v
--->节点1 ---------->节点2
| next - next - null
| prev - null prev--
| |
----------------------------------



添加后,你需要这个:

 Head-- Tail ------------------- ---- 
| |
v v
--->节点1 ---------->节点2 ---------->节点3
| next - next - next - null
| prev - null prev - prev - 返回Node2
| | (我不是在画那个!)
-----------------------------------



所以在一个临时变量中创建你的节点,正确地设置它的下一个和前一个,然后移动当前Tail的下一个,然后移动Tail指针来寻址新节点。 / blockquote>

使用调试器来查看代码正在做什么,而不是猜测代码在做什么。它允许你逐行执行第1行并在每行之后检查变量。



调试器允许你逐行跟踪执行,检查变量你会看到有一点让它停止做你期望的事情。

调试器 - 维基百科,免费的百科全书 [ ^ ]

< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn>在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有在调试器中魔术,它没有发现bug,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I'm dabbling with creating a linked list. I'm using new and delete, as i should update this code in the future to C++ anyway. But something is not right with my add or remove code functions, which i have pasted below.

Would appreciate your help. I tried removing a node either with a doubly linked list or a single linked list, but it's not happening. What am i doing wrong?

My add code:

void add(int number)
{
    if(!head) {
    head=new tracker;
    head->i=number;
    head->next=0;
    head->prev=0;
    tail=head;
    } else {
    tail->next=new tracker;
    tail->prev=tail;
    tail=tail->next;
    tail->i=number;
    tail->next=0;
    }
}


My remove code:

PS the while loop needs to go through the entire list as the production code will have a flag for every record that needs to be deleted. But no matter what i do, my print list function keep printing out 3 records, despite me using the remove function.

Thanks for your help.

What I have tried:

Would appreciate your help. I tried removing a node either with a doubly linked list or a single linked list, but it's not happening. What am i doing wrong?

解决方案

Um...

tail->next=new tracker;
tail->prev=tail;

So every element has a previous element pointer which points at itself...and the previous element never points at the one after it...
And then you do this:

tail=tail->next;

Which sets tail to an undefined value which you then try to use in subsequent operations...
Find your debugger, and use it to follow through what you code is doing - you would have spotted this kind of problem very quickly.

It's a good idea to create a new node and only add it to the list when it's complete - that way it both works better because you can access the two nodes you are interested in separately, and works better in a multi threaded environment as well.

Quote:

Yes but i set the prev pointer only when creating a new tail record, so tail->prev gets set to tail , which i am updating shortly.



Stop and think about it for a moment.
Before your insert, it looks like this:

Head--               Tail--
     |                    |
     v                    v
---> Node 1   ----------> Node 2
|      next --              next -- null
|      prev -- null         prev--
|                                |
----------------------------------


After your add, you want this:

Head--               Tail-----------------------
     |                                         |
     v                                         v
---> Node 1   ----------> Node 2   ----------> Node 3
|      next --              next --              next -- null
|      prev -- null         prev --              prev -- back to Node2
|                                 |                        (I'm not drawing that!)
-----------------------------------


So create your node in a temporary variable, set it's next and previous correctly, and then move the next of the current Tail, and then Tail pointers to address the new node.


Rather than guessing what your code is doing, use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables after each lines.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我的链表代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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