C语言实现带表头单链表的就地逆置

#include<stdlib.h>
#include<stdio.h>
#define ERROR 0
#define OK 1
typedef int ElemType;                           //建立带表头结点的单链表
typedef struct node 
{
ElemType element;
struct node *link;
}node;
typedef struct
{
struct node *head;
int n;
}headerList;


typedef int Status;
Status Init (headerList *h )                           //对单链表初始化
{
h->head=(node*)malloc (sizeof(node));
if(!h->head)
return ERROR;
h->head->link=NULL;
h->n=0;
return OK;
}


Status Insert(headerList *h,int i,ElemType x)//单链表的插入操作
{


    node *p,*q;
int j;
if (i<-1||i>h->n-1)
return ERROR;
    p=h->head;
for (j=0;j<=i;j++)  p=p->link;
    q=malloc(sizeof(node));
q->element=x;
q->link=p->link;
p->link=q;
h->n++;
return OK;
}
Status Output(headerList h)//单链表的输出操作
{       
int i=0;
node *p;
if(!h.head)
return ERROR;                               //判断单链表是否为空
p=h.head;
for(i=0;i<h.n;i++)
{  
p=p->link;
printf("%d",p->element);
}
     return OK;
}
void Invert(headerList *h)                                  //实现单链表的逆置
{
node *p,*q;
p=h->head;
h->head=NULL;
while(p)
{
q=p->link;
        p->link=h->head;
h->head=p;
p=q;
}
}
void main()
{
int i;
int x;
        headerList list;
Init (&list);                                          //单链表初始化
for(i=0;i<9;i++)
Insert (&list,i-1,i);                                      //插入数据
printf("the linklist is:");
Output(list);
Invert(&list);                                      //执行逆置操作
        printf("\nthe linklist is:");
Output(list);

}

结果不太完整,后面出现-842150451是位置因为带表头单链表最前面有一个空白链,但执行的过程中有一个问题,最后一个链表8的位置后面没有后续结点,所以最后一个结点没有逆置

C语言实现带表头单链表的就地逆置