Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double linkedlist in c# #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 99 additions & 37 deletions C#/DoubleLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,100 @@
class DoubleLinkedList<T>
public static void Main()
{
private T value;
private LinkedList<T> next;
private LinkedList<T> prev;

public DoubleLinkedList(LinkedList<T> prev, T value, LinkedList<T> next) {
SetPrev(prev);
SetValue(value);
SetNext(next);
}

public DoubleLinkedList(T value) : this(null, value, null){}

public LinkedList<T> GetPrev() {
return prev;
}

public T GetValue() {
return value;
}

public LinkedList<T> GetNext() {
return next;
}

public void SetPrev(LinkedList<T> prev) {
this.prev = prev;
}

public void SetValue(T value) {
this.value = value;
}

public void SetNext(LinkedList<T> next) {
this.next = next;
}
}
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;
}
}
}