Esempio n. 1
0
    public static string ToString <T>(NodeLink <T> list)
    {
        var p = list;

        if (!Linked.IsCyclic(p))
        {
            var sb = new StringBuilder();
            sb.Append("[");
            while (p != null && p.Next != null)
            {
                if (p.Next.Value != null)
                {
                    sb.Append($"{p.Next.Value.ToString()},");
                }
                else
                {
                    sb.Append($"(null)");
                }
                p = p.Next;
            }
            if (sb[sb.Length - 1] != '[')
            {
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            return(sb.ToString());
        }
        else
        {
            return("[cyclic]");
        }
    }
Esempio n. 2
0
    public static int Count <T>(NodeLink <T> list)
    {
        int counter = 0;
        var p       = list;

        if (!Linked.IsCyclic(list))
        {
            while (p != null && p.Next != null)
            {
                counter++;
                p = p.Next;
            }
        }
        else
        {
            throw new Exception("List is Cyclic.");
        }
        return(counter);
    }
Esempio n. 3
0
    public static NodeLink <T> CycleNode <T>(NodeLink <T> list)
    {
        if (Linked.IsCyclic(list) == false)
        {
            return(default(NodeLink <T>));
        }
        NodeLink <T> fast     = list;
        NodeLink <T> slow     = list;
        NodeLink <T> prevfast = list;

        while (fast != null && fast.Next != null)
        {
            prevfast = fast;
            fast     = fast.Next.Next;
            slow     = slow.Next;
            if (fast == slow)
            {
                return(prevfast.Next);
            }
        }
        return(default(NodeLink <T>));
    }