예제 #1
0
        public void AddFiveItemsAndVerify()
        {
            int linkedListStackLast = 0;
            int arrayStackLast      = 0;

            for (int i = 1; i <= 5; i++)
            {
                linkedListStack.Push(i);
                arrayStack.Push(i);
            }

            Assert.AreEqual(linkedListStack.Peek(), 5, "invalid top element");
            Assert.AreEqual(linkedListStack.Count, 5, "invalid count");

            Assert.AreEqual(arrayStack.Peek(), 5, "invalid top element");
            Assert.AreEqual(arrayStack.Count, 5, "invalid count");

            while (linkedListStack.Count > 0)
            {
                linkedListStackLast = linkedListStack.Pop();
                arrayStackLast      = arrayStack.Pop();
            }
            Assert.AreEqual(linkedListStackLast, 1, "invalid last element");
            Assert.AreEqual(arrayStackLast, 1, "invalid last element");
        }
        public void LinkedListStackTest()
        {
            var stack = new LinkedListStack <int>();

            stack.Push(1);
            stack.Push(2);

            Assert.IsTrue(stack.ItemCount == 2);
            Assert.IsTrue(stack.IsEmpty() == false);
            Assert.IsTrue(stack.Peek() == 2);
            Assert.IsTrue(stack.Pop() == 2);
            Assert.IsTrue(stack.ItemCount == 1);
            stack.Pop();
            Assert.IsTrue(stack.IsEmpty());

            for (int i = 0; i < 1000; i++)
            {
                stack.Push(i);
            }

            Assert.IsTrue(stack.ItemCount == 1000);

            for (int i = 0; i < 1000; i++)
            {
                stack.Pop();
            }

            Assert.IsTrue(stack.ItemCount == 0);
        }
예제 #3
0
        public void LinkedListStackWorksInLifoOrder()
        {
            var sut = new LinkedListStack <int>();

            sut.Push(1);
            sut.Push(2);
            Assert.Equal(2, sut.Pop());
        }
        public void Peek_PushTwoItemsAndPop_ReturnsHeadElement()
        {
            var stack = new LinkedListStack <int>();

            stack.Push(1);
            stack.Push(2);
            stack.Pop();
            Assert.AreEqual(1, stack.Peek());
        }
예제 #5
0
        public void LinkedListStackIsEnptyWhenAllElementsAreRemoved()
        {
            var sut = new LinkedListStack <int>();

            sut.Push(1);
            sut.Push(2);
            Assert.Equal(2, sut.Pop());
            Assert.Equal(1, sut.Pop());
            Assert.True(sut.IsEmpty);
        }
예제 #6
0
        public void LinkedListStackIncreasesCapacityOnDemand()
        {
            var sut = new LinkedListStack <int>();

            sut.Push(1);
            sut.Push(2);
            Assert.Equal(2, sut.Pop());
            Assert.Equal(1, sut.Pop());
            Assert.True(sut.IsEmpty);
        }
예제 #7
0
        [Test] // 1.3.12
        public void CopyStack()
        {
            var input = new LinkedListStack <string>();

            input.Push("three");
            input.Push("two");
            input.Push("one");
            Assert.AreEqual(new string[] { "one", "two", "three" }, input.ToArray());

            var output = CopyStack(input);

            Assert.AreEqual(output.ToArray(), input.ToArray());
        }
예제 #8
0
        public void LinkedLinkStack()
        {
            var stack = new LinkedListStack <string>();

            stack.Push("turtles");
            stack.Push("all");
            stack.Push("the");
            stack.Push("way");
            stack.Push("down");

            Assert.AreEqual("down", stack.Pop());
            Assert.AreEqual("way", stack.Pop());
            Assert.AreEqual("the", stack.Pop());
            Assert.AreEqual("all", stack.Pop());
            Assert.AreEqual("turtles", stack.Pop());
        }
예제 #9
0
파일: Answer2.cs 프로젝트: v4karuna/xjvb3r
        public void StackTest()
        {
            var stack = new LinkedListStack <int>();

            //insert 1 to 10 into stack
            for (int i = 1; i <= 10; i++)
            {
                stack.Push(i);
            }

            //size should be 10
            Assert.Equal(10, stack.Size());

            //pop items and assert to ensure FIFO
            for (int i = 10; i > 0; i--)
            {
                var value = stack.Pop();
                Assert.Equal(i, value);
            }

            //size should be 0
            Assert.Equal(0, stack.Size());

            //poping on empty stack should throw an exception
            Assert.Throws <InvalidOperationException>(() => stack.Pop());
        }
