Exemplo n.º 1
0
 public MyLinkedList()
 {
     Root = new MLLElement()
     {
         Value = 0
     };
 }
Exemplo n.º 2
0
        public void Reverse()
        {
            MLLElement current = Root;
            MLLElement prev    = null;
            MLLElement next    = null;
            bool       exit    = false;

            while (!exit)
            {
                next = current.Next;

                if (current == Root)
                {
                    current.Next = null;
                }
                else
                {
                    current.Next = prev;
                }

                prev = current;

                if (next != null)
                {
                    current = next;
                }
                else
                {
                    Root = current;
                    exit = true;
                }
            }
        }
Exemplo n.º 3
0
        public void Print()
        {
            MLLElement current = Root;

            while (current != null)
            {
                Console.WriteLine(current.Value);
                current = current.Next;
            }
        }
        public string ConvertToString()
        {
            StringBuilder sb      = new StringBuilder();
            MLLElement    current = Root;

            while (current != null)
            {
                sb.AppendLine(current.Value.ToString());
                current = current.Next;
            }

            return(sb.ToString());
        }
Exemplo n.º 5
0
        public void Populate(int amount)
        {
            MLLElement current = Root;

            for (int i = 1; i < amount; i++)
            {
                MLLElement el = new MLLElement()
                {
                    Value = i
                };
                current.Next = el;
                current      = el;
            }
        }
        public void Add(int value)
        {
            MLLElement newEl = new MLLElement();

            newEl.Value = value;
            if (Root == null)
            {
                Root = newEl;
            }
            else
            {
                MLLElement current  = Root;
                MLLElement previous = null;

                while (true)
                {
                    if (newEl.Value <= current.Value)
                    {
                        if (current == Root)
                        {
                            newEl.Next = Root;
                            Root       = newEl;
                            break;
                        }
                        else
                        {
                            newEl.Next    = current;
                            previous.Next = newEl;
                            break;
                        }
                    }
                    else
                    {
                        if (current.Next == null)
                        {
                            current.Next = newEl;
                            break;
                        }
                    }

                    previous = current;
                    current  = current.Next;
                }
            }
        }
Exemplo n.º 7
0
        public bool HasLoop2()
        {
            //StringBuilder sb = new StringBuilder();
            MLLElement slow_p = Root, fast_p = Root;

            while (slow_p != null && fast_p != null && fast_p.Next != null)
            {
                slow_p = slow_p.Next;
                fast_p = fast_p.Next.Next;
                //Console.WriteLine(slow_p.Value + " " + fast_p.Value);
                //sb.AppendLine(slow_p.Value + " " + fast_p.Value);
                if (slow_p == fast_p)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 8
0
        public bool HasLoop()
        {
            HashSet <MLLElement> hsOccurencies = new HashSet <MLLElement>();

            MLLElement current = Root;

            while (current != null)
            {
                if (hsOccurencies.Contains(current))
                {
                    return(true);
                }
                else
                {
                    hsOccurencies.Add(current);
                }
                current = current.Next;
            }

            return(false);
        }