Ejemplo n.º 1
0
        public void SplitItIntoTwoHalves()
        {
            CircularLinkedListNodeSM dummy = Head;
            CircularLinkedListNodeSM secondHalf;

            CircularLinkedListNodeSM firstHalf;

            if (Head == null)
            {
                Console.WriteLine("No data present to be split");
                return;
            }
            int nodeCount = 0;

            do
            {
                nodeCount++;
                dummy = dummy.Next;
            } while (dummy != Head);
            dummy = Head;
            int count = (int)Math.Ceiling(nodeCount / 2.0);

            while (count-- > 1)
            {
                dummy = dummy.Next;
            }
            secondHalf = dummy.Next;
            firstHalf  = Head;
            dummy.Next = firstHalf;
            Last.Next  = secondHalf;
            Print(firstHalf);
            Console.WriteLine();
            Print(secondHalf);
        }
Ejemplo n.º 2
0
        public void SortedInsertInCircularList(int d)
        {
            if (Head == null)
            {
                AddToEnd(d);
                return;
            }

            if (Head.Data > d)
            {
                AddAtStart(d);
                return;
            }

            if (Last.Data < d)
            {
                AddToEnd(d);
                return;
            }

            CircularLinkedListNodeSM dummy       = Head;
            CircularLinkedListNodeSM dummybefore = null;

            while (dummy.Data < d)
            {
                dummybefore = Head;
                dummy       = dummy.Next;
            }
            CircularLinkedListNodeSM c = new CircularLinkedListNodeSM(d);

            c.Next           = dummy;
            dummybefore.Next = c;
        }
Ejemplo n.º 3
0
        public void Print(CircularLinkedListNodeSM firstHalf)
        {
            CircularLinkedListNodeSM dummy = firstHalf;

            if (dummy == null)
            {
                Console.WriteLine("No Data present");
                return;
            }
            do
            {
                Console.WriteLine(dummy.Data);
                dummy = dummy.Next;
            } while (dummy != firstHalf);
        }
Ejemplo n.º 4
0
        public void AddToEnd(int d)
        {
            CircularLinkedListNodeSM circularLinkedListNodeSM = new CircularLinkedListNodeSM(d);

            if (Head == null)
            {
                Head      = circularLinkedListNodeSM;
                Head.Next = Head;
                Last      = Head;
                return;
            }
            Last.Next = circularLinkedListNodeSM;
            circularLinkedListNodeSM.Next = Head;
            Last = circularLinkedListNodeSM;
        }