28279번: 덱 2 | Baekjoon Online Judge

문제

정수를 저장하는 덱을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 여덟 가지이다.

  1. 1 X: 정수 X를 덱의 앞에 넣는다. (1 ≤ X ≤ 100,000)
  2. 2 X: 정수 X를 덱의 뒤에 넣는다. (1 ≤ X ≤ 100,000)
  3. 3: 덱에 정수가 있다면 맨 앞의 정수를 빼고 출력한다. 없다면 -1을 대신 출력한다.
  4. 4: 덱에 정수가 있다면 맨 뒤의 정수를 빼고 출력한다. 없다면 -1을 대신 출력한다.
  5. 5: 덱에 들어있는 정수의 개수를 출력한다.
  6. 6: 덱이 비어있으면 1, 아니면 0을 출력한다.
  7. 7: 덱에 정수가 있다면 맨 앞의 정수를 출력한다. 없다면 -1을 대신 출력한다.
  8. 8: 덱에 정수가 있다면 맨 뒤의 정수를 출력한다. 없다면 -1을 대신 출력한다.

입력

첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000)

둘째 줄부터 N개 줄에 명령이 하나씩 주어진다.

출력을 요구하는 명령은 하나 이상 주어진다.

출력

출력을 요구하는 명령이 주어질 때마다 명령의 결과를 한 줄에 하나씩 출력한다.

예제 입력 1 복사

11
6
1 3
1 8
7
8
3
2 5
1 2
5
4
4

예제 출력 1 복사

1
8
3
8
3
5
3

출처

https://www.acmicpc.net/problem/28279

C# 13

using System.Text;

static int Input(string str, int min, int max) =>
    int.TryParse(str, out var result) && result >= min && result <= max ? result : throw new("Invalid number");

var dq = new Deque<int>();

var count = Input(Console.ReadLine() ?? "", 1, 1_000_000);
var output = new StringBuilder();
while (count > 0)
{
    var temp = (Console.ReadLine() ?? "").Split(' ', StringSplitOptions.RemoveEmptyEntries);
    if (temp.Length > 2)
        throw new("Invalid input");
    
    var command = temp[0];

    if (command is "1")
    {
        var value = Input(temp[1], 1, 100_000);
        dq.PushHead(value);
    }
    else if (command is "2")
    {
        var value = Input(temp[1], 1, 100_000);
        dq.PushTail(value);
    }
    else if (command is "3")
    {
        output.AppendLine(dq.PopHead().ToString());
    }
    else if (command is "4")
    {
        output.AppendLine(dq.PopTail().ToString());
    }
    else if (command is "5")
    {
        output.AppendLine(dq.Count.ToString());
    }
    else if (command is "6")
    {
        output.AppendLine((dq.IsEmpty ? 1 : 0).ToString());
    }
    else if (command is "7")
    {
        output.AppendLine(dq.PeekHead().ToString());
    }
    else if (command is "8")
    {
        output.AppendLine(dq.PeekTail().ToString());
    }

    count--;
}

Console.WriteLine(output);


class Deque<T>
{
    class Node(T value, Node? prev = null, Node? next = null)
    {
        public T Value { get; } = value;
        public Node? Prev { get; set; } = prev;
        public Node? Next { get; set; } = next;
    }

    private Node? _head;
    private Node? _tail;
    private int _count;

    public int Count => _count;
    public bool IsEmpty => _count is 0;
    public T PeekHead() => (_head ?? throw new InvalidOperationException("Deque is empty")).Value;
    public T PeekTail() => (_tail ?? throw new InvalidOperationException("Deque is empty")).Value;

    public void PushTail(T value)
    {
        var node = new Node(value, prev: _tail);
        if (_tail is not null)
        {
            _tail.Next = node;
        }
        else
        {
            _head = node;
        }
        _tail = node;
        _count++;
    }

    public void PushHead(T value)
    {
        var node = new Node(value, next: _head);
        if (_head is not null)
        {
            _head.Prev = node;
        }
        else
        {
            _tail = node;
        }
        _head = node;
        _count++;
    }

    public T PopHead()
    {
        if (_head is null)
        {
            throw new InvalidOperationException("Deque is empty");
        }
        var value = _head.Value;
        _head = _head.Next;
        if (_head is null)
        {
            _tail = null;
        }
        else
        {
            _head.Prev = null;
        }
        _count--;
        return value;
    }

    public T PopTail()
    {
        if (_tail is null)
        {
            throw new InvalidOperationException("Deque is empty");
        }
        var value = _tail.Value;
        _tail = _tail.Prev;
        if (_tail is null)
        {
            _head = null;
        }
        else
        {
            _tail.Next = null;
        }
        _count--;
        return value;
    }
}