不知道你是需要用STL来写还是类似C的写法,给个简单的例子吧#include "stdafx.h"
#include "malloc.h"
#include "ostream.h"typedef struct _Node
{
int value;
_Node * pNext;
}Node;Node * InitList()
{
Node * pHead = (Node*)malloc(sizeof(Node));
Node * pNode = pHead;
pHead->value = 0;
for(int i = 1; i < 50; i ++)
{
pNode->pNext = (Node *)malloc(sizeof(Node));
pNode = pNode->pNext;
pNode->value = i;
pNode->pNext = NULL;
}
return pHead;
}
//返回尾节点
Node * Revert1(Node * pHead)
{
Node * pfather = pHead;
Node * pNode;
if(!pfather) return NULL;
pHead = pHead->pNext;
pfather->pNext = NULL;
while(pHead != NULL)
{
pNode = pHead->pNext;
pHead->pNext = pfather;
pfather = pHead;
pHead = pNode;
}
return pfather;
}
//返回尾节点
Node * Revert2(Node * pHead, Node * pfather = NULL)
{
Node * ret = NULL;
if(!pHead) return NULL;
if(pHead->pNext)
ret = Revert2(pHead->pNext, pHead);
pHead->pNext = pfather;
if(!ret)
return pHead;
else
return ret;
}void PrintNode(Node * pNode)
{
while(pNode)
{
cout<
pNode = pNode->pNext;
}
}
int main(int argc, char* argv[])
{
Node * pNode = Revert2(InitList());
PrintNode(pNode);
return 0;
}