#include
struct Node
{
int id;
struct Node *pNext;
};
typedef struct Node *Link; // 需要typedef,否则同时定义多个指针的时候不能写在同一行。
void reverseList(Link *ppHead) // 需要传递头节点的指针的地址,这是为了修改这个值。
{
Link p = *ppHead;
Link q, r; // <=> Node *q, *r;
q = p->pNext;
p->pNext = NULL;
while (q != NULL)
{
r = q->pNext; // 需要预先保存第三个节点的位置。
q->pNext = p;
p = q; // 把p,q 同时往后移一个位置。
q = r;
}
*ppHead = p;
}
void printList(const Link pHead)
{
Link p = pHead;
while (p != NULL)
{
std::cout <id <
p = p->pNext;
}
std::cout <
}
int main(void)
{
Link pHead, pPrev, pNext;
pHead = new Node;
pHead->id = 1;
pHead->pNext = NULL;
pPrev = pHead;
for (int i = 1; i
{
pNext = new Node;
pNext->id = i + 1;
pNext->pNext = NULL;
pPrev->pNext = pNext;
pPrev = pNext;
}
printList(pHead);
reverseList(&pHead);
printList(pHead);
return 0;
}