public List <BinaryTreeNode> DLRStackSequence() { if (_head == null) { return(null); } Stack961 <BinaryTreeNode> stack = new Stack961 <BinaryTreeNode>(); List <BinaryTreeNode> sequence = new List <BinaryTreeNode>(); BinaryTreeNode node = _head; while (node != null || !stack.isEmpty()) { while (node != null) { sequence.Add(node); stack.push(node); node = node.leftChildNode; } if (!stack.isEmpty()) { node = stack.pop(); node = node.rightChildNode; } } return(sequence); }
public T[] getLDRSequenct() { if (_heapsize == 0) { return(null); } Stack961 <int> stack = new Stack961 <int>(); int curidx = 0; List <T> result = new List <T>(); while (!stack.isEmpty() || curidx < _heapsize) { while (curidx < _heapsize) { stack.push(curidx); curidx = leftchildidx(curidx); } curidx = stack.pop(); result.Add(_heap[curidx]); curidx = rightchildidx(curidx); } return(result.ToArray()); }
protected List <GraphNode> DFS(int nodeidx, int[] visitedflag) { if (nodeidx < 0 || nodeidx >= NodesCount || visitedflag[nodeidx] == 1) { return(null); } Stack961 <int> stack = new Stack961 <int>(); GraphNode node = _nodesarray[nodeidx]; int curidx = nodeidx; List <GraphNode> result = new List <GraphNode>(); List <int> neighbours; while (!stack.isEmpty() || curidx >= 0) { while (curidx >= 0) { stack.push(curidx); result.Add(node); visitedflag[curidx] = 1; neighbours = getNeighbourNodes(curidx); node = null; curidx = -1; for (int i = 0; i < neighbours.Count; i++) { if (visitedflag[neighbours[i]] == 0) { curidx = neighbours[i]; node = _nodesarray[curidx]; } } } int popidx = stack.pop(); neighbours = getNeighbourNodes(popidx); for (int i = 0; i < neighbours.Count; i++) { if (visitedflag[neighbours[i]] == 0) { curidx = neighbours[i]; node = _nodesarray[curidx]; } } } return(result); }
static void testStack961() { int maxvals = 200; int[] vals = new int[maxvals]; for (int i = 1; i < maxvals; i++) { vals[i] = i; } Stack961 <int> stack = new Stack961 <int>(); //测试插入直至栈满 Console.WriteLine("开始测试压栈:"); for (int i = 1; i < maxvals; i++) { bool bsuccess = stack.push(vals[i]); if (bsuccess) { printStackTop(stack); } else { Console.WriteLine(""); Console.WriteLine("压栈失败: 当前压栈数{0},是否栈满{1},当前栈内元素数{2}", i, stack.isFull(), stack.Count()); break; } } //测试弹出直至栈空 Console.WriteLine("开始测试弹栈:"); for (int i = 1; i < maxvals; i++) { printStackTop(stack); int?val = stack.pop(); if (val == null) { Console.WriteLine(""); Console.WriteLine("弹栈失败: 当前弹栈数{0},是否栈空{1},当前栈内元素数{2}", i, stack.isEmpty(), stack.Count()); break; } } }