Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            CircularLinkedList <string> circularList = new CircularLinkedList <string>();

            circularList.Added   += (object sender, AddToCollection <string> argument) => { Console.WriteLine(argument.Message); };
            circularList.Removed += (object sender, RemFromCollection <string> argument) => { Console.WriteLine(argument.Message); };

            circularList.Add("1");
            circularList.Add("2");
            circularList.Add("3");
            circularList.Add("4");
            foreach (var item in circularList)
            {
                Console.WriteLine(item);
            }

            circularList.Remove("2");
            Console.WriteLine("\n После удаления: \n");
            foreach (var item in circularList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("\n Содержит 3: \n");
            bool b = circularList.Contains("3");

            Console.WriteLine(b);
        }
        public void RemoveFirst()
        {
            CircularLinkedList <int> cll = new CircularLinkedList <int>();

            cll.Add((10));
            cll.Add((20));
            cll.Add((30));
            cll.Add((40));

            /*  40 -> 30 -> 20 - > 10
             *   ^                  |
             *   |__________________|        Head = 40, Tail = 10
             */

            cll.RemoveFirst();

            /*  30 -> 20 - > 10
             *   ^            |
             *   |____________|        Head = 40, Tail = 10
             */

            Assert.AreEqual(cll.Head.Value, 30);
            Assert.AreEqual(cll.Tail.Value, 10);
            Assert.AreEqual(cll.Tail.Next.Value, 30);
        }
 private void AddWords(CircularLinkedList <string> list)
 {
     list.Add("Ndumiso Chamane");
     list.Add("Mfundo Mkhize");
     list.Add("Ntuthuko Cele");
     list.Add("Njabulo Khuzwayo");
 }
Exemplo n.º 4
0
        public static long Puzzle2()
        {
            var cups  = 1000000;
            var moves = 10000000;

            var nodes = new Node[cups];

            for (long i = 0; i < cups; ++i)
            {
                nodes[i] = new Node(i + 1);
            }

            var list = new CircularLinkedList(nodes);

            foreach (var i in PuzzleInput())
            {
                list.Add(nodes[i - 1]);
            }
            for (long i = 10; i <= cups; ++i)
            {
                list.Add(nodes[i - 1]);
            }

            list.RunMoves(moves, cups);

            list.Start = list[1];

            return(list
                   .Skip(1)
                   .Take(2)
                   .Select(i => i.Value)
                   .Aggregate((a, b) => a * b));
        }
        public void RemoveLastAndEmptyList()
        {
            CircularLinkedList <int> cll = new CircularLinkedList <int>();

            cll.Add((10));
            cll.Add((20));
            cll.Add((30));
            cll.Add((40));

            /*  40 -> 30 -> 20 - > 10
             *   ^                  |
             *   |__________________|        Head = 40, Tail = 10
             */

            cll.RemoveLast();
            cll.RemoveLast();
            cll.RemoveLast();
            cll.RemoveLast();

            /*
             *  NULL, Count should be 0
             */

            Assert.AreEqual(cll.Head, null);
            Assert.AreEqual(cll.Count, 0);
        }
Exemplo n.º 6
0
        public void CircularLinkedList()
        {
            // Arrange
            var circularLinkedList = new CircularLinkedList <int>();

            // Act
            circularLinkedList.Add(1);
            circularLinkedList.Add(2);
            circularLinkedList.Add(3);
            circularLinkedList.Add(4);
            var count1 = circularLinkedList.Count;

            circularLinkedList.Delete(3);
            var count2 = circularLinkedList.Count;

            var count = 0;

            for (var i = 0; i < circularLinkedList.Count * 2; i++)
            {
                count++;
            }

            // Assert
            Assert.Equal(4, count1);
            Assert.Equal(3, count2);
            Assert.Equal(circularLinkedList.Count * 2, count);
        }
Exemplo n.º 7
0
 public Rover()
 {
     Area       = new Size(5, 5);
     Directions = new CircularLinkedList <Direction>();
     Directions.Add(Direction.NORTH);
     Directions.Add(Direction.EAST);
     Directions.Add(Direction.SOUTH);
     Directions.Add(Direction.WEST);
 }
        public void AddTest()
        {
            //Arrange
            var list = new CircularLinkedList <int>();

            //Act
            list.Add(1);
            list.Add(2);
            list.Add(3);

            //Assert
            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(2, list.Head.NextCell.Data);
            Assert.AreEqual(3, list.Head.PrevCell.Data);
        }
Exemplo n.º 9
0
 private void BuildReviewers(List <string> reviewers)
 {
     foreach (string reviewer in reviewers)
     {
         _reviewers.Add(new Reviewer(reviewer));
     }
 }
