Last active
September 26, 2022 02:04
-
-
Save yetanotherchris/4960171 to your computer and use it in GitHub Desktop.
Double Linked List C# example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main() | |
{ | |
DoubleLinkedList list = new DoubleLinkedList(); | |
list.Insert("1"); | |
list.Insert("2"); | |
list.Insert("3"); | |
DoubleLink link4 = list.Insert("4"); | |
list.Insert("5"); | |
Console.WriteLine("List: " + list); | |
list.InsertAfter(link4, "[4a]"); | |
Console.WriteLine("List: " + list); | |
Console.Read(); | |
} | |
public class DoubleLink | |
{ | |
public string Title { get; set; } | |
public DoubleLink PreviousLink { get; set; } | |
public DoubleLink NextLink { get; set; } | |
public DoubleLink(string title) | |
{ | |
Title = title; | |
} | |
public override string ToString() | |
{ | |
return Title; | |
} | |
} | |
public class DoubleLinkedList | |
{ | |
private DoubleLink _first; | |
public bool IsEmpty | |
{ | |
get | |
{ | |
return _first == null; | |
} | |
} | |
public DoubleLinkedList() | |
{ | |
_first = null; | |
} | |
public DoubleLink Insert(string title) | |
{ | |
// Creates a link, sets its link to the first item and then makes this the first item in the list. | |
DoubleLink link = new DoubleLink(title); | |
link.NextLink = _first; | |
if (_first != null) | |
_first.PreviousLink = link; | |
_first = link; | |
return link; | |
} | |
public DoubleLink Delete() | |
{ | |
// Gets the first item, and sets it to be the one it is linked to | |
DoubleLink temp = _first; | |
if (_first != null) | |
{ | |
_first = _first.NextLink; | |
if (_first != null) | |
_first.PreviousLink = null; | |
} | |
return temp; | |
} | |
public override string ToString() | |
{ | |
DoubleLink currentLink = _first; | |
StringBuilder builder = new StringBuilder(); | |
while (currentLink != null) | |
{ | |
builder.Append(currentLink); | |
currentLink = currentLink.NextLink; | |
} | |
return builder.ToString(); | |
} | |
///// New operations | |
public void InsertAfter(DoubleLink link, string title) | |
{ | |
if (link == null || string.IsNullOrEmpty(title)) | |
return; | |
DoubleLink newLink = new DoubleLink(title); | |
newLink.PreviousLink = link; | |
// Update the 'after' link's next reference, so its previous points to the new one | |
if (link.NextLink != null) | |
link.NextLink.PreviousLink = newLink; | |
// Steal the next link of the node, and set the after so it links to our new one | |
newLink.NextLink = link.NextLink; | |
link.NextLink = newLink; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a version that removes from the end for personal use: