예제 #1
0
        public IEnumerable <Edge <T> > PathTo(T v)
        {
            if (!vertexes.Contains(v))
            {
                throw new IndexOutOfRangeException("Graph does not contain parameter vertex");
            }
            if (HasNegativeCycle())
            {
                throw new Exception("Negative cost cycle exists");
            }
            if (!HasPathTo(v))
            {
                return(null);
            }
            var path = new StackLinkedList <Edge <T> >();

            //for (var x = v; x.CompareTo(s) != 0; x = edgeTo[x].Other(x))
            //{
            //    path.Push(edgeTo[x]);
            //}
            for (var e = edgeTo[v]; e != null; e = edgeTo[e.From()])
            {
                path.Push(e);
            }
            return(path);
        }
예제 #2
0
        private void dfs(DiGraph G, int v)
        {
            marked[v]  = true;
            onStack[v] = true;
            foreach (var w in G.adj(v))
            {
                if (!marked[w])
                {
                    edgeTo[w] = v;
                    dfs(G, w);
                }
                else if (onStack[w])
                {
                    if (circle == null)
                    {
                        return;
                    }
                    else
                    {
                        circle = new StackLinkedList <int>();
                        for (var x = w; x != v; x = edgeTo[x])
                        {
                            circle.Push(x);
                        }

                        circle.Push(v);
                        circle.Push(w);
                    }
                }
            }
            onStack[v] = false;
        }
        public void PushElements()
        {
            var stack = new StackLinkedList <int>();

            for (int i = 0; i < MAX_SIZE; i++)
            {
                stack.Push(i);
            }
        }
예제 #4
0
        public IEnumerable <Edge> PathTo(int v)
        {
            var path = new StackLinkedList <Edge>();

            for (var x = v; x != s; x = edgeTo[x].from())
            {
                path.Push(edgeTo[x]);
            }
            return(path);
        }
        public void StackSizeIncreases()
        {
            var stack = new StackLinkedList <int>();

            for (int i = 0; i < MAX_SIZE; i++)
            {
                stack.Push(i);
            }
            Assert.AreEqual(MAX_SIZE, stack.Size());
        }
        public void Get_GetFromEmptyStackShouldThrowExeption()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();

            // Act

            // Assert
            Assert.Throws <DataStructureIsEmptyOnReadExeption>(() => sut.Get());
        }
예제 #7
0
        public IEnumerable <int> Path(int v)
        {
            var path = new StackLinkedList <int>();

            for (var x = v; x != s; x = edgeTo[x])
            {
                path.Push(x);
            }
            path.Push(s);
            return(path);
        }
예제 #8
0
        public void Null_Stack_Using_LinkedList()
        {
            var head = new StackNodeSingly <int>(1);

            StackLinkedList <int> stack = new StackLinkedList <int>(head);

            Assert.Equal(1, stack.Pop());
            Assert.Equal(0, stack.Pop());
            stack.Push(new StackNodeSingly <int>(2));

            output.WriteLine(stack.Draw());
        }
        public void TestStack()
        {
            var stack = new StackLinkedList <int>();

            stack.Push(10);
            stack.Push(20);
            Assert.Equal(2, stack.Count);
            Assert.False(stack.IsEmpty);
            Assert.Equal(20, stack.Pop());
            Assert.Equal(10, stack.Pop());
            Assert.True(stack.IsEmpty);
        }
        public void Size_EmptyStackShouldReturnZero()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            int expected = 0;

            // Act
            int actual = sut.Size();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void IsEmpty_EmptyStackShouldReturnTrue()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            bool expected             = true;

            // Act
            bool actual = sut.IsEmpty();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Indexer_SetFromWrongIndexShouldThrowExeption()
        {
            // Arrange
            StackLinkedList <double> sut = new StackLinkedList <double>();

            sut.Add(1);
            sut.Add(2);

            // Act

            // Assert
            Assert.Throws <IndexOutOfRangeException>(() => sut[5] = 10);
        }
