【问题标题】:LinkedList C# (data structures and algorithms using C# michael mcmillan )LinkedList C#(使用 C# michael mcmillan 的数据结构和算法)
【发布时间】:2022-01-19 03:20:29
【问题描述】:

我在一本书中找到了这个代码(使用 C# michael mcmillan 的数据结构和算法)来做一个链表,但我不明白什么是对象,为什么我们要使用 find 方法,当我把它放在 vsc 中时它给了我错误.

(错误 CS1061:“节点”不包含“标头”的定义,并且找不到接受“节点”类型的第一个参数的可访问扩展方法“标头”)

public class Node
{
      public Object Element;
      public Node Link;
      
      public Node()
      {
          Element = null;
          Link = null;
      }
      public Node(Object theElement)
      {
          Element = theElement;
          Link = null;
      }
}
class LinkedList
{
     protected Node header;
     public LinkedList() 
     {
         header = new Node("header");
     }

     private Node Find(Object item) 
     {
         Node current = new Node();
         current = header;
         while(current.header !=item)   //i cant understand this line page 198 in the book
              current = current.Link;
         return current;
     }
     public void Insert(Object newItem, Object after) 
     {
         Node current = new Node();
         Node newNode = new Node(newItem);
         current = Find(after);
         newNode.Link = current.Link;
         current.Link = newNode;
     }
}

【问题讨论】:

  • 看起来像是一个错字。试试while(current.Element !=item)
  • 作者打错了....条件应该是current.Element !=item
  • 但是如果没有找到item会导致错误。也许买一本新书。
  • 而他们执行new Node()的次数只是为了立即丢弃它并不能很好地反映作者..

标签: c# singly-linked-list


【解决方案1】:
public Node Find(Object item)
{
    Node current = new Node();
    current = header;
    while (!object.Equals(Current.Element, Item) && Current.Link !=null)
        current = current.Link;
    return current;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多