求助关于单链表问题
老师布置的作业:已知带头结点的动态单链表L中的结点是按整数值递增排序的,试写一算法将值为x的结点插入到表L中,使L仍然有序。这是我写的代码:
#include<stdio.h>
typedef int datatype;
typedef struct node{
datatype data;
struct node *next;
} linklist;
linklist *CreatList(){
char ch;
linklist *head,*s,*r;
head=NULL;
r=NULL;
printf("Please input the data of list,enter $ to quit:\n");
ch=getchar();
while(ch!='$'){
s=malloc(sizeof(linklist));
s->data=ch;
if(head==NULL)head=s;
else r->next=s;
r=s;
ch=getchar();
}
if(r!=NULL)r->next=NULL;
return head;
}
InSert(L,x)
linklist *L;
datatype x;
{
linklist *s,*t,*p;
s=malloc(sizeof(linklist));
s->data=x;
t=L=p;
while(t->data<=x)
t=t->next;
while(p->next!=t)
p=p->next;
s->next=t;
p->next=s;
}
main(){
linklist *p;
datatype x;
p=CreatList();
printf("Please input x you want to insert:\n");
scanf("%d",&x);
InSert(p,x);
for(;p->next!=NULL;p=p->next)
printf("%c ",p->data);
printf("\n");
}
运行出错,我知道肯定是插入函数出的错,但不知道错误在哪!请老师们帮我改正一下,谢谢!