예제 #13
0
        public void Implement_Stack_Using_LinkedList()
        {
            var head = new StackNodeSingly <int>(1);

            StackLinkedList <int> stack = new StackLinkedList <int>(head);

            stack.Push(new StackNodeSingly <int>(2));
            stack.Push(new StackNodeSingly <int>(3));
            Assert.Equal(3, stack.Pop());
            stack.Push(new StackNodeSingly <int>(4));
            Assert.Equal(4, stack.Pop());
            output.WriteLine(stack.Draw());
        }
        public void PopReturnsValue()
        {
            var stack = new StackLinkedList <int>();

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

            for (int i = MAX_SIZE - 1; i > 0; i--)
            {
                Assert.AreEqual(i, stack.Pop());
            }
        }
예제 #15
0
    static void Main()
    {
        StackLinkedList list = new StackLinkedList();

        list.push(2);
        //Console.WriteLine(list.pop());
        Console.WriteLine(list.peek());
        Console.WriteLine(list.pop());
        list.push(3);
        list.push(4);
        list.push(5);
        Console.WriteLine(list.pop());
        Console.WriteLine(list.pop());
        Console.WriteLine(list.pop());
    }
예제 #16
0
        public DepthFirstPostOrder(DiGraph G)
        {
            var V = G.V();

            marked           = new bool[V];
            reversePostOrder = new StackLinkedList <int>();
            for (var v = 0; v < V; ++v)
            {
                if (marked[v])
                {
                    continue;
                }
                dfs(G, v);
            }
        }
        public void Clear_ClearStackWithTwoNumbersShouldWork()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            bool expected             = true;

            // Act
            sut.Add(int.MaxValue);
            sut.Add(int.MinValue);
            sut.Clear();
            bool actual = sut.IsEmpty();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void StackSizeDecreases()
        {
            var stack = new StackLinkedList <int>();

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

            for (int i = MAX_SIZE; i > 0; i--)
            {
                stack.Pop();
            }
            Assert.AreEqual(0, stack.Size());
        }
예제 #19
0
    public static void Main(string[] args)
    {
        StackLinkedList <string> stackLinkedList = new StackLinkedList <string>();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");
        Console.WriteLine($"IsEmpty: {stackLinkedList.IsEmpty()}");

        Console.WriteLine();

        stackLinkedList.Push("John");
        stackLinkedList.Push("Sarah");
        stackLinkedList.Push("Mario");

        Console.WriteLine();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");
        Console.WriteLine($"IsEmpty: {stackLinkedList.IsEmpty()}");

        Console.WriteLine();

        Console.WriteLine($"Pop: {stackLinkedList.Pop().ToString()}");
        Console.WriteLine($"Peek: {stackLinkedList.Peek().ToString()}");
        Console.WriteLine($"Pop: {stackLinkedList.Pop().ToString()}");
        Console.WriteLine($"Peek: {stackLinkedList.Peek().ToString()}");

        Console.WriteLine();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");

        // Expected Output
        // ------------------
        // Size: 0
        // IsEmpty: True
        //
        // Push: John
        // Push: Sarah
        // Push: Mario
        //
        // Size: 3
        // IsEmpty: False
        //
        // Pop: Mario
        // Peek: Sarah
        // Pop: Sarah
        // Peek: John
        //
        // Size: 1
    }
예제 #20
0
        public void PushPop()
        {
            var stack = new StackLinkedList <int>();

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

            Assert.AreEqual("321", stack.GetValues());

            stack.Pop();
            Assert.AreEqual("21", stack.GetValues());

            stack.Pop();
            Assert.AreEqual("1", stack.GetValues());
        }
        public void Add_AddNumbersToStackShouldWork(int count)
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            int expected = count;

            // Act
            for (int i = 0; i < count; i++)
            {
                sut.Add(i);
            }
            int actual = sut.Size();

            // Assert
            Assert.Equal(expected, actual);
        }
