```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int iData;
struct Node* iNext;
};
void Add(struct Node **Phead, struct Node **Pend,int iData);
void AddHead(struct Node** Phead, struct Node** Pend, int iData);
void Delete(struct Node** Phead, struct Node** Pend, int iData);
void FreeAll(struct Node** Phead, struct Node** pend);
void OutPut(struct Node* Phead);
int GetListCount(struct Node* Phead);
struct Node * LookIndex(struct Node* Phead, int Index);
void SwapIndex(struct Node* Phead, int Index1,int Index2);
void ReverseData(struct Node* Phead);
void ReverseSpace(struct Node ** Phead, struct Node ** pend);
int main()
{
struct Node* Phead = NULL;
struct Node* Pend = NULL;
AddHead(&Phead, &Pend, 4);
Add(&Phead,&Pend,3);
Add(&Phead,&Pend,2);
Add(&Phead,&Pend,9);
ReverseSpace(&Phead,&Pend);
OutPut(Phead);
}
void Add(struct Node** Phead, struct Node** Pend, int iData)
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->iData = iData; p->iNext = NULL;
if (*Phead == NULL)
*Phead = p;
else
(*Pend)->iNext = p;
*Pend = p;
}
void AddHead(struct Node** Phead, struct Node** Pend, int iData)
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->iData = iData; p->iNext = NULL;
if (*Phead == NULL)
{
*Phead = p;
*Pend = p;
}
else
{
p->iNext = *Phead;
*Phead = p;
}
}
void Delete(struct Node** Phead, struct Node** Pend, int iData)
{
if ((*Phead)->iData == iData)
{
struct Node* py = *Phead;
*Phead = py->iNext;
free(py);
}
else
{
struct Node* p = *Phead;
while (p->iNext != NULL)
{
if (p->iNext->iData == iData)
break;
p = p->iNext;
}
if (p->iNext != NULL)
{
struct Node* pt = p->iNext;
p->iNext = pt->iNext;
free(pt);
}
else
printf("查无此节点");
}
}
void FreeAll(struct Node** Phead, struct Node** pend)
{
struct Node* p = *Phead;
while (p != NULL)
{
struct Node* py = p;
p = p->iNext;
free(py);
}
}
void OutPut(struct Node* Phead)
{
while (Phead !=NULL)
{
printf("%d ", Phead->iData);
Phead = Phead->iNext;
}
putchar('\n');
}
int GetListCount(struct Node* Phead)
{
int sum = 0;
while (Phead != NULL)
{
sum++;
Phead = Phead->iNext;
}
return sum;
}
struct Node *LookIndex(struct Node* Phead, int Index)
{
if (Phead == NULL || Index < 0)
{
printf("链表参数有误或者下标小于0");
return NULL;
}
int i = 0;
while (Phead!=NULL)
{
if (i == Index)
{
return Phead;
}
Phead = Phead->iNext;
i++;
}
printf("下标过大未找到");
return NULL;
}
void SwapIndex(struct Node* Phead, int Index1, int Index2)
{
if (Phead == NULL)
return;
struct Node* pt1 = LookIndex(Phead, Index1);
struct Node* pt2 = LookIndex(Phead, Index2);
if (pt1 != NULL && pt2 != NULL)
{
struct Node* p = pt1->iData;
pt1->iData = pt2->iData;
pt2->iData = p;
}
}
void ReverseData(struct Node* Phead)
{
if (Phead == NULL || Phead->iNext == NULL)
return;
int j = GetListCount(Phead)-1;
for (int i = 0; i < j; i++, j--)
{
SwapIndex(Phead, i, j);
}
}
void ReverseSpace(struct Node** Phead, struct Node** pend)
{
if (*Phead == NULL || (*Phead)->iNext == NULL)
return;
struct Node* p = *Phead;
struct Node* PNewhead = NULL;
struct Node* PNewend = NULL;
while (p!= NULL)
{
AddHead(&PNewhead, &PNewend,p->iData);
p = p->iNext;
}
FreeAll(Phead, pend);
*Phead = PNewhead;
*pend = PNewend;
}