Insert Method added to Doubly Linked list

This commit is contained in:
Luke Else 2022-03-16 22:31:54 +00:00
parent 431785b586
commit 2828466e23
3 changed files with 48 additions and 25 deletions

View File

@ -23,7 +23,7 @@ namespace C_.Datastructures
//Create a new Class with a single item //Create a new Class with a single item
return new DoublyLinkedList<T>() return new DoublyLinkedList<T>()
{ {
Head = DoublyLinkedListNode<T>.Create(value, null, null), Head = DoublyLinkedListNode<T>.Create(value, default, default),
Count = 1 Count = 1
}; };
} }
@ -36,10 +36,10 @@ namespace C_.Datastructures
DoublyLinkedList<T> list; DoublyLinkedList<T> list;
list = list1; 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 //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(); DoublyLinkedListNode<T>? end = list.Traverse();
@ -61,7 +61,7 @@ namespace C_.Datastructures
//Return Value //Return Value
DoublyLinkedListNode<T>? node = Traverse(i); DoublyLinkedListNode<T>? node = Traverse(i);
if (node != null) return node.Value; if (node != default) return node.Value;
return default; return default;
} }
set set
@ -74,22 +74,42 @@ namespace C_.Datastructures
node!.Value = value; node!.Value = value;
} }
} }
public void Add(T value) public void Append(T value)
{ {
Count++; Count++;
if (Tail != null && Count > 0) 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; Tail = Tail.Next;
return; return;
} }
Head = new DoublyLinkedListNode<T> { Value = value, Next = null, Prev = null }; Head = new DoublyLinkedListNode<T> { Value = value, Next = default, Prev = default };
Tail = Head; 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){ public bool Delete(int index){
DoublyLinkedListNode<T>? node = Traverse(index); DoublyLinkedListNode<T>? node = Traverse(index);
@ -153,7 +173,7 @@ namespace C_.Datastructures
} }
private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current) private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current)
{ {
if (current != null) if (current != default)
return current.Next; return current.Next;
return null; return null;
@ -161,7 +181,7 @@ namespace C_.Datastructures
private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current) private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
{ {
if (current != null) if (current != default)
return current.Prev; return current.Prev;
return null; return null;

View File

@ -91,6 +91,7 @@ namespace C_.Datastructures
//Fetch point in list at which item will be added //Fetch point in list at which item will be added
LinkedListNode<T>? node = Traverse(index - 1); LinkedListNode<T>? node = Traverse(index - 1);
node!.Next = new LinkedListNode<T> { node!.Next = new LinkedListNode<T> {
Value = value, Value = value,
Next = node.Next Next = node.Next

View File

@ -7,25 +7,27 @@ Console.WriteLine("Hello, World!");
DoublyLinkedList<int> list = new DoublyLinkedList<int>(); DoublyLinkedList<int> list = new DoublyLinkedList<int>();
list.Add(1); list.Append(1);
list.Add(2); list.Append(2);
list.Add(3); list.Append(3);
list.Add(4); list.Append(4);
list.Add(5); list.Append(5);
list.Add(6); list.Append(6);
list.Add(7); list.Append(7);
list.Add(8); list.Append(8);
DoublyLinkedList<int> list2 = new DoublyLinkedList<int>(); DoublyLinkedList<int> list2 = new DoublyLinkedList<int>();
list2.Add(1); list2.Append(1);
list2.Add(2); list2.Append(2);
list2.Add(3); list2.Append(3);
list2.Add(4); list2.Append(4);
list2.Add(5); list2.Append(5);
list2.Add(6); list2.Append(6);
list2.Add(7); list2.Append(7);
list2.Add(8); list2.Append(8);
list.Insert(9, 5);
list.Delete(2); list.Delete(2);