Sha256: db730ed7b5b31846fb91b04cceb7ed55f22d99f4b00d3ad07976282352466efb

Contents?: true

Size: 1.12 KB

Versions: 396

Compression:

Stored size: 1.12 KB

Contents

public class Deque<T>
{
    private Element head;

    public void Push(T value)
    {
        if (head == null)
        {
            head = new Element(value);
        }
        else
        {
            var last = head.Next;
            var e = new Element(value, last, head);
            last.Prev = e;
            head.Next = e;
        }
    }

    public T Pop()
    {
        head = head.Next;
        return Shift();
    }

    public void Unshift(T value)
    {
        Push(value);
        head = head.Next;
    }

    public T Shift()
    {
        var value = head.Value;
        var last = head.Next;

        if (last == head)
            head = null;
        else
        {
            last.Prev = head.Prev;
            head.Prev.Next = last;
            head = head.Prev;
        }

        return value;
    }

    private class Element
    {
        public readonly T Value;
        public Element Next;
        public Element Prev;

        public Element(T value, Element next = null, Element prev = null)
        {
            Value = value;
            Next = next ?? this;
            Prev = prev ?? this;
        }
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.6.20 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.19 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.18 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.17 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.16 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.15 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.14 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.13 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.12 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.11 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.10 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.9 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.8 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.7 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.6 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.5 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.4 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.3 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.2 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.6.1 tracks/csharp/exercises/linked-list/Example.cs