예제 #10
0
 private void GeneratePopulatedExample()
 {
     PopulatedLinkedListStack = new LinkedListStack <string>();
     foreach (var item in Items)
     {
         PopulatedLinkedListStack.Push(item);
     }
 }
        public void Count_PushOneItem_ReturnsOne()
        {
            var stack = new LinkedListStack <int>();

            stack.Push(1);
            Assert.AreEqual(1, stack.Count);
            Assert.IsFalse(stack.IsEmpty);
        }
예제 #12
0
파일: Stack.cs 프로젝트: FOSS-UCSC/FOSSALGO
    public static void Main(string[] args)
    {
        var llStack = new LinkedListStack();

        llStack.Print();
        llStack.Pop();
        llStack.Push(30);
        llStack.Push(20);
        llStack.Print();
        llStack.Push(50);
        llStack.Push(120);
        llStack.Print();
        llStack.Pop();
        llStack.Pop();
        llStack.Print();
        llStack.Search(20);
    }
예제 #13
0
        public void LinkedListStackPushTest()
        {
            var linkedListStackStack = new LinkedListStack <int>();

            for (var i = 0; i < Elements; i++)
            {
                linkedListStackStack.Push(i * 10);
            }
            Assert.AreEqual(1000000000, linkedListStackStack.Peek());
        }
예제 #14
0
        public decimal EvaluatePostfix(string input)
        {
            LinkedListStack <decimal> stack = new LinkedListStack <decimal>();

            foreach (var i in input)
            {
                if (IsOperator(i))
                {
                    decimal result;
                    var     right = decimal.Parse(stack.Pop().ToString());
                    var     left  = decimal.Parse(stack.Pop().ToString());
                    switch (i)
                    {
                    case '+':
                        result = left + right;
                        break;

                    case '-':
                        result = left - right;
                        break;

                    case '*':
                        result = left * right;
                        break;

                    case '/':
                        result = left / right;
                        break;

                    default:
                        result = 0;
                        break;
                    }
                    stack.Push(result);
                }
                else
                {
                    stack.Push(decimal.Parse(i.ToString()));
                }
            }
            return(stack.First());
        }
예제 #15
0
            public void Push(T val)
            {
                T min = val, max = val;

                if (!IsEmpty)
                {
                    min = min.CompareTo(Min) < 0 ? min : Min;
                    max = max.CompareTo(Max) > 0 ? max : Max;
                }
                _stack.Push(new MinMaxStackItem(min, max, val));
            }
예제 #16
0
        public override void Init()
        {
            _arrayStack           = new ArrayStack <int>(100);
            _linkedListStackStack = new LinkedListStack <int>();

            for (var i = 0; i < Elements; i++)
            {
                _arrayStack.Push(i * 10);
                _linkedListStackStack.Push(i * 10);
            }

            base.Init();
        }
예제 #17
0
        private string ShuntingYard(string input)
        {
            LinkedListStack <char> operators = new LinkedListStack <char>();
            Queue <char>           output    = new LinkedListQueue <char>();

            foreach (var i in input)
            {
                if (Char.IsNumber(i))
                {
                    output.Enqueue(i);
                }
                if (IsOperator(i))
                {
                    while (!operators.IsEmpty() && OperatorPrecedence(operators.Peek()) >= OperatorPrecedence(i))
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Push(i);
                }
                if (i == '(')
                {
                    operators.Push(i);
                }
                if (i == ')')
                {
                    while (operators.Peek() != '(')
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Pop();
                }
            }
            while (!operators.IsEmpty())
            {
                output.Enqueue(operators.Pop());
            }

            return(new string(output.ToArray()));
        }
예제 #18
0
        public void TestLinkedListStack()
        {
            var stack = new LinkedListStack <int>();

            for (var i = 0; i < 5; i++)
            {
                stack.Push(i);
                Console.WriteLine(stack);
            }

            stack.Pop();
            Console.WriteLine(stack);
        }
