AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

ALDS1_3_C: Doubly Linked List


問題へのリンク


C#のソース

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

// Q009 双方向連結リスト https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("7");
            WillReturn.Add("insert 5");
            WillReturn.Add("insert 2");
            WillReturn.Add("insert 3");
            WillReturn.Add("insert 1");
            WillReturn.Add("delete 3");
            WillReturn.Add("insert 6");
            WillReturn.Add("delete 5");
            //6 1 2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("9");
            WillReturn.Add("insert 5");
            WillReturn.Add("insert 2");
            WillReturn.Add("insert 3");
            WillReturn.Add("insert 1");
            WillReturn.Add("delete 3");
            WillReturn.Add("insert 6");
            WillReturn.Add("delete 5");
            WillReturn.Add("deleteFirst");
            WillReturn.Add("deleteLast");
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        var InsLinkedList = new LinkedList<int>();

        foreach (string EachStr in InputList.Skip(1)) {
            string[] SplitArr = EachStr.Split(' ');
            if (SplitArr[0] == "insert") {
                InsLinkedList.AddFirst(int.Parse(SplitArr[1]));
            }
            if (SplitArr[0] == "delete") {
                InsLinkedList.Remove(int.Parse(SplitArr[1]));
            }
            if (SplitArr[0] == "deleteFirst") {
                InsLinkedList.RemoveFirst();
            }
            if (SplitArr[0] == "deleteLast") {
                InsLinkedList.RemoveLast();
            }
        }
        int[] wkArr = InsLinkedList.ToArray();
        Console.WriteLine(string.Join(" ", Array.ConvertAll(wkArr, pX => pX.ToString())));
    }
}


解説

LinkedListジェネリックを使ってます。