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

http://www.algosort.com/

 

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) {
        Node node = _head.Next;
        while (node != null) {
            if (node.Value.Equals(obj)) {
                return true;
            }
            node = node.Next;
        }
        return false;
    }

    public void InsertAtHead(Object obj) {
        _head.Next = new Node(_head.Next, obj);
    }

    public void InsertAtHeadFromArray(Object[] array) {
        if (array != null) {
            for (int i = 0; i < array.Length; i++) {
                InsertAtHead(array[array.Length – 1 – i]);
            }
        }
    }

    public bool InsertAt(Object obj, int index) {
        if (index < 0) {
            return false;
        } else {
            Node node = _head;
            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("[ ");
        Node node = _head.Next;
        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());

        list.InsertAtHeadFromArray(name);
        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"));
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s