예제 #19
0
파일: Program.cs 프로젝트: viaches/leetcode
        static void Main(string[] args)
        {
            var arrayStack = new ArrayStack <string>();

            arrayStack.Push("first");
            arrayStack.Push("second");
            arrayStack.Push("third");
            arrayStack.Push("forth");
            arrayStack.Pop();
            arrayStack.Pop();
            arrayStack.Pop();

            var linkedListStack = new LinkedListStack <string>();

            linkedListStack.Push("first");
            linkedListStack.Push("second");
            linkedListStack.Push("third");
            linkedListStack.Push("forth");
            linkedListStack.Pop();
            linkedListStack.Pop();
            linkedListStack.Pop();
        }
예제 #20
0
        internal LinkedListStack <Node> GetNodesForMessage(RelayMessage message)
        {
            if (!Activated)
            {
                return(new LinkedListStack <Node>());
            }

            NodeCluster            cluster;
            LinkedListStack <Node> nodes;

            //messages that, from out of system, go to each cluster
            if (message.IsClusterBroadcastMessage)
            {
                if (MyCluster == null)                 //out of system, each cluster
                {
                    nodes = new LinkedListStack <Node>();
                    for (int clusterIndex = 0; clusterIndex < Clusters.Count; clusterIndex++)
                    {
                        nodes.Push(Clusters[clusterIndex].GetNodesForMessage(message));
                    }
                }
                else                 //in system, my cluster
                {
                    nodes = MyCluster.GetNodesForMessage(message);
                }
            }
            else
            {
                //messages that route to one modded cluster
                //to modded cluster in group
                cluster = GetClusterForId(message.Id, message.IsInterClusterMsg);
                if (cluster != null)
                {
                    if (message.IsInterClusterMsg && cluster.MeInThisCluster)
                    {
                        nodes = new LinkedListStack <Node>();
                        nodes.Push(cluster.Me);
                    }
                    else
                    {
                        nodes = cluster.GetNodesForMessage(message);
                    }
                }
                else
                {
                    nodes = new LinkedListStack <Node>();
                }
            }
            return(nodes);
        }
예제 #21
0
        static void Main(string[] args)
        {
            LinkedList      list  = new LinkedList();
            LinkedListStack stack = new LinkedListStack();
            LinkedListQueue queue = new LinkedListQueue();

            list.Add(56);
            list.Add(70);
            list.Add(90);
            list.Add(12);
            list.Add(30);
            list.Add(60);
            list.AddInReverseOrder(22);
            list.AddInReverseOrder(44);
            list.InsertAtParticularPosition(2, 12);
            list.Display();
            list.Total();
            list.DeleteFirst();
            list.DeleteLast();
            list.SearchValueInList(90);
            stack.Push(10);
            stack.Push(20);
            stack.Push(30);
            stack.Push(40);
            stack.Push(50);
            stack.Display();
            stack.Peek();
            stack.Pop();
            stack.IsEmpty();
            queue.Enqueue(12);
            queue.Enqueue(24);
            queue.Enqueue(36);
            queue.Enqueue(48);
            queue.Dequeue();
            queue.Display();
            Console.ReadKey();
        }
예제 #22
0
        private string InsertParens(string input)
        {
            LinkedListStack <string> stack = new LinkedListStack <string>();

            foreach (var i in input)
            {
                if (i.Equals(')'))
                {
                    string expression = ")";
                    for (var j = 0; j < 3; j++)
                    {
                        expression = stack.Pop() + expression;
                    }
                    expression = "(" + expression;
                    stack.Push(expression);
                }
                else
                {
                    stack.Push(i.ToString());
                }
            }

            return(stack.First());
        }
예제 #23
0
        private Stack <string> CopyStack(Stack <string> source)
        {
            var reverse = new LinkedListStack <string>();
            var output  = new LinkedListStack <string>();

            foreach (var item in source)
            {
                reverse.Push(item);
            }

            foreach (var item in reverse)
            {
                output.Push(item);
            }
            return(output);
        }
        public void LinkedListStack()
        {
            Random random = new Random();
            LinkedListStack <string> Stacklist = new LinkedListStack <string>();

            for (int i = 0; i < CountItems; i++)
            {
                Stacklist.Push($"{i.ToString()}");
            }

            for (int i = 0; i < CountItems; i++)
            {
                if (random.Next(i - RndMax, i + RndMin) == i)
                {
                    Stacklist.Pop();
                }
            }
        }
예제 #25
0
        public static void LinkedListStack()
        {
            LinkedListStack <int> linkedListStack = new LinkedListStack <int>();

            linkedListStack.Push(10);
            linkedListStack.Push(9);
            linkedListStack.Push(8);
            linkedListStack.Push(7);
            linkedListStack.Push(6);
            linkedListStack.Push(5);
            linkedListStack.PrintAll();
            linkedListStack.Pop();
            linkedListStack.Pop();
            linkedListStack.PrintAll();
            Console.Read();
        }
