看这个吧,更新了:
//元素x插入到i的位置
int Insert(struct node *head,int i, elemtype x){
//当前访问节点
struct node *p=head;
//struct node *newp;//新增节点 /*这里就不要定义了,下面会有重新定义的地方*/
int count;//计数器
if(i<1){
printf("Argument is error,out of range");//参数范围出错
return(0);//返回假值表示不成功
} /*少了这个*/
//遍历链表 找到i-1节点
while(p!=NULL&&count<i-1){
p=p->next;
count++;
}
if(p==NULL)
{
printf("the length of the linked list is less than %d.\n",i-1); /*打印是不是需要看i-1的值呢?*/
//需要找到i-1节点 但是链表的长度小于i-1
return (0);//插入不成功
}
//给新节点申请空间
struct node *newp = (struct node*)malloc (LEN);
if(newp== NULL){
printf("申请失败");
exit(1);}
//插入新节点 指针指向 数据域插入
newp->data=x;
newp->next=p->next;
p->next=newp;
return(1);//插入成功;
}
void reverse(
struct node *head){
/*需要带参数*/
struct node *cp=head->next;//当前指针,指向当前正处理的节点
struct node *pp=NULL;//指向当前节点的前驱
struct node *np;//指向当前节点的后驱
while(cp!=NULL){
np=cp->next;
cp->next==pp;
pp=cp;
cp=np;
}
head->next=pp;
}