示例#1
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 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);
        }
        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);
        }
示例#4
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());
        }
示例#5
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
    }
示例#6
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);
        }
示例#7
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());
        }
示例#8
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 PushElements()
        {
            var stack = new StackLinkedList <int>();

            for (int i = 0; i < MAX_SIZE; i++)
            {
                stack.Push(i);
            }
        }
        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());
        }
示例#11
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);
        }
示例#12
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());
        }
示例#13
0
 private void dfs(DiGraph G, int v)
 {
     marked[v] = true;
     foreach (var w in G.adj(v))
     {
         if (marked[w])
         {
             continue;
         }
         dfs(G, w);
     }
     reversePostOrder.Push(v);
 }
        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());
            }
        }
        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());
        }
        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.");
            }
        }
示例#17
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);
        }