在c#中搜索特定节点 [英] Search for a specific node in c#

查看:69
本文介绍了在c#中搜索特定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我终于掌握了双链表!

虽然有点混淆我可以编辑代码的方式显示一个输出,说明当用户搜索时是否找到了Node。



这就是我的Find方法。



Hi Guys,

I am finally getting the hang of doubly linked lists!
Although kind of confused as to what way i can edit my code to Display an output saying whether or not the Node was found when a user searched for it.

This is what i have so far as my Find method.

public Node Find(object n) // finds a node in the DLL
            {
                
                current = head;
                while ((current != null) && (current.data != n))
                    current = current.next;

                if (current == null)
                {

                    return null;                  
                }
                else
                    return current;
            }





我在考虑添加一个字符串组件,但不确定最好的方法呢?



I was thinking of adding a string component to it but not sure the best way to go about it?

推荐答案

使用变量:



节点node = Find(2);

if(node == null)

Printf(Not Found);

else

Printf(Found);



或通过在该函数中添加上述代码行来修改您的函数以显示消息
Use variable:

Node node =Find(2);
if(node==null)
Printf("Not Found");
else
Printf("Found");

or modify your function by adding the above line of code in that function to display the message


这篇关于在c#中搜索特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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