Sha256: 3bcf26df3a141cb895734145fe4cd542b903d45076a843a3c80bbb12d4e6a10f

Contents?: true

Size: 1.2 KB

Versions: 396

Compression:

Stored size: 1.2 KB

Contents

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SimpleLinkedList<T> : IEnumerable<T>
{
    public SimpleLinkedList(T value) :
        this(new[] { value })
    {
    }

    public SimpleLinkedList(IEnumerable<T> values)
    {
        var array = values.ToArray();

        if (array.Length == 0)
        {
            throw new ArgumentException("Cannot create tree from empty list");
        }

        Value = array[0];
        Next = null;

        foreach (var value in array.Skip(1))
        {
            Add(value);
        }
    }

    public T Value { get; }

    public SimpleLinkedList<T> Next { get; private set; }

    public SimpleLinkedList<T> Add(T value)
    {
        var last = this;

        while (last.Next != null)
        {
            last = last.Next;
        }

        last.Next = new SimpleLinkedList<T>(value);

        return this;
    }

    public IEnumerator<T> GetEnumerator()
    {
        yield return Value;

        foreach (var next in Next?.AsEnumerable() ?? Enumerable.Empty<T>())
        {
            yield return next;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.118 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.117 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.116 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.115 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.114 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.113 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.111 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.110 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.109 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.108 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.107 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.106 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.105 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.104 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.103 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.102 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.101 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.100 tracks/csharp/exercises/simple-linked-list/Example.cs
trackler-2.2.1.99 tracks/csharp/exercises/simple-linked-list/Example.cs