C語言反轉單向鏈表的代碼
阿新 • • 發佈:2019-04-24
clu 應該 eve else int test rev var for 學習過程中中,把內容過程中常用的內容片段做個珍藏,下邊內容段是關於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;
}
C語言反轉單向鏈表的代碼