void InitList(Node *pHead)
{
Node* pList = pHead;
int i = 0;
for(; i < 10; i++)
{
Node *pNode = new Node;
pNode->data = i;
pNode->next = NULL;
pList->next = pNode;
pList = pNode;
}
}
void PrintList(Node *pHead)
{
Node* pList = pHead->next;
while(pList)
{
printf("%d-",pList->data);
pList = pList->next;
}
printf("\r\n");
}
void ReleaseList(Node *pHead)
{
Node* pList = pHead->next;
while(pList)
{
Node* pNext = pList->next;
delete pList;
pList = pNext;
}
}
void ReverseList(Node *pHead)
{
Node* pList = pHead->next;
Node* pPre = NULL;
Node* pNext = NULL;
while(pList)
{
pNext = pList->next;
pList->next = pPre;
pPre = pList;
pList = pNext;
}
pHead->next = pPre;
}
int main(int argc, char** argv) {
Node head;
InitList(&head);
PrintList(&head);
ReverseList(&head);
PrintList(&head);
ReleaseList(&head);
return 0;
}
单链表的反转
最新推荐文章于 2023-05-16 13:54:02 发布