# Data Structures and Algorithms: Single List

Recently, I have to teach my co-worker about the Data structures and algorithms with C#. Today, I made a Single List sample and published it.

And, I found cool site about the Algorithms.

Computer Programming Algorithms Directory

How to compile this source code: csc /nologo SingleList.cs SingleApp.cs Node.cs

Node.cs

————————–

using System;

public class Node {
Node _next;
Object _value;

public Node(Node next, Object value) {
_next = next;
_value = value;
}

public Node Next {
get {
return _next;
}
set {
_next = value;
}
}

public Object Value {
get {
return _value;
}
}

public override String ToString() {
return _value.ToString();
}
}

———–

SingleList.cs

————-

using System;
using System.Text;

public class SingleList {
Node _head = new Node(null, "Dummy");

public bool Contains(Object obj) {
while (node != null) {
if (node.Value.Equals(obj)) {
return true;
}
node = node.Next;
}
return false;
}

}

if (array != null) {
for (int i = 0; i < array.Length; i++) {
}
}
}

public bool InsertAt(Object obj, int index) {
if (index < 0) {
return false;
} else {
for (int i = 0; i < index; i++) {
node = node.Next;
if (node == null) {
return false;
}
}
node.Next = new Node(node.Next, obj);
return true;
}
}

public override String ToString() {
StringBuilder builder = new StringBuilder();
builder.Append("[ ");
while (node != null) {
builder.Append(node.ToString());
builder.Append(" ");
node = node.Next;
}
builder.Append("]");
return builder.ToString();
}
}

———————-

SingleApp.cs (testing code)

———————-

using System;

public class SingleApp {
public static void Main(String[] args) {
String[] name = {
"Alice", "Bob", "Diana", "Elmo", "Fred", "Gaas",
};
SingleList list = new SingleList();
Console.WriteLine("list = {0}", list.ToString());

Console.WriteLine("list = {0}", list.ToString());

list.InsertAt("Chris", 2);
Console.WriteLine("list = {0}", list.ToString());

list.InsertAt("Harold", 7);
Console.WriteLine("list = {0}", list.ToString());

Console.WriteLine("list.Contains(Chris) = {0}", list.Contains("Chris"));
Console.WriteLine("list.Contains(Zoe) = {0}", list.Contains("Zoe"));
}
}