Skip to content

CSharp example #18

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions CSharp/1-single.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedList
{
class SingleLinkedList<T>
{
private Node last = null;

private class Node
{
internal T item;
internal Node previous;
}

public bool isEmpty()
{
return last == null;
}

public void addNode(T item)
{
Node oldLast = last;
last = new Node();
last.item = item;
last.previous = oldLast;
}

public List<T> returnList()
{
List<T> res = new List<T>();
Node temp = last;
T item = default(T);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for item initialization.

while (!isEmpty())
{
item = last.item;
last = last.previous;
res.Add(item);
}
last = temp;
return res;
}
}
}
52 changes: 52 additions & 0 deletions CSharp/2-doubly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedList
{
class DoubleLinkedList<T>
{
private Node last = null;

private class Node
{
internal T item;
internal Node next;
internal Node previous;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifier can be public because class Node is inner private, so fields can be accessed only in DoubleLinkedList class scope.

}

public bool isEmpty()
{
return last == null;
}

public void addNode(T item)
{
Node oldLast = last;
last = new Node();
last.item = item;
last.previous = oldLast;
if (oldLast != null)
{
oldLast.next = last;
}
}

public List<T> returnList()
{
List<T> res = new List<T>();
Node temp = last;
T item = default(T);
while (!isEmpty())
{
item = last.item;
last = last.previous;
res.Add(item);
}
last = temp;
return res;
}
}
}