Exemplo n.º 10
0
        public void MoveNextTest()
        {
            int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            CircularLinkedList <int> list = new CircularLinkedList <int>();

            foreach (int a in arr)
            {
                list.Add(a);
            }
            IEnumerator enumerator = list.GetEnumerator();

            for (int i = 0; i < arr.Length * 2; i++)
            {
                enumerator.MoveNext();

                if (i >= arr.Length)
                {
                    Assert.AreEqual(arr[i - arr.Length], (int)enumerator.Current);
                }
                else
                {
                    Assert.AreEqual(arr[i], (int)enumerator.Current);
                }
            }
        }
 // Testing the Add method.
 private void AddNumbers(CircularLinkedList <int> list)
 {
     for (int i = 1; i < 10; i++)
     {
         list.Add(i);
     }
 }
Exemplo n.º 12
0
    //拉丁方阵问题
    void LatinSquare(int count)
    {
        CircularLinkedList <int> numbers = new CircularLinkedList <int>();

        for (int i = 0; i < count; i++)
        {
            numbers.Add(i + 1);
        }

        string log = "";

        for (int i = 0; i < count; i++)
        {
            int row = 0;
            while (row < count)
            {
                log += numbers.Move() + " ,";
                row++;
            }
            //新的一行从下一个位置开始
            numbers.Move();
            log += "\n";
        }

        Debug.LogError(log);
    }
Exemplo n.º 13
0
    string Vigenere(string input)
    {
        int[] secret = new int[] { 1, 27, 2, 2 };
        CircularLinkedList <char> letters = new CircularLinkedList <char>();
        char a = 'a';

        //赋值26个字母
        for (int i = 0; i < 26; i++)
        {
            letters.Add(a++);
        }

        string result = "";

        //开始解密
        for (int i = 0; i < input.Length; i++)
        {
            char decode = letters.MoveAt(letters.IndexOf(input[i]));
            for (int j = 0; j < secret[i]; j++)
            {
                decode = letters.Move();
            }

            result += decode;
        }
        Debug.LogError(result);
        return(result);
    }
 private void AddAlphabets(CircularLinkedList <char> list)
 {
     list.Add('$');
     list.Add('!');
     list.Add('_');
     list.Add(';');
     list.Add('*');
     list.Add('@');
     list.Add('_');
     list.Add('/');
 }
        public void RemoveTest()
        {
            //Arrange
            var list = new CircularLinkedList <int>();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);

            //Act
            list.Remove(2);

            //Assert
            Assert.AreEqual("1 3 4 5 ", list.ToString());
            list.Remove(1);
            Assert.AreEqual("3 4 5 ", list.ToString());
        }
Exemplo n.º 16
0
        public void Add_AddNullElement_ThrowsArgumentNullException()
        {
            //arrange
            CircularLinkedList <string> circularList = new CircularLinkedList <string>()
            {
                "2", "2", "5", "8", "1", "6"
            };

            //act & assert
            Assert.Throws <ArgumentNullException>(() => circularList.Add((string)null));
        }
Exemplo n.º 17
0
        public void Clearing(IEnumerable <int> data)
        {
            var linkedList = new CircularLinkedList <int>();

            foreach (var item in data)
            {
                linkedList.Add(item);
            }
            linkedList.Clear();
            Assert.AreEqual(0, linkedList.Count);
            Assert.AreEqual(null, linkedList.First);
        }
Exemplo n.º 18
0
        //出圈的人用OutValue表示
        public static void JosephusStart2(int n, int s, int d)
        {
            int OutValue = 0;
            //创建包含所有人的一个链表
            CircularLinkedList <int> person = new CircularLinkedList <int>();

            for (int i = 1; i <= n; i++)
            {
                person.Add(i);
            }

            //定位到开始的那个人
            SingleLinkedNode <int> start_p = person.Head;

            for (int i = 0; i < s; i++)
            {
                start_p = start_p.Next;
            }

            int           cnt = 0, total = person.Count;
            StringBuilder out_sequence = new StringBuilder();

            while (total > 1)
            {
                start_p = start_p.Next;
                if (start_p.Item != OutValue)
                {
                    cnt++;
                }
                if ((cnt != 0) && (cnt % d == 0))//有一个人出圈
                {
                    out_sequence.Append(start_p.Item + " -> ");
                    start_p.Item = OutValue;
                    while (start_p.Item == 0)
                    {
                        start_p = start_p.Next;
                    }
                    cnt = 0;
                    total--;
                    person.Show();
                    Console.WriteLine();
                }
            }
            //只剩最后一个人
            while (start_p.Item == 0)
            {
                start_p = start_p.Next;
            }
            out_sequence.Append(start_p.Item);
            Console.WriteLine("Out sequence:");
            Console.WriteLine(out_sequence.ToString());
        }
