标签:华为入职练习 华为oj
#include <stdlib.h> #include <algorithm> #include <functional> #include <iostream> #include "oj.h" using namespace std; /* 功能: 输入一个不带头节点的单向链表(链表的节点数小于100),删除链表中内容重复的节点(重复的节点全部删除),剩余的节点逆序倒排。 输入: pstrIn: 输入一个不带头节点的单向链表 输出: pstrOut:删除内容重复的节点后,逆序排列的链表(不带头节点,链表第一个节点的内存已经申请)。 返回: 示例: 输入链表的内容依次为 6,7,8,8,9,10,6 则输出链表的内容依次应该是 10,9,7 */ int iChanProcess(strNode * pstrIn,strNode * pstrOut) { if (NULL == pstrIn || NULL == pstrOut) { return -1; } strNode *pCur = pstrIn; int len = 0; while (NULL != pCur) { len++; pCur = pCur->pstrNext; } int *iArray = new int [len]; pCur = pstrIn; int iCur =0; while (NULL != pCur) { iArray[iCur] = pCur->data; iCur++; pCur = pCur->pstrNext; } sort(iArray,iArray + len, greater<int>()); pCur = pstrOut; iCur = 0; int flag = 0; //标记输出的第一个节点是否被使用 int *iArrayTemp = new int [len]; memset(iArrayTemp, 0 , sizeof(int) * len); int iCurTemp =0; int tempLen = 0; if (iArray[iCur] == iArray[iCur + 1]) //处理第一节点 { iCur ++; } else { iArrayTemp[iCurTemp] = iArray[iCur]; iCurTemp++; iCur++; } for (; iCur < len -1; ++iCur) { if (iArray[iCur] == iArray[iCur - 1] ||iArray[iCur] == iArray[iCur + 1]) { continue; } else { iArrayTemp[iCurTemp] = iArray[iCur]; iCurTemp++; } } if (iArray[iCur] != iArray[iCur - 1] ) //处理最后一个节点 { iArrayTemp[iCurTemp] = iArray[iCur]; iCurTemp++; } int iTempLen = iCurTemp; pCur = pstrOut; pCur->data = iArrayTemp[0]; pCur->pstrNext = NULL ; for (int i = 1; i < iCurTemp ; ++ i ) { strNode *node = new strNode; node->data = iArrayTemp[i]; node->pstrNext =NULL; pCur->pstrNext = node; pCur = pCur->pstrNext; } return 0; } /* 释放链表 */ void vFreeChan(strNode * pstrChan) { if (NULL == pstrChan) { return ; } strNode * pCur = pstrChan; strNode * pNext = pstrChan; while (NULL != pCur) { pNext = pCur->pstrNext; delete pCur; pCur = NULL; pCur = pNext; } return; } strNode *vCreatChan(int * piData, int iNum) { int iLoop = 0; strNode * pstrChanHead = NULL; strNode * pstrChanEnd = NULL; strNode * pstTemp = NULL; if ((NULL == piData) || (iNum < 1)) { return NULL; } pstrChanHead = (strNode *)malloc(sizeof(strNode)); pstrChanHead->data = *piData; pstrChanHead->pstrNext = NULL; pstrChanEnd = pstrChanHead; for (iLoop = 1; iLoop < iNum; iLoop++) { pstTemp = (strNode *)malloc(sizeof(strNode)); pstTemp->data = piData[iLoop]; pstTemp->pstrNext = NULL; pstrChanEnd->pstrNext = pstTemp; pstrChanEnd = pstrChanEnd->pstrNext; } return pstrChanHead; } int iChanToArray(strNode *pstrChan, int * piData, int * piLenth) { int iLoop = 0; strNode *pstrTemp = pstrChan; if ((NULL == pstrChan) || (NULL == piData) || (NULL == piLenth)) { return -1; } while(NULL != pstrTemp) { *(piData + iLoop) = pstrTemp->data; pstrTemp = pstrTemp->pstrNext; iLoop++; } *piLenth = iLoop; return 0; } int main() { strNode * pstrIn = NULL; strNode * pstrOut = NULL; int iLenth = 0; int iaDataIn[7] = {6, 7, 8, 8 ,9, 10, 6}; int iaDataOut[7] = {10,9,7}; int iaDataMyOut[7] = {0}; pstrIn = vCreatChan(iaDataIn, 7); pstrOut = (strNode *)malloc(sizeof(strNode)); pstrOut->pstrNext = NULL; /* TODO: 调用被测函数 */ //请考生自己构造单向链表进行测试 iChanProcess(pstrIn,pstrOut); /* TODO: 执行完成后可比较是否是你认为正确的值 */ iChanToArray(pstrOut, iaDataMyOut, &iLenth); //printf("%d\n",pstrOut->data); //CPPUNIT_ASSERT(3 == iLenth); //CPPUNIT_ASSERT(0 == memcmp((char *)iaDataOut, (char *)iaDataMyOut, sizeof(int) * iLenth)); /* 释放内存*/ vFreeChan(pstrIn); vFreeChan(pstrOut); return 0; }
标签:华为入职练习 华为oj
原文地址:http://blog.csdn.net/xiaohanstu/article/details/42582279