Implementing interface with class members and functions that need access to them in C#

Viewed 50

I have this class:

public class Node
{
    public int Data;
    public Node Next;
}

and this interface:

public interface IMyList
{
    Node Head { get; set; }

    void Add(int elm);   // add new element
    void PrintAll();   //print all list
    void Reverse(); // reverse list, implement without using helpers collection\arrays  
}

and I need to implement the interface to use all it's inner functions

this is my class that implements the interface:

public class MyCustomizedList : IMyList
{
    Node IMyList.Head { get; set ; }

    public void Add(int elm) { /*something goes here*/ }  // add new element
    public void PrintAll() { /*and here*/ }   //print all list
    public void Reverse() { /*and also here*/ } 
    // reverse list, implement without using helpers collection\arrays  
}

but I don't understand how do I gain access to the fields of class Node for example If I want to Implement the function Add() how do I set the fields of the inner Node Class to attach pointer to the Next Node and the elm value to it's data?

2 Answers

Right now, you are implementing the property Head explicitly:

Node IMyList.Head { get; set ; }

To access it, the left hand side of the . must be IList. One simple way to do that is ((IList)this).Head.

For example, to print it to the console:

Console.WriteLine(((IList)this)Head);

Alternatively, if there is no reason for you to implement Head explicitly, don't. Implement it implicitly instead, just like the other members:

public Node Head { get; set; }

Try this:

public class MyCustomizedList : IMyList
{
    public Node Head { get; set; }

    public void Add(int elm)
    {
        var newNode = new Node { Data = elm };
        if (this.Head == null)
        {
            this.Head = newNode;
        }
        else
        {
            var node = this.Head;
            while (node.Next != null)
            {
                node = node.Next;
            }

            node.Next = newNode;
        }
    }

    public void PrintAll()
    {
        var builder = new StringBuilder();

        var node = this.Head;
        while (node.Next != null)
        {
            builder.AppendLine(node.Data.ToString());
            node = node.Next;
        }

        var text = builder.ToString();
        Console.WriteLine(text);
    }

    public void Reverse()
    {
        if (this.Head?.Next != null)
        {
            var node = this.Head;
            this.Head = Reverse(node, node.Next);
            node.Next = null;
        }
    }

    private static Node Reverse(Node node1, Node node2)
    {
        if (node2.Next != null)
        {
            var lastNode = Reverse(node2, node2.Next);
            node2.Next = node1;
            return lastNode;
        }

        node2.Next = node1;
        return node2;
    }
}

I think isn't not an efficient data structure. What is the purpose of this class? To Add an item you must iterate all elements. You haven't any remove method. Head is public and their properties too: is not protected from outside mistakes, is not encapsulated...

Related