예제 #26
0
        internal LinkedListStack <Node> GetNodesForMessage(RelayMessage message)
        {
            LinkedListStack <Node> nodes;
            Node node;

            if (message.IsTwoWayMessage)
            {
                //messages that always go to a selected node
                nodes = new LinkedListStack <Node>();
                node  = GetChosenNode();
                if (node != null)
                {
                    nodes.Push(node);                      // this is where the single node is selected and returned for OUT messages
                }
            }
            else
            {
                //messages that are routed
                //  out of system: to a selected node
                //  in system: to every other node
                if (Me == null)                 //out of system
                {
                    nodes = new LinkedListStack <Node>();
                    node  = GetChosenNode();
                    if (node != null)
                    {
                        nodes.Push(node);
                    }
                }
                else                 //in system
                {
                    nodes = SelectNodes(message);
                }
            }
            return(nodes);
        }
예제 #27
0
 public void Enqueue(T val)
 {
     ++Count;
     _inStack.Push(val);
 }
예제 #28
0
 private void GenerateSingleItemExample()
 {
     SingleItemLinkedListStack = new LinkedListStack <string>();
     SingleItemLinkedListStack.Push(Items[0]);
 }
예제 #29
0
 public void Push_pushToEmpty_expectTrue()
 {
     _teStack.Push("wo");
     Assert.AreEqual(_teStack.Peek(), "wo");
     Assert.AreEqual(_teStack.Count(), 1);
 }
예제 #30
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Testing singly linked list");
            SinglyLinkedList <int> test = new SinglyLinkedList <int>();

            test.InsertAtFront(1);
            test.InsertAtBack(3);
            test.InsertAtFront(2);
            test.InsertAtBack(4);
            Console.WriteLine(test);

            Console.WriteLine("Testing doubly linked list");
            DoublyLinkedList <int> test2 = new DoublyLinkedList <int>();

            test2.InsertAtFront(1);
            test2.InsertAtFront(2);
            test2.InsertAtFront(3);
            test2.InsertAtFront(4);
            test2.InsertAt(2, 5);
            test2.RemoveAt(2);
            Console.WriteLine(test2);

            Console.WriteLine("Testing LinkListStack");
            LinkedListStack <int> test3 = new LinkedListStack <int>();

            test3.Push(1);
            test3.Push(2);
            test3.Push(3);
            test3.Push(4);
            test3.Remove(1);
            Console.WriteLine(test3);

            Console.WriteLine("Testing LinkListQueue");
            LinkedListQueue <int> test4 = new LinkedListQueue <int>();

            test4.Enqueue(1);
            test4.Enqueue(2);
            Console.WriteLine(test4);
            test4.Dequeue();
            test4.Remove(1);
            Console.WriteLine(test4);

            Console.WriteLine("Testing Chaining Hash Table");
            LinkedHashTable <int, string> test5 = new LinkedHashTable <int, string>();
            string toInsert = "test";

            test5.Add(toInsert.GetHashCode(), toInsert);
            toInsert = "test2";
            test5.Add(toInsert.GetHashCode(), toInsert);
            toInsert = "test3";
            test5.Add(toInsert.GetHashCode(), toInsert);
            toInsert = "test";
            test5.Add(toInsert.GetHashCode(), toInsert);
            Console.WriteLine(test5);

            Random randNum   = new Random();
            var    testarray = Enumerable
                               .Repeat(0, 100)
                               .Select(i => randNum.Next(0, 100));

            Console.WriteLine("Testing Chaining Hash Table with random numbers.");
            LinkedHashTable <int, int> table = new LinkedHashTable <int, int>();

            foreach (var item in testarray)
            {
                table.Add(item.GetHashCode(), item);
            }
            Console.WriteLine(table);

            Console.WriteLine("Testing Chaining Hash table with dates.");
            LinkedHashTable <int, DateTime> tableDT = new LinkedHashTable <int, DateTime>();
            var testDateTimeArray = Enumerable
                                    .Repeat(0, 100)
                                    .Select(i => RandomDayFunc().Invoke());

            foreach (var item in testDateTimeArray)
            {
                tableDT.Add(item.GetHashCode(), item);
            }
            Console.WriteLine(tableDT);

            Console.ReadLine();
        }