大家请看,我这个程序错在哪儿啦?
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int date;
struct Node *next;
};
struct Node *creat()
{
struct Node *head,*tail,*p;
int x;
head=tail=NULL;
printf("Input the number:");
while(scanf("%d",&x)==1) /*输入一个字母或符号结束输入*/
{
p= (struct Node *) malloc (sizeof(struct Node));
p->date=x;
p->next=NULL;
if(head==NULL)
head=tail=p;
else
tail=tail->next;
tail->next=p;
}
return(head);
}
struct Node *del(struct Node *head)
{
struct Node *p,*q;
int i,x;
p=head;
printf("Input the number you want to del:");
scanf("%d",&x);
while(1)
{
if(x==1)
{
head=head->next;
free(p);
break;
}
else
{
for(i=1;i<x;i++)
{
q=p;
p=p->next;
}
q=p->next;
free(p);
break;
}
}
return(head);
}
main()
{
struct Node *head,*p,*q,*r,*s;
int i,x;
head=creat();
p=r=head;
while(p)
{
printf("%d ",p->date);
q=p->next;
free(p);
p=q;
}
scanf("%d",&x);
r=del(r);
}
就是不能删除,不知问题在哪儿。