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.2.1.180 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/linked-list/Example.cs