Insert Method added to Doubly Linked list
This commit is contained in:
		@@ -23,7 +23,7 @@ namespace C_.Datastructures
 | 
			
		||||
            //Create a new Class with a single item
 | 
			
		||||
            return new DoublyLinkedList<T>()
 | 
			
		||||
            {
 | 
			
		||||
                Head = DoublyLinkedListNode<T>.Create(value, null, null),
 | 
			
		||||
                Head = DoublyLinkedListNode<T>.Create(value, default, default),
 | 
			
		||||
                Count = 1
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
@@ -36,10 +36,10 @@ namespace C_.Datastructures
 | 
			
		||||
            DoublyLinkedList<T> list;
 | 
			
		||||
 | 
			
		||||
            list = list1;
 | 
			
		||||
            if (list == null || list.Count == 0) return list2;
 | 
			
		||||
            if (list == default || list.Count == 0) return list2;
 | 
			
		||||
 | 
			
		||||
            //Find end of list and append fist item of next list
 | 
			
		||||
            if (list2 == null || list.Count == 0) return list;
 | 
			
		||||
            if (list2 == default || list.Count == 0) return list;
 | 
			
		||||
 | 
			
		||||
            DoublyLinkedListNode<T>? end = list.Traverse();
 | 
			
		||||
 | 
			
		||||
@@ -61,7 +61,7 @@ namespace C_.Datastructures
 | 
			
		||||
 | 
			
		||||
                //Return Value
 | 
			
		||||
                DoublyLinkedListNode<T>? node = Traverse(i);
 | 
			
		||||
                if (node != null) return node.Value;
 | 
			
		||||
                if (node != default) return node.Value;
 | 
			
		||||
                return default;
 | 
			
		||||
            }
 | 
			
		||||
            set
 | 
			
		||||
@@ -74,22 +74,42 @@ namespace C_.Datastructures
 | 
			
		||||
                node!.Value = value;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        public void Add(T value)
 | 
			
		||||
        public void Append(T value)
 | 
			
		||||
        {
 | 
			
		||||
            Count++;
 | 
			
		||||
 | 
			
		||||
            if (Tail != null && Count > 0)
 | 
			
		||||
            {
 | 
			
		||||
                Tail.Next = new DoublyLinkedListNode<T> { Value = value, Next = null, Prev = Tail };
 | 
			
		||||
                Tail.Next = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = Tail };
 | 
			
		||||
                Tail = Tail.Next;
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Head = new DoublyLinkedListNode<T> { Value = value, Next = null, Prev = null };
 | 
			
		||||
            Head = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = default };
 | 
			
		||||
            Tail = Head;
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Insert(int index, T value)
 | 
			
		||||
        {
 | 
			
		||||
            if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
 | 
			
		||||
            
 | 
			
		||||
            //Fetch point in list and add new item
 | 
			
		||||
            DoublyLinkedListNode<T>? node = Traverse(index - 1);
 | 
			
		||||
            node!.Next = new DoublyLinkedListNode<T>{
 | 
			
		||||
                Value = value,
 | 
			
		||||
                Next = node.Next,
 | 
			
		||||
                Prev = node
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            //Create backlink in the list
 | 
			
		||||
            if(node.Next.Next != default)
 | 
			
		||||
                node.Next.Next.Prev = node.Next;
 | 
			
		||||
 | 
			
		||||
            Count++;
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool Delete(int index){
 | 
			
		||||
            DoublyLinkedListNode<T>? node = Traverse(index);
 | 
			
		||||
 | 
			
		||||
@@ -153,7 +173,7 @@ namespace C_.Datastructures
 | 
			
		||||
        }
 | 
			
		||||
        private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current)
 | 
			
		||||
        {
 | 
			
		||||
            if (current != null)
 | 
			
		||||
            if (current != default)
 | 
			
		||||
                return current.Next;
 | 
			
		||||
 | 
			
		||||
            return null;
 | 
			
		||||
@@ -161,7 +181,7 @@ namespace C_.Datastructures
 | 
			
		||||
 | 
			
		||||
        private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
 | 
			
		||||
        {
 | 
			
		||||
            if (current != null)
 | 
			
		||||
            if (current != default)
 | 
			
		||||
                return current.Prev;
 | 
			
		||||
 | 
			
		||||
            return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -91,6 +91,7 @@ namespace C_.Datastructures
 | 
			
		||||
 | 
			
		||||
            //Fetch point in list at which item will be added
 | 
			
		||||
            LinkedListNode<T>? node = Traverse(index - 1);
 | 
			
		||||
 | 
			
		||||
            node!.Next = new LinkedListNode<T> { 
 | 
			
		||||
                Value = value, 
 | 
			
		||||
                Next = node.Next 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user