예제 #22
0
        /// <summary>
        /// Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
        /// Finds all vertices that can be reached by the starting vertex.
        /// </summary>
        /// <param name="rootVertex"></param>
        /// <param name="previsit"></param>
        /// <returns></returns>
        public HashSet <T> DepthFirstSearch(T rootVertex, Action <T> preVisit = null)
        {
            // Traversed graph information
            var visitedVerticesInfo = new HashSet <T>();

            if (!_aList.ContainsKey(rootVertex))
            {
                return(visitedVerticesInfo);
            }

            // Create a stack and add root vertex
            var stack = new StackLinkedList <T>();

            stack.Push(rootVertex);

            // As long as the stack is not empty:
            while (!stack.IsEmpty())
            {
                // Repeatedly pop a vertex u from the queue.
                var vertex = stack.Pop();

                // Ignore if neigbor is already visited
                if (visitedVerticesInfo.Contains(vertex))
                {
                    continue;
                }

                // Trace the path
                preVisit?.Invoke(vertex);

                // Add vertex info to the visited list
                visitedVerticesInfo.Add(vertex);

                // For each neighbor v of u that has not been visited:
                foreach (var neighbor in _aList[vertex])
                {
                    if (!visitedVerticesInfo.Contains(neighbor))
                    {
                        // Push v
                        stack.Push(neighbor);
                    }
                }
            }

            return(visitedVerticesInfo);
        }
        public void Indexer_IndexerGetAndSetShouldWork()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            int expected = 42;

            // Act
            for (int i = 0; i < 10; i++)
            {
                sut.Add(i);
            }
            sut[5] = 42;
            int actual = sut[5];

            // Assert
            Assert.Equal(expected, actual);
        }
        public void PushPop()
        {
            try
            {
                var stack = new StackLinkedList <int>();
                for (int i = 0; i < MAX_SIZE; i++)
                {
                    stack.Push(i);
                }

                for (int i = MAX_SIZE - 1; i > 0; i--)
                {
                    stack.Pop();
                }
            }
            catch (InvalidOperationException)
            {
                Assert.Fail("Failed to pop all elements.");
            }
        }
        public void Get_SetAndGetNumberShouldBeTheSame(int count)
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            int expected = 42;

            // Act
            sut.Add(expected);
            for (int i = 0; i < count; i++)
            {
                sut.Add(i);
            }
            for (int i = 0; i < count; i++)
            {
                sut.Get();
            }
            int actual = sut.Get();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Enumerable_EnumShouldWork()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int> {
                5, 4, 3, 2, 1
            };

            int[] arr = new int[5];

            // Act
            int i = 0;

            foreach (var item in sut)
            {
                arr[i] = item;
                i++;
            }

            // Assert

            Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, arr);
        }
예제 #27
0
        public void Check_Balanced_Brackets(char[] brackers, bool isBalanced)
        {
            StackLinkedList <char> stack = new StackLinkedList <char>();
            var result = true;

            foreach (var item in brackers)
            {
                switch (item)
                {
                case '{':
                case '[':
                case '(':
                    stack.Push(new StackNodeSingly <char>(item));
                    break;

                case '}':
                case ']':
                case ')':
                    char bracket = stack.Pop();
                    bool isPair  = isPairMatch(bracket, item);
                    if (!isPair)
                    {
                        result = false;
                    }
                    break;

                default:
                    break;
                }
                if (!result)
                {
                    break;
                }
            }

            Assert.Equal(isBalanced, result);
        }
        public void StackIsEmpty()
        {
            var stack = new StackLinkedList <int>();
            InvalidOperationException ex = new InvalidOperationException();

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

                for (int i = 0; i < MAX_SIZE + 1; i++)
                {
                    stack.Pop();
                }
            }
            catch (InvalidOperationException e)
            {
                ex = e;
            }

            Assert.AreEqual("The Stack is empty.", ex.Message);
        }
        public void EmptyStackSizeIsZero()
        {
            var stack = new StackLinkedList <int>();

            Assert.AreEqual(0, stack.Size());
        }