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; | |
} | |
} |
It's a stacked doubly linked list viniciusmi00 hence he added the InsertAfter so you can add to the stack at any position.
I made a version that removes from the end for personal use:
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 class DoubleLinkedList
{
private DoubleLink _first;
private DoubleLink _last;
private int _length;
public int length() { return _length; }
public DoubleLinkedList()
{
_first = null;
_last = null;
_length = 0;
}
public DoubleLink unshift(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;
if (_last == null) _last = link;
_length ++;
return link;
}
public DoubleLink shift()
{
// 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;
}
_length = Math.Max(0, _length - 1);
return temp;
}
public DoubleLink pop()
{
// Gets the last item, and sets it to be the one it is linked to
DoubleLink temp = _last;
if (_last != null)
{
_last = _last.PreviousLink;
if (_last != null)
_last.NextLink = null;
}
_length = Math.Max(0, _length - 1);
return temp;
}
public override string ToString()
{
DoubleLink currentLink = _first;
StringBuilder builder = new StringBuilder();
while (currentLink != null)
{
builder.Append(currentLink.Title);
currentLink = currentLink.NextLink;
}
return builder.ToString();
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The list is reversed.