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.0.5 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.0.4 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.0.3 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.0.2 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.0.1 tracks/csharp/exercises/linked-list/Example.cs
trackler-2.0.0.0 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.4.1 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.4.0 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.3.0 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.2.1 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.2.0 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.1.2 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.1.1 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.1.0 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.0.1 tracks/csharp/exercises/linked-list/Example.cs
trackler-1.0.0 tracks/csharp/exercises/linked-list/Example.cs