#include <stdio.h>
#include <stdlib.h>
#include <time.h>typedef struct Node
{
int data;
struct Node * next;
}Node, * LinkList;void ListReverse(LinkList L)
{
Node *p,*q;
p=L->next;
L->next=NULL;
while(p)
{
q=p->next;
p->next=L->next;//头插法
L->next=p;
p=q;
}
}/*逆置函数*/void InitList(LinkList *L)
{
*L = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL;
}void CreateListHead(LinkList L)//头插法创建
{
Node *s;
int i=0;
srand((unsigned)time(NULL));
while(i<10)
{
s=(LinkList)malloc(sizeof(Node));
s->data=rand()%99+10;
s->next=L->next;
L->next=s;
i++;
}
}void DestoryList(LinkList L)
{
Node * p; p = L->next;
while(p)
{
L->next = p->next;
free(p);
p = L->next;
}
free(L);
}void TraverseList(LinkList L)//显示
{
Node * p;
p = L->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LinkList L;
InitList(&L);
CreateListHead(L);
TraverseList(L); ListReverse(L);
printf("逆置后:\n");
TraverseList(L); DestoryList(L); return 0;
}
温馨提示:内容为网友见解,仅供参考