//链表逆转
#define ElementType int
typedef struct ListNode *List;
struct ListNode
{
ElementType Data;
List pNext;
};
List CreatList()
{
List L = (List)malloc(sizeof(struct ListNode));
L->pNext = NULL;
return L;
}
void Insert(List* pRear,ElementType E)
{
List Temp = CreatList();
Temp->Data = E;
(*pRear)->pNext = Temp;
(*pRear) = Temp;
}
void Output(List L)
{
List Temp = L;
while (Temp->pNext)
{
printf("%4d", Temp->pNext->Data);
Temp = Temp->pNext;
}
printf("\n");
}
void Transer(List L)
{
List pOld = L->pNext;
List pNew = pOld->pNext;
List pTemp = pNew->pNext;
int k = 0;
while (k<3)
{
pNew->pNext = pOld;
pOld = pNew;
pNew = pTemp;
pTemp = pTemp->pNext;
++k;
}
L->pNext->pNext = pNew;
L->pNext = pOld;
}
int main()
{
List L1 = CreatList();
List pRear = L1;
Insert(&pRear, 4);
Insert(&pRear, 3);
Insert(&pRear, 2);
Insert(&pRear, 1);
Insert(&pRear, 5);
Insert(&pRear, 6);
Insert(&pRear, 7);
printf("逆转前:");
Output(L1);
Transer(L1);
printf("逆转后:");
Output(L1);
system("pause");
return 0;
}