求注解
#include
#include
#define N 100
typedef struct node
{int data;
struct node *next;
}node;
node * createsl()
{
node *p,*s,h;
int j=1,x;
p=s=h=(node)malloc(sizeof(node));
h->next=NULL;
printf("please input \n");
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}
int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("%d\n",j);
return(p->data);
}
else
{
printf("error!\n");
return -1;
}
}
void insertsl(node *h,int i,int l)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL&&j
p=p->next;
j++;
}
t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
}
void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}
if(p->next==NULL)
{
printf("error.\n");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
}
}
void print(node *h)
{
node *s;
s=h->next;
if(s!=NULL)
{
while(s->next!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("error");
printf("\n");
}
int main()
{
node *p;
int a,k;
p=createsl() ;
print(p);
scanf("%d",&a);
access(p,a);
printf("\nplease input :\n");
scanf("%d",&a);
printf("\nplease input :\n");
scanf("%d",&k);
insertsl(p,a,k);
printf("\nthe list is:\n");
print(p);
printf("\nplease input :\n");
scanf("%d",&a);
deletesl(p,a);
print(p);
return 0;
}