Esempio n. 1
0
        public void DeleteNodeOnNthPosition(T key)
        {
            if (EqualityComparer <T> .Default.Equals(head.Value, key))
            {
                head = head.Next;
            }
            else
            {
                var temp = head;
                try
                {
                    while (!EqualityComparer <T> .Default.Equals(temp.Next.Value, key))
                    {
                        temp = temp.Next;
                    }
                }
                catch
                {
                    Console.WriteLine("Given key not present in the collection");
                    return;
                }

                var nodeToDelete = temp.Next;
                temp.Next    = nodeToDelete.Next;
                nodeToDelete = null;
            }
        }
Esempio n. 2
0
        public void RecursivePrint(SingleLinkedListNode <T> head)
        {
            var temp = head;

            if (temp != null)
            {
                Console.WriteLine(temp.Value);
                RecursivePrint(temp.Next);
            }
            return;
        }
Esempio n. 3
0
        public void InsertNodeNthPosition(T newData, T key)
        {
            var temp = head;

            while (!EqualityComparer <T> .Default.Equals(temp.Value, key))
            {
                temp = temp.Next;
            }
            var newNode = new SingleLinkedListNode <T>(newData);

            newNode.Next = temp.Next;
            temp.Next    = newNode;
        }
Esempio n. 4
0
        public void RecursiveReverse(SingleLinkedListNode <T> current, SingleLinkedListNode <T> previous)
        {
            var temp = current;

            if (temp != null)
            {
                temp      = temp.Next;
                head      = current;
                head.Next = previous;
                previous  = head;
                RecursiveReverse(temp, previous);
            }
        }
Esempio n. 5
0
        public void InsertingAtTheBeginning(T newData)
        {
            var newNode = new SingleLinkedListNode <T>(newData);

            if (head == null)
            {
                head = newNode;
                return;
            }
            var tempHead = newNode;

            tempHead.Next = head;
            head          = newNode;
        }
Esempio n. 6
0
        public void ReverseLinkedList()
        {
            SingleLinkedListNode <T> temp     = head;
            SingleLinkedListNode <T> previous = null;
            SingleLinkedListNode <T> current  = head;

            while (temp != null)
            {
                current   = temp;
                temp      = temp.Next;
                head      = current;
                head.Next = previous;
                previous  = current;
            }
            return;
        }
Esempio n. 7
0
        public void insertingNodeAtTheEnd(T newData)
        {
            var newNode = new SingleLinkedListNode <T>(newData);

            if (head == null)
            {
                head = newNode;
                return;
            }
            var temp = head;

            while (temp.Next != null)
            {
                temp = temp.Next;
            }
            temp.Next = newNode;
        }
Esempio n. 8
0
        public void RecursiveReversePrint(SingleLinkedListNode <T> current)
        {
            if (current == null)
            {
                Console.WriteLine("Nothing to print");
                return;
            }
            var temp = current;

            if (temp.Next != null)
            {
                temp = temp.Next;
                RecursiveReversePrint(temp);
                Console.WriteLine(temp.Value);
            }
            Console.WriteLine(current.Value);
        }
Esempio n. 9
0
 public SingleLinkedListNode(T value)
 {
     Value = value;
     Next  = null;
 }