逆置1 (无限的头插)
void Reverse1(struct Node* plist)
{
//assert
PNode p = plist->next;
PNode q;
plist->next = NULL;
while(p != NULL)
{
q = p->next;
p->next = plist->next;
plist->next = p;
p = q;
}
}
//逆置2 (需要三个临时指针)
void Reverse2(struct Node* plist)
{
assert(plist != NULL);//确保头结点
assert(plist->next != NULL);//确保首节点存在
assert(plist->next->next != NULL);//确保第二个节点存在
PNode p = plist->next;
PNode q = p->next;
PNode r;
p->next = NULL;
while(q != NULL)//pqr 中用 q!=NULL 来做判断依据
{
r = q->next;
q->next = p;
p = q;
q = r;
}
plist->next = p;
}
在主函数中运行
int main()
{ Node head;
Init_list(&head);
for(int i=0; i<20; i++)
{
Insert_pos(&head, i, i+1);
}
Show(&head);
Reverse1(&head);
Show(&head);
//Reverse2(&head);
Show(&head);
return 0;
}
?
|