C語言反轉單向鏈表的代碼

學習過程中中,把內容過程中常用的內容片段做個珍藏,下邊內容段是關於C語言反轉單向鏈表的內容,應該能對大夥有較大用處。

#include "stdafx.h"

enum{N = 3};
class Node
{
public:
int var;
Node(int i):pNext(NULL), var(i){}
};

{
if(pHead->pNext->pNext != NULL)
helper(pHead->pNext, reverseHead);
else
reverseHead = pHead->pNext;
pHead->pNext->pNext = pHead;
}

{
if(NULL == pHead || NULL == pHead->pNext)
return NULL;

helper(pHead, reverseHead);
pHead->pNext = NULL;
return reverseHead;

}

{
if(NULL == pHead || NULL == pHead->pNext)
{
return NULL;
}

if(NULL == N2)
{
    N1->pNext = pHead;
    pHead->pNext = NULL;
}
else
{
    while (NULL != N2)
    {
        N1->pNext = pHead;
        pHead = N1;
        N1 = N2;
        N2 = N2->pNext;
    }
    N1->pNext = pHead;
    t->pNext = NULL;
}

return N1;

}

{
while (pHead != NULL)
{
printf("%d ",pHead->var);
pHead = pHead->pNext;
}
printf("rn");
}

void Test()
{
for(int i = 1; i < N; i++)
{
pNode->pNext = pNext;
pNode = pNode->pNext;
}
PrintNode(pHead);
PrintNode(reversedHead);

}

{
Test();
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章