笔试题记录
字符串拷贝
char* strcpy(char *dst, char *src)
{
char *tempDst = dst;
//测试字符串的长度,如果目标字符串长度大于源字符串,返回目标指针dst,否则,返回NULL
if(strlen(dst) > strlen(src))
{
while(*src != '\0')
{
*tempDst++ = *src++;
}
*tempDst = '\0';
return dst;
}
else
{
return NULL;
}
}
实现链表的插入功能
//在第i个位置插入节点,如果i大于0小于等于链表长度,插入节点成功,返回true,否则返回false
bool Inerset(CNode *pHead, int i,CNode *pNode)
{
int index = 1; //记录当前节点的位置
CNode *currentNode = pHead; //记录当前节点
//判断当前节点的位置
while(currentNode->pNext != NULL && index != i)
{
currentNode = currentNode->pNext;
index++;
}
if(index == i && currentNode->pNext != NULL)
{
pNode->pNext = currentNode->pNext;
currentNode->pNext->pPrev = pNode;
pNode->pPrev = currentNode;
currentNode->pNext = pNode;
return true;
}
else
{
return false;
}
}
用递归实现查找叶子的颜色
//此函数的算法采用的是深度遍历,如果找到,就返回目标节点,如果为找到,就返回NULL
CLeaf* FindColor(CLeaf *Tree, int Color, int& Deap)
{
//返回目标节点
if(Tree->Color == Color)
{
return Tree;
}
else
{
CLeaf **Sub = Tree->SubLeafs;
Deap++;
//深度遍历
while(*Sub != NULL)
{
CLeaf* dstNode = FindColor(*Sub,Color,Deap);
if(dstNode != NULL && dstNode->Color == Color)
{
return dstNode;
}
else
{
Sub++;
}
}
}
Deap--;
return NULL;
}
feiyue3008