Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建自定义链接列表_C# - Fatal编程技术网

C# 创建自定义链接列表

C# 创建自定义链接列表,c#,C#,我正在创建自己的LinkedList。但我似乎无法解决这个错误。谁能帮帮我 问题是我想在特定元素之后插入一个对象。我创建了Find方法来搜索那个特定的项并返回它的引用,但我似乎无法解决它 CustomLinkedList c = new CustomLinkedList(); c.Add(31); c.Add(45); c.Add(23); c.Add(36); c.PrintList();

我正在创建自己的LinkedList。但我似乎无法解决这个错误。谁能帮帮我

问题是我想在特定元素之后插入一个对象。我创建了Find方法来搜索那个特定的项并返回它的引用,但我似乎无法解决它

CustomLinkedList c = new CustomLinkedList();
        c.Add(31);

        c.Add(45);

        c.Add(23);

        c.Add(36);

        c.PrintList();

        Console.WriteLine("\n" + " Process of adding item at a spectifed location");
        c.Addafter(66,23);


        c.PrintList();

class Node
{

    public object Element;
    public Node Link;

    public Node()
    {
        Element = null;
        Link = null;
    }

    public Node(object TheElement)
    {
        Element = TheElement;
        Link = null;
    }

class CustomLinkedList
{
    protected Node header;
    protected Node last;

    public CustomLinkedList()
    {
        //header = new Node("header");
    }

    private Node Find(object Item)
    {
        Node Current = new Node();
        Current = header;
        while (Current.Element != Item && Current.Link !=null)
        {
            Current = Current.Link;
        }
        return Current;
    }


    public void PrintList()
    {            
        Node n = new Node();
        n = header;
        while (n != null)
        {
            Console.WriteLine(n.Element);
            n = n.Link;
        }
    }

    public void Add(object a)
    {
        Node n = new Node();
        n.Element = a;
        n.Link = null;
        if (last == null)
        {
            header = n;
            last = n;
        }
        else
        {
            last.Link = n;
            last = n;
        }
    }

    public void Addafter(object newitem, object After)
    {
        Node current = new Node();
        Node newNode = new Node(newitem);
        current = Find(After);

        newNode.Link = current.Link;
        current.Link = newNode;
    }
}

您的代码有两个问题

当项目不在列表中时,Find函数返回Tail元素。 当n.Element和Item是object类型时,比较它们是很棘手的。让你的类成为泛型来解决这个问题。


我不知道您在测试中使用的是什么,但是值类型不会按照您认为应该的方式工作,因为正在比较框值

例如,这将始终返回列表中的最后3个元素

CustomLinkedList cList= new CustomLinkedList();
cList.Add(1);
cList.Add(2);
cList.Add(3);

Console.WriteLine(cList.Find(2).Element);
但这将对输出2起作用

CustomLinkedList cll = new CustomLinkedList();
object a = 1;
object b = 2;
object c = 3;
cll.Add(a);
cll.Add(b);
cll.Add(c);

Console.WriteLine(cll.Find(b).Element);

对于不实现!=接线员。因此,字符串可以工作,但其他功能几乎不起作用。它不起作用的原因在于这一行:

while (Current.Element != Item && Current.Link !=null)
==和!=对象类型的运算符检查引用相等性。例如,如果将列表与值类型int一起使用,则这些值将被装箱到不同的对象中,并且!=运算符将始终返回true。有关装箱的详细信息,请参阅

考虑这一点:

object x = 42;
object y = 42;
Console.WriteLine(x == y); // prints False
Console.WriteLine(x.Equals(y)); // prints True
您当前的代码可以很好地处理引用类型:

var list = new CustomLinkedList();
list.Add("hello");
list.Add("!");
list.Addafter("world", "hello");
list.PrintList();
输出:

你好 世界 !

但对于值类型,它永远不会找到after值,因此它会将新项追加到列表的末尾:

var list = new CustomLinkedList();
list.Add(1);
list.Add(3);
list.Addafter(2, 1);
list.PrintList();
输出:

一, 3. 二,

因此,您需要将==运算符替换为对Equals的调用:


您可能还希望包括Node@Conrad弗里克斯:我想把节点放在哪里?@Pro_Zeck。很抱歉我的意思是,您可能希望在帖子中包含Node的实现,因为Node.Element和Node.Link可能是problemNode Current=new Node;当前=标题;:伊克斯。@Prozeck:新的节点对象从未被使用过。==并不像看上去那么简单。请参见编辑。感谢您的详细解释!
var list = new CustomLinkedList();
list.Add(1);
list.Add(3);
list.Addafter(2, 1);
list.PrintList();
while (!object.Equals(Current.Element, Item) && Current.Link !=null)