当前位置: 首页>>代码示例>>C++>>正文


C++ DLList::getNumItems方法代码示例

本文整理汇总了C++中DLList::getNumItems方法的典型用法代码示例。如果您正苦于以下问题:C++ DLList::getNumItems方法的具体用法?C++ DLList::getNumItems怎么用?C++ DLList::getNumItems使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DLList的用法示例。


在下文中一共展示了DLList::getNumItems方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: setFibNumber

/**********************************************************************
 * Fib - setFibNumber
 * Inputs: n (int)
 * Calculates the n-th Fibonacci number 
 ***********************************************************************/
void Fib::setFibNumber(int n)
{
   if (n < 1)
   {
      cout << "Fibonacci subscript cannot be less than 1\n";
      return;
   }
    /*need to clear fibNumber. setFibNumber() may be called several times on an     object and you would have problems if you didn't erase all previous data*/
   fibNumber.clear();
   fibNumber.insert(1,0); // fibNumber will start as f1. f1 = 1;
   fibSubscript = n;
   
   if (n < 3) // can return here if n is 1 or 2. 
      return;
   
   DLList<int> f2;
   f2.insert(1,0); // fibNumber(aka f1) = 1 and f2 = 1. We are rdy to start.
   Node<int>* p1;// traversal pointers for DLList objects fibNumber(f1) and f2
   Node<int>* p2;
   int temp = 0; // used as f1's node value placeholder. 
   int carry = 0;

   for (int i = 0; i < n - 2; i++) //run n - 2 times. f1 and f2 done already
   {
      //reset pointers to respective beginnings of lists. reset carry to 0
      p1 = fibNumber.getFirstNode();
      p2 = f2.getFirstNode();
      carry = 0;
      //Add both lists. fibNumber becomes sum, f2 becomes what fibNumber was
      while (p1 != NULL) 
      {
         temp = p1->getData();
         p1->setData((p1->getData() + p2->getData() + carry) % 1000000000); 
         carry = (temp + p2->getData()) / 1000000000;
         p2->setData(temp);
         p1 = p1->getNext();
         p2 = p2->getNext();
      }
      if (carry == 1) // Account for carry on addition of final nodes.
      {
         fibNumber.insert(1, fibNumber.getNumItems());
         f2.insert(0, f2.getNumItems());
      }
   }      
}
开发者ID:JRicaurte1985,项目名称:CS-235-Data-Structures-Cplusplus,代码行数:50,代码来源:fibonacci.cpp


注:本文中的DLList::getNumItems方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。