Пример #1
0
 public MyQueue()
 {
     n = 0;
     stacks = new Stack[2];
     stacks[activeStack] = new Stack();
     stacks[inactiveStack] = new Stack();
 }
Пример #2
0
        public void PrintInOrderNonRecursive()
        {
            if (root == null) return;

            Console.WriteLine("In-order traversal (non recursive):");
            Stack<Node> st = new Stack<Node>();

            // add the whole left-most path to stack
            Node p = root;
            while (p != null)
            {
                st.Push(p);
                p = p.left;
            }

            while (st.Count > 0)
            {
                Node top = st.Pop();
                Console.Write("{0}, ", top.val);

                if (top.right != null)
                {
                    // add another left-most path
                    p = top.right;
                    while (p != null)
                    {
                        st.Push(p);
                        p = p.left;
                    }
                }
            }

            Console.WriteLine();
        }
Пример #3
0
 public static void main()
 {
     Stack s = new Stack();
     s.push(19);
     s.push(10);
     s.push(11);
     s.pop();
     s.peek();
 }
 public CycleCounterSingleThreaded(INeighbourshipContainer container)
 {
     _container = container;
     _verticesCount = _container.Size;
     _marked = new bool[_verticesCount];
     for (int i = 0; i < _verticesCount; ++i)
     {
         unmark(i);
     }
     _stack = new Stack<int>();
     _path = new Stack<int>();
 }
Пример #5
0
 public static void main()
 {
     SortedStack sorter = new SortedStack();
     Stack s = new Stack();
     s.push(10);
     s.push(12);
     s.push(4);
     s.push(22);
     s.push(3);
     s.push(153);
     Stack sorted = sorter.sort(s);
     sorter.print(sorted);
 }
Пример #6
0
 public Stack sort(Stack s)
 {
     while (!s.isEmpty())
     {
         int tmp = s.pop();
         while (!tempStack.isEmpty() && tempStack.peek() > tmp)
         {
             s.push(tempStack.pop());
         }
         tempStack.push(tmp);
     }
     return tempStack;
 }
Пример #7
0
        public override void Execute(Instance instance)
        {
            if ( !(instance is SMInstance)) {
                this.result = new SMResult("Type mismatch");
                return;
            }
            SMInstance smi = (SMInstance)instance;

            int n = smi.Size;

            int[] mMatch = new int[n];
            int[] mProposals = new int[n];
            int[] wMatch = new int[n];
            Stack<int> unmatched = new Stack<int>(n);

            for (int i = 0; i < n; i++) {
                mMatch[i] = -1;
                unmatched.Push(i);
                mProposals[i] = 0;

                wMatch[i] = -1;
            }

            while (unmatched.Count > 0) {
                int m = unmatched.Pop();
                int w = smi.MPrefMatrix[m][mProposals[m]];
                mProposals[m]++;

                if (wMatch[w] == -1) {
                    mMatch[m] = w;
                    wMatch[w] = m;
                }
                else {
                    int m1 = wMatch[w];
                    if (smi.WRankMatrix[w][m] < smi.WRankMatrix[w][m1]) {
                        mMatch[m] = w;
                        wMatch[w] = m;
                        mMatch[m1] = -1;
                        unmatched.Push(m1);
                    }
                    else {
                        unmatched.Push(m);
                    }
                }

            }

            this.result = new SMResult(mMatch, wMatch, smi);
        }
Пример #8
0
 public static IEnumerable<Vertex> DepthFirstGraphTraversal(
     this IGraph graph,
     Vertex source)
 {
     Stack<Vertex> toVisit = new Stack<Vertex>();
     toVisit.Push(source);
     while (toVisit.Count > 0)
     {
         Vertex v = toVisit.Pop();
         yield return v;
         foreach (Vertex w in graph.Neighbors(v))
         {
             toVisit.Push(w);
         }
     }
 }
Пример #9
0
 public static IEnumerable<Vertex> PreOrderTreeTraversal(
     this IGraph graph,
     Vertex source)
 {
     Stack<Vertex> toVisit = new Stack<Vertex>();
     HashSet<Vertex> visited = new HashSet<Vertex>();
     toVisit.Push(source);
     visited.Add(source);
     while (toVisit.Count > 0)
     {
         Vertex v = toVisit.Pop();
         yield return v;
         foreach (Vertex w in graph.Neighbors(v))
         {
             if (!visited.Contains(w))
             {
                 toVisit.Push(w);
                 visited.Add(w);
             }
         }
     }
 }
 private void initialise()
 {
     _cyclesCount = 0;
     _verticesCount = _container.Size;
     _marked = new bool[_verticesCount];
     for (int i = 0; i < _verticesCount; ++i)
     {
         unmark(i);
     }
     _stack = new Stack<int>();
     _path = new Stack<int>();
     _rolledBackNeighbours = new HashSet<int>[_cycleLength];
     for (int i = 0; i < _cycleLength; ++i)
     {
         _rolledBackNeighbours[i] = new HashSet<int>();
     }
 }
Пример #11
0
 public void print(Stack s){
     while (!s.isEmpty())
     {
         Console.WriteLine(s.pop());
     }
 }
Пример #12
0
 public SortedStack()
 {
     tempStack = new Stack();
 }