Exemplo n.º 19
0
        public void TestAddingItem()
        {
            var list = new CircularLinkedList <int>();

            for (var i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            Assert.AreEqual(list.Head.Value, 0);
            Assert.AreEqual(list.Tail.Value, 9);
            Assert.AreEqual(list.Current.Value, 0);
        }
Exemplo n.º 20
0
        private static void ex8()
        {
            Random rnd = new Random();

            Console.Write("Введите размерность матрицы: ");
            int a = Convert.ToInt32(Console.ReadLine());

            int[,] matrix  = new int[a, a];
            int[,] matrix2 = new int[a, a];
            int[,] array   = new int[a, a];
            int[,] mT      = new int[a, a];
            int[,] mT2     = new int[a, a];
            CircularLinkedList <int> linkedList = new CircularLinkedList <int>();

            Console.WriteLine("Первая матрица: \n");
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    matrix[rnd.Next(0, a - 1), rnd.Next(0, a - 1)] = rnd.Next(0, 2);
                    Console.Write("{0} ", matrix[i, j]);
                    linkedList.Add(matrix[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("Вторая матрица: \n");
            for (int i = 0; i < matrix2.GetLength(0); i++)
            {
                for (int j = 0; j < matrix2.GetLength(1); j++)
                {
                    matrix2[rnd.Next(0, a - 1), rnd.Next(0, a - 1)] = rnd.Next(0, 2);
                    Console.Write("{0} ", matrix2[i, j]);
                }
                Console.WriteLine();
            }
            int[,] arrayOutput = new int[a, a];
            Console.WriteLine("Сумма матриц: \n");
            arrayOutput = sum(matrix, matrix2, a);
            Console.WriteLine("Умножение матриц: \n");
            array = multi(matrix, matrix2, a);
            Console.WriteLine("Транспонирования первой матрицы: \n");
            mT = trans(matrix, a);
            Console.WriteLine("Транспонирования второй матрицы: \n");
            mT2 = trans2(matrix2, a);
            foreach (char c in linkedList)
            {
                Console.WriteLine(c);
            }
            Console.ReadKey();
        }
Exemplo n.º 21
0
    private void Start()
    {
        //_singleList = new SingleLinkedList<string>();
        //for (int i = 0; i < 5; i++)
        //{
        //    _singleList.Insert(0, i.ToString());
        //}

        _cList = new CircularLinkedList <string>();
        for (int i = 0; i < 5; i++)
        {
            _cList.Add(i.ToString());
        }
    }
        public void RemoveFirstandMakeEmpty()
        {
            CircularLinkedList <int> cll = new CircularLinkedList <int>();

            cll.Add((10));
            cll.Add((20));


            /*  20 - > 10
             *   ^       |
             *   |_______|        Head = 20, Tail = 10
             */

            cll.RemoveFirst();
            cll.RemoveFirst();

            /*
             *   NULL & Count should be 0
             *
             */

            Assert.AreEqual(cll.Head, null);
            Assert.AreEqual(cll.Count, 0);
        }
Exemplo n.º 23
0
        /// <summary>
        /// 循环单链表
        /// </summary>
        public static void CircularLinkedListData()
        {
            CircularLinkedList <int> circularLinkedList = new CircularLinkedList <int>();
            Random random = new Random();

            for (int i = 0; i < 5; i++)
            {
                int num = random.Next(1, 100);
                circularLinkedList.Add(num);
                Console.WriteLine($"循环单链表添加成功{num}");
            }
            Console.WriteLine(circularLinkedList.GetAllNodes());
            circularLinkedList.RemoveAt(3);
            Console.WriteLine("移除循环单链表索引3的值");
            Console.WriteLine(circularLinkedList.GetAllNodes());
        }
Exemplo n.º 24
0
        public void Add_CheckThatEventRaisedWhenAddingAnItem()
        {
            //arrange
            CircularLinkedList <string> circularList = new CircularLinkedList <string>()
            {
                "2", "2", "5", "8", "1", "6"
            };
            //act
            var receivedEvent = Assert.Raises <AddToCollection <string> >(
                a => circularList.Added += a,
                a => circularList.Added -= a,
                () => { circularList.Add("0"); });

            //assert
            Assert.NotNull(receivedEvent);
            Assert.Equal("0", receivedEvent.Arguments.AddItem);
            Assert.Equal("0 was added", receivedEvent.Arguments.Message);
        }
Exemplo n.º 25
0
        public void TestMovingToSpecificItem()
        {
            var list = new CircularLinkedList <int>();

            for (var i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            Assert.AreEqual(list.Head.Value, 0);
            Assert.AreEqual(list.Tail.Value, 9);

            Assert.AreEqual(list.Current.Value, 0);

            list.GoTo(5);

            Assert.AreEqual(list.Current.Value, 5);
        }
Exemplo n.º 26
0
        public static string Puzzle1()
        {
            var cups  = 9;
            var moves = 100;

            var nodes = Enumerable.Range(1, cups).Select(i => new Node(i)).ToArray();

            var list = new CircularLinkedList(nodes);

            foreach (var i in PuzzleInput())
            {
                list.Add(nodes[i - 1]);
            }

            list.RunMoves(moves, cups);

            list.Start = list[1];
            return(list.Skip(1).Select(n => n.Value.ToString()).Aggregate((a, b) => a + b));
        }
Exemplo n.º 27
0
        //出圈的人直接从链表中去掉
        public static void JosephusStart1(int n, int s, int d)
        {
            //创建包含所有人的一个链表
            CircularLinkedList <int> person = new CircularLinkedList <int>();

            for (int i = 1; i <= n; i++)
            {
                person.Add(i);
            }

            //定位到开始的那个人
            SingleLinkedNode <int> start_p = person.Head;

            for (int i = 0; i < s; i++)
            {
                start_p = start_p.Next;
            }

            int           cnt          = 0;
            StringBuilder out_sequence = new StringBuilder();

            while (person.Count > 1)
            {
                start_p = start_p.Next;
                cnt++;
                if ((cnt != 0) && (cnt % d == 0))
                {
                    //利用cnt暂时存放一下出圈人的编号,可以避免新建一个变量
                    cnt     = start_p.Item;
                    start_p = start_p.Next;
                    out_sequence.Append(cnt + " -> ");
                    person.Remove(cnt);
                    cnt = 0;
                    person.Show();
                    Console.WriteLine();
                }
            }
            //只剩最后一个人
            out_sequence.Append(person.Head.Next.Item);
            Console.WriteLine("Out sequence:");
            Console.WriteLine(out_sequence.ToString());
        }
Exemplo n.º 28
0
    private void OnGUI()
    {
        insertValue = GUI.TextField(new Rect(110, 40, 80, 50), insertValue);

        indexValue = GUI.TextField(new Rect(110, 140, 80, 50), indexValue);
        if (GUI.Button(new Rect(20, 140, 80, 50), "指定插入"))
        {
            _singleList.Insert(int.Parse(indexValue), insertValue);
            indexValue = "";
        }

        if (GUI.Button(new Rect(20, 240, 80, 50), "获取"))
        {
            string s = _singleList[int.Parse(indexValue)];
            Debug.LogError(s);
            indexValue = "";
        }

        //indexValue = GUI.TextField(new Rect(110, 340, 80, 50), indexValue);
        if (GUI.Button(new Rect(20, 340, 80, 50), "移除"))
        {
            _singleList.RemoveAt(int.Parse(indexValue));
            indexValue = "";
        }

        if (GUI.Button(new Rect(20, 440, 80, 50), "移除元素"))
        {
            _cList.Delete(insertValue);
            indexValue = "";
        }

        if (GUI.Button(new Rect(110, 440, 80, 50), "尾插"))
        {
            _cList.Add(insertValue);
            indexValue = "";
        }

        if (GUI.Button(new Rect(400, 140, 80, 50), "打印"))
        {
            _cList.LogList();
        }
    }
Exemplo n.º 29
0
        public void Adding(int[] data)
        {
            var linkedList = new CircularLinkedList <int>();
            int count      = 0;

            foreach (var item in data)
            {
                linkedList.Add(item);
                count++;
            }
            Assert.AreEqual(count, linkedList.Count);
            Assert.AreNotEqual(null, linkedList.First);
            var enumerator = data.Reverse().GetEnumerator();

            enumerator.MoveNext();
            foreach (var item in linkedList)
            {
                Assert.AreEqual(enumerator.Current, item);
                enumerator.MoveNext();
            }
        }
Exemplo n.º 30
0
        static void Main(string[] args)
        {
            CircularLinkedList <string> members = new CircularLinkedList <string>();
            string path = @"C:\Users\MSI\source\repos\Lab9 (3)\Lab9 (3)\List\Имена.txt";

            using (var fileRead = new StreamReader(path))
            {
                while (fileRead.Peek() > -1)
                {
                    members.Add(fileRead.ReadLine());
                }
            }
            foreach (string member in members)
            {
                Console.Write(member + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Введите человека с кого начать считалку?");
            string name = "";

            while (!members.Contains(name))
            {
                name = Console.ReadLine();
                if (!members.Contains(name))
                {
                    Console.WriteLine("Имя не найдено введите из списка:");
                    foreach (string member in members)
                    {
                        Console.Write(member + " ");
                    }
                    Console.WriteLine();
                }
            }
            Console.WriteLine("Введите считалочку");
            string text = Console.ReadLine();

            string[] word = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            members.Swap(name);
            Сhoice(members, word.Length - 1);
        }