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


C++ LList::InsertLeft方法代码示例

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


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

示例1: SetData

	void SetData(char* longNum) //set long integer
	{
		delete intData;
		intData = NULL;
		intData = new LList();
		intData->head->value = 0;

		int numLen = strlen(longNum);
		char* tempStr = NULL;
		while(numLen > 0) //get the last four digits everytime
		{
			tempStr = new char[EVERY_NODE_LEN + 1];
			tempStr[EVERY_NODE_LEN] = '\0';
			for(int i = EVERY_NODE_LEN-1, j = 0; i >= 0; i--, j++)
			{
				if (numLen-1-j >= 0)
					tempStr[i] = longNum[numLen-1-j];
				else
					tempStr[i] = '0';
			}

			LNode* tempNode = (LNode*) malloc(sizeof(LNode));
			tempNode->value = atoi(tempStr); //turn into integer
			tempNode->next = tempNode->prev = NULL;
			intData->InsertLeft(tempNode);

			tempNode = NULL;
			tempStr = NULL;
			numLen -= EVERY_NODE_LEN;
		}

	}
开发者ID:jasminelu,项目名称:LongInt,代码行数:32,代码来源:CS610Project1.cpp

示例2: Add

	LongInt* Add(LongInt* otherInt) //addition
	{
		LongInt *result = new LongInt();
		LList* otherList = otherInt->intData;
		LList* resultList = result->intData;
		LNode* myNode = intData->FirstRight();
		LNode* otherNode = otherList->FirstRight();

		int addition = 0;
		int carry = 0;
		LNode* tempNode;

		while(intData->count != otherList->count)
		{
			LNode* zeroNode = new LNode();
			zeroNode->value = 0;
			zeroNode->next = zeroNode->prev = NULL;
			if(intData->count > otherList->count)
				otherList->InsertLeft(zeroNode);
			else
				intData->InsertLeft(zeroNode);
			zeroNode = NULL;
		}

		if(intData->head->value == otherList->head->value)
		{
			resultList->head->value = intData->head->value;

			while(myNode != intData->head)
			{
				addition = myNode->value + otherNode->value + carry;
				carry = OverFlow(addition);
				addition = addition - carry * (int)pow(10.0, (double)EVERY_NODE_LEN);

				tempNode = new LNode();
				tempNode->value = addition;
				tempNode->prev = tempNode->next = NULL;
				resultList->InsertLeft(tempNode);
				tempNode = NULL;
				myNode = intData->NextLeft(myNode);
				otherNode = otherList->NextLeft(otherNode);
			}

			if(carry != 0)
			{
				LNode* carryNode = new LNode();
				carryNode->value = carry;
				carryNode->next = carryNode->prev = NULL;
				resultList->InsertLeft(carryNode);
			}
		}
		else //opposite sign
		{
			LList *leftList, *rightList;

			int abs = CompAbs(otherInt);
			if(abs == 0)
			{
				LNode* zeroNode = new LNode();
				zeroNode->value = 0;
				zeroNode->next = zeroNode->prev = NULL;
				resultList->InsertLeft(zeroNode);
				return result;
			}
			else if(abs == 1)
			{
				resultList->head->value = intData->head->value;
				leftList = intData;
				rightList = otherInt->intData;
			}
			else
			{
				resultList->head->value = otherInt->intData->head->value;
				leftList = otherInt->intData;
				rightList = intData;
			}

			myNode = leftList->FirstRight();
			otherNode = rightList->FirstRight();
			carry = 0;

			while(myNode != leftList->head)
			{

				addition = myNode->value - otherNode->value + carry;
				if(addition < 0)
				{
					carry = -1;
					addition = addition + (int)pow(10.0, (double)EVERY_NODE_LEN);
				}
				else
					carry = 0;

				tempNode = new LNode();
				tempNode->value = addition;
				tempNode->prev = tempNode->next = NULL;
				resultList->InsertLeft(tempNode);
				tempNode = NULL;
				myNode = leftList->NextLeft(myNode);
				otherNode = rightList->NextLeft(otherNode);
//.........这里部分代码省略.........
开发者ID:jasminelu,项目名称:LongInt,代码行数:101,代码来源:CS610Project1.cpp


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