Ahmed reda
Ahmed reda

Reputation: 19

LinkedList C# (data structures and algorithms using C# michael mcmillan )

I found This Code in a Book (data structures and algorithms using C# michael mcmillan )to do a linkedlist but i cant understand what is object after and why do we use find method and when i put it in vsc it gave me error.

(error CS1061: 'Node' does not contain a definition for 'header' and no accessible extension method 'header' accepting a first argument of type 'Node' could be found)

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;
     }
}

Upvotes: 0

Views: 155

Answers (3)

AryanVadera
AryanVadera

Reputation: 1

The problem and code confusion are caused by some errors in the Find method's implementation, as well as some misunderstandings about how the linked list should work.

  1. Error in the Find Method: The line while(current.header!= item) should be replaced with while(current.Element!= item). The Node class does not have a header field, but instead has an Element field.

  2. Purpose of the After and Find methods: After is an object that represents the element to which you wish to place the new node. The Find method searches the linked list for a node that has the given item. If it finds a node, it returns it.

Here is the revised version of the code :

class LinkedList
{
    protected Node header;

    public LinkedList()
    {
        header = new Node("header");
    }

    private Node Find(Object item)
    {
        Node current = header;
        while (current != null && !current.Element.Equals(item))
            current = current.Link;
        return current;
    }

    public void Insert(Object newItem, Object after)
    {
        Node current = Find(after);
        if (current != null)
        {
            Node newNode = new Node(newItem);
            newNode.Link = current.Link;
            current.Link = newNode;
        }
    }
}

Upvotes: 0

Om Dave
Om Dave

Reputation: 1

The mistake and the confusion are caused by not understanding the code. There are several things that need to be done:

Using title in the Find method in the wrong way: There is a mistake in the line while(current.header!= item) because current is a Node object and does not have a header field. The correct thing to do is to compare current.Element to the item.

Setting up new Node objects: The way that new Node objects are set up is unnecessary. It is possible to make it easier to create a new Node object and then quickly change its assignment.

// Find the node containing the specified item
private Node Find(Object item) 
{
    Node current = header;
    while (current != null && !current.Element.Equals(item)) // Compare with current.Element
    {
        current = current.Link;
    }
    return current;
}

Upvotes: 0

Zakaria Najim
Zakaria Najim

Reputation: 68

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;
}

Upvotes: 0

Related Questions