- 结构体与函数声明
#include<Stdio.h>
#include<stdlib.h>
struct node{
int num;
struct node *nextptr;
};
typedef struct node *lim;
lim Creatlist(lim *headptr);
lim reverse(lim headptr);
void Print(lim headptr);
void destroy(lim headptr);
2.主函数与函数调用
{
lim headptr;
headptr=reverse(Creatlist(&headptr));
Print(headptr);
destroy(headptr);
return 0;
}
3.链表的创建
lim Creatlist(lim * headptr)
{
lim currentptr=NULL,lastptr=NULL;
int num;
scanf("%d",&num);
while(num!=-1)
{
currentptr=(lim)malloc(sizeof(lim));
if(currentptr!=NULL)
{
currentptr->num=num;
if(*headptr==NULL)
{
currentptr->nextptr=NULL;
*headptr=currentptr;
}
else
{
currentptr->nextptr=*headptr;
*headptr=currentptr;
}
}
scanf("%d",&num);
}
return *headptr;
}
4.逆序函数的调用与实现
{
lim currentptr,newptr=NULL;
while(headptr!=NULL)
{
currentptr=headptr;
headptr=headptr->nextptr;
if(newptr==NULL)
{
newptr=currentptr;
newptr->nextptr=NULL;
}
else
{
currentptr->nextptr=newptr;
newptr=currentptr;
}
}
return newptr;
}
5.链表的打印
void Print(lim headptr)
{
lim headptrr=headptr;
while(headptrr!=NULL)
{
printf("%d ",headptrr->num);
headptrr=headptrr->nextptr;
}
return ;
}
6.清除占用的动态内存
void destroy(lim headptr)
{
lim cocmb;
while(headptr!=NULL)
{
cocmb=headptr;
headptr=headptr->nextptr;
free(cocmb);
}
return ;
}