Updated Append and Delete methods in Doubly Linked List
This commit is contained in:
		@@ -78,36 +78,39 @@ namespace C_.Datastructures
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            Count++;
 | 
					            Count++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (Tail != null && Count > 0)
 | 
					            //Set head to new item if list is empty
 | 
				
			||||||
            {
 | 
					            if (Head == null)
 | 
				
			||||||
                Tail.Next = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = Tail };
 | 
					            {//Append item to front of list (End as well)
 | 
				
			||||||
                Tail = Tail.Next;
 | 
					                Head = DoublyLinkedListNode<T>.Create(value, default, default);
 | 
				
			||||||
 | 
					                Tail = Head;
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Head = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = default };
 | 
					            //Append item to the end of the list
 | 
				
			||||||
            Tail = Head;
 | 
					            Tail!.Next = DoublyLinkedListNode<T>.Create(value, default, Tail);
 | 
				
			||||||
            
 | 
					            Tail = Tail.Next;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public void Insert(int index, T value)
 | 
					        public void Insert(int index, T value)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
 | 
					            if (index > Count) throw new System.Exception("Error! Index outside of Bounds");
 | 
				
			||||||
            
 | 
					            Count++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //Set head to new item if list is empty
 | 
				
			||||||
 | 
					            if (index == 0 || Head == null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Head = DoublyLinkedListNode<T>.Create(value, Head, default);
 | 
				
			||||||
 | 
					                Tail = Head;
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //Fetch point in list and add new item
 | 
					            //Fetch point in list and add new item
 | 
				
			||||||
            DoublyLinkedListNode<T>? node = Traverse(index - 1);
 | 
					            DoublyLinkedListNode<T>? node = Traverse(index - 1);
 | 
				
			||||||
            node!.Next = new DoublyLinkedListNode<T>{
 | 
					            node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node);
 | 
				
			||||||
                Value = value,
 | 
					 | 
				
			||||||
                Next = node.Next,
 | 
					 | 
				
			||||||
                Prev = node
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //Create backlink in the list
 | 
					            //Create backlink in the list
 | 
				
			||||||
            if(node.Next.Next != default)
 | 
					            if(node.Next.Next != default)
 | 
				
			||||||
                node.Next.Next.Prev = node.Next;
 | 
					                node.Next.Next.Prev = node.Next;
 | 
				
			||||||
 | 
					 | 
				
			||||||
            Count++;
 | 
					 | 
				
			||||||
            
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public bool Delete(int index){
 | 
					        public bool Delete(int index){
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user