刪除鏈表中的重複節點、剩餘節點逆序輸出,節點逆序

來源:互聯網
上載者:User

刪除鏈表中的重複節點、剩餘節點逆序輸出,節點逆序

#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.