int main()
{
	MyStack<int> stack;
	for(int i=0;i<10;i++)
		stack.Push(i);
	for(i=0;i<10;i++)
		cout<<stack.Pop()<<endl;
	return 0;
}
Exemple #2
0
//前序遍历用循环实现
void CTree::PreOrderLoop(CTreeNode *lpRoot)
{
    CTreeNode *lpCurt = lpRoot; 
    MyStack<CTreeNode*> stack;
    while(1)
    {
        assert(lpCurt != NULL);
        if (lpCurt != NULL)
        {
            cout << lpCurt->m_data << " ";
            if (lpCurt->m_lpLeft != NULL)
            {
                if (lpCurt->m_lpRight)
                {
                    stack.Push(lpCurt->m_lpRight);
                }
                lpCurt = lpCurt->m_lpLeft;
                continue;
            }
            else
            {
                if (lpCurt->m_lpRight != NULL)
                {
                    lpCurt = lpCurt->m_lpRight;
                }
                else
                {                    
                    if (stack.Length() != 0)
                    {                
                        lpCurt = stack.GetHead();
                        stack.Pop();
                    }
                    else
                    {
                        break;
                    }
                }
                continue;
            }
        }
        else //(lpCurt == NULL)
        {
        }
    }
}
Exemple #3
0
TreeNode<char> *  Input (BinTree<char> & B) //已声明为模板树的友元函数,可以访问其任意实例
{
	if (B.root!=NULL) {cout<<"该树已存在"<<endl;return NULL;}
    char c[DEFAULTSIZE];
	char *ch=c;  //字符指针
	cin>>ch;
	MyStack<TreeNode<char> *> S;//储存结点指针的栈
	B.root=new TreeNode<char>(*ch);
	int flag;
	TreeNode<char> *p=B.root;
	while (ch++,*ch!='#')
	{
		if (*ch=='(') {flag=1;S.Push(p);}
		else if (*ch==',') flag=2;
		     else if(*ch==')') S.Pop();
			 else
			 {
				 if (flag==1) p=S.GetTop()->leftChild=new TreeNode<char>(*ch);
				 else p=S.GetTop()->rightChild=new TreeNode<char>(*ch);
			 }
	}
	return B.root;
}
Exemple #4
0
/*
  T-Check
  Do the following: 
    1. Identify the ROOT nodes ( with only one upstream edges )
    2. Identify the LEAF nodes ( with only one downstream edges )
    3. Propagate from all LEAF nodes down to all ROOT nodes;
    4. Propagate from all ROOT nodes up to all LEAF nodes;
    5. All nodes should be visited exactly twice. If not, raise the flag
  returncode: 
      0 good
     <0 bad
*/
int ngraph::TChk() {
  int rc;
  int jj;
  
  edge *eup, *edn;

  rc = 0;
  for (jj=0; jj<_num_nodes; jj++) {
    eup = _nodes[jj]->GetUp();
    edn = _nodes[jj]->GetDown();
    if ( eup != NULL ) {
      if ( edn != NULL ) _nodes[jj]->SetType( NORMAL );
      else               _nodes[jj]->SetType( ROOT );
    } else {
      if ( edn != NULL ) _nodes[jj]->SetType( LEAF );
      else               { 
	_nodes[jj]->SetType( UNDEFINED );
	_nodes[jj]->SetFlag( HANGING );
	rc = -1;
      }
    }
  }
  if ( rc != 0 ) return (rc) ; // hanging nodes, no need to continue

  // do a quick check to make sure no vertices with both edge and other
  // which should not happen anyway. 
  // also set the vertex type to those vertices, they could be mislabelled as leaf or root
  for (jj=0; jj<_num_nodes; jj++ ) {
    if ( _nodes[jj]->GetOther() != NULL && _nodes[jj]->GetDown() != NULL && _nodes[jj]->GetUp() != NULL ) {
      _nodes[jj]->SetType( UNDEFINED );
      _nodes[jj]->SetFlag( WRONG_J );
      rc = -2;  // error flag
    }
    if ( _nodes[jj]->GetOther() != NULL ) _nodes[jj]->SetType(IN_JUNC);
  }
  if (rc != 0 ) return (rc);

  
  // Each Leaf should have a positive injection
  for (jj=0; jj<_num_nodes; jj++) {
    if ( _nodes[jj]->GetType() != LEAF ) continue;
    if ( _nodes[jj]->Injection() < this->_tol ) {
      _nodes[jj]->SetFlag(NEED_Q);
      rc = -3;
    }
  }
  if ( rc != 0) return (rc);
  
  // Each Root should have a positive A
  for (jj=0; jj<_num_nodes; jj++) {
    if ( _nodes[jj]->GetType() != ROOT ) continue;
    if ( _nodes[jj]->SumUp() < this->_tol) {
      _nodes[jj]->SetFlag(NEED_A);
      rc = -4;
    }
  }
  if ( rc != 0) return (rc);
  
  /* propagate Q downstream, from leaves to roots */
  // note injection can present at any nodes, not necessarily only on the leaf
  // starting from the leaves
  
  // two stacks, always work in sync
  MyStack<vertex*>   NS;  // node stack
  MyStack<double>    VS;  // value stack
  
  vertex* nn;
  double  vv=-1.0;
  TinyVec<vertex*, 64>   tmp_nodes;  // somehow unsafe, but 64 should be enough
  TinyVec<double,  64>   tmp_vals;
  int                    junk;
  // start from leaves
  for (jj=0; jj<_num_nodes; jj++) {
    if ( _nodes[jj]->GetType() == LEAF) { 
      NS.Push(_nodes[jj]);
      VS.Push( _nodes[jj]->SumDown() );
    }
  }

  while (1) {
    if ( NS.Pop(nn) == -1 ) break;
    VS.Pop(vv);
    
    junk = this->_FindDownStreamNodes(nn, &tmp_nodes[0], &tmp_vals[0]);
    vv += nn->Injection();
    nn->SumDown() += vv;
    for (int jj=0; jj<junk; jj++) {
      NS.Push(tmp_nodes[jj]);
      VS.Push(vv*tmp_vals[jj]);
    }
  }

  // ditto for upstream traversing, but start from roots
  for (jj=0; jj<_num_nodes; jj++) {
    if ( _nodes[jj]->GetType() == ROOT ) {
      NS.Push(_nodes[jj]);
      VS.Push( _nodes[jj]->SumUp() );
    }
  }
  
  while (1) {
    if ( NS.Pop(nn) == -1 ) break;
    VS.Pop(vv);
    
    junk = this->_FindUpStreamNodes(nn, &tmp_nodes[0], &tmp_vals[0]);
    nn->SumUp() = vv;   // upstream is slightly different, no accumulation
    
    for (int jj=0; jj<junk; jj++) {
      NS.Push(tmp_nodes[jj]);
      VS.Push(vv*tmp_vals[jj]);
    }
  }

  // both sumup and sumdown should be nonzero at all nodes
  for (jj=0; jj<_num_nodes; jj++) {
    if ( _nodes[jj]->SumDown() < this->_tol ) {
      _nodes[jj]->SetFlag(NO_QPATH);
      rc = -5;
    }
    if ( _nodes[jj]->SumUp() < _tol ) {
      _nodes[jj]->SetFlag(NO_APATH);
      rc = -5;
    }
#if 0
    printf("%d: %.5f %.5f\n", jj, _nodes[jj]->SumUp(), _nodes[jj]->SumDown());
#endif   
  }
  if ( rc != 0 ) return (rc);

  _chk_ok = 1;
  return (rc);
}
    void InfectDeadToNeighborsNonRecursive(vector<vector<char>> &board, int x, int y)
    {
        int     n = board.size();
        if (n == 0)
            return;

        int     m = board.at(0).size();
        if (m == 0)
            return;

        StackElem elem = { 1, x, y };
        stack->Push(elem);

        while (!stack->IsEmpty())
        {
            x = stack->Peak().x;
            y = stack->Peak().y;

            switch (stack->Peak().step)
            {
            case 0:
                stack->Pop(elem);

                break;
            case 1:
                board.at(x).at(y) = 'D';
                printf("Infect %d %d\n", x, y);

                //infect left
                stack->Peak().step = 2;
                if (y - 1 >= 0 && board.at(x).at(y - 1) == 'O')  //step1
                {
                    elem.step = 1;
                    elem.x = x;
                    elem.y = y - 1;
                    stack->Push(elem);
                }

                break;
            case 2:

                //infect right
                stack->Peak().step = 3;
                if (y + 1 < m && board.at(x).at(y + 1) == 'O')  //step2
                {
                    elem.step = 1;
                    elem.x = x;
                    elem.y = y + 1;
                    stack->Push(elem);
                }

                break;
            case 3:
                //infect down
                stack->Peak().step = 4;
                if (x + 1 < n && board.at(x + 1).at(y) == 'O')  //step3
                {
                    elem.step = 1;
                    elem.x = x + 1;
                    elem.y = y;
                    stack->Push(elem);
                }

                break;
            case 4:

                //infect up
                stack->Peak().step = 0;
                if (x - 1 >= 0 && board.at(x - 1).at(y) == 'O')  //step4
                {
                    elem.step = 1;
                    elem.x = x - 1;
                    elem.y = y;
                    stack->Push(elem);
                }
                break;
            default:
                break;
            }
        }

        return;
    }