主题:帮看下出了什么问题,为什么会出现负数
#include<stdio.h>
#define max 10
typedef struct stu
{
int number;
char name[max];
struct str *next;
}student;
void print(student *head)
{
student *pointer;
pointer=head;
while(pointer!=NULL)
{
printf("##input data##\n");
printf("data number:%d\n",pointer->number);
printf("data name:%s\n",pointer->name);
pointer=pointer->next;
}
}
int query(student *head,char *qname)
{
student *p;
p=head;
while(p!=NULL)
{
if(!strcmp(p->name,qname))
{
printf("the student number:%d\n",p->number);
printf("the student name:%d\n",p->name);
return 1;
}
p=p->next;
}
return 0;
}
int insert(student *head,char *qname,char *inname,int inno)
{
student *p,*hew;
hew=(student*)malloc(sizeof(student));
hew->number=inno;
strcpy(hew->name,inname);
p=head;
while(p!=NULL)
{
if(!strcmp(p->name,qname))
{
hew->next=p->next;
p->next=hew;
return 1;
}
p=p->next;
}
return 0;
}
student *delete(student *head,char *dname)
{
student *pointer;
student *back;
pointer=head;
while(1)
{
if(pointer==NULL)return head;
back=pointer;
pointer=pointer->next;
if(!strcmp(pointer->name,dname))
{
back->next=pointer->next;
free(pointer);
break;
}
if(pointer!=NULL&&pointer->next==NULL)
{
printf("not found\n");
break;
}
}
return head;
}
student *create()
{
student *head;
int datanum;
char dataname[max];
student *hew;
student *pointer;
int i;
head=(student*)malloc(sizeof(student));
if(head==NULL)
printf("内存分配失败");
else
{
datanum=1;
printf("please input the data name:");
scanf("%s",dataname);
head->number=datanum;
for(i=0;i<=max;i++)
{
head->name[i]=dataname[i];
}
head->next=NULL;
pointer=head;
while(1)
{
datanum++;
hew=(student*)malloc(sizeof(student));
printf("please input the data name:");
scanf("%s",dataname);
if(dataname[0]=='0')
break;
hew->number=datanum;
for(i=0;i<=max;i++)
hew->name[i]=dataname[i];
hew->next=NULL;
pointer->next=hew;
pointer=hew;
}
}
return head;
}
main()
{student *h;
char qname[max];
char inname[max];
char dname[max];
int inno;
h=create();
if(h!=NULL)
print(h);
getchar();
printf("please input the name that you want to query:");
gets (qname);
if(!query(h,qname))
printf("没有找到");
printf("please input the name after that you want to inset:");
gets(qname);
printf("please input the name that you want to insert:");
gets(inname);
printf("please input the number that you want to insert:");
scanf("%d",&inno);
if(insert(h,qname,inname,inno))
{
printf("成功");
print(h);
}else
printf("没有找到");
getchar();
printf("please input the name that you want to delete:");
gets(dname);
h=delete(h,dname);
printf(h);
}
运行后是名字会出现负数,最后删除的地方会出现屯屯屯
#define max 10
typedef struct stu
{
int number;
char name[max];
struct str *next;
}student;
void print(student *head)
{
student *pointer;
pointer=head;
while(pointer!=NULL)
{
printf("##input data##\n");
printf("data number:%d\n",pointer->number);
printf("data name:%s\n",pointer->name);
pointer=pointer->next;
}
}
int query(student *head,char *qname)
{
student *p;
p=head;
while(p!=NULL)
{
if(!strcmp(p->name,qname))
{
printf("the student number:%d\n",p->number);
printf("the student name:%d\n",p->name);
return 1;
}
p=p->next;
}
return 0;
}
int insert(student *head,char *qname,char *inname,int inno)
{
student *p,*hew;
hew=(student*)malloc(sizeof(student));
hew->number=inno;
strcpy(hew->name,inname);
p=head;
while(p!=NULL)
{
if(!strcmp(p->name,qname))
{
hew->next=p->next;
p->next=hew;
return 1;
}
p=p->next;
}
return 0;
}
student *delete(student *head,char *dname)
{
student *pointer;
student *back;
pointer=head;
while(1)
{
if(pointer==NULL)return head;
back=pointer;
pointer=pointer->next;
if(!strcmp(pointer->name,dname))
{
back->next=pointer->next;
free(pointer);
break;
}
if(pointer!=NULL&&pointer->next==NULL)
{
printf("not found\n");
break;
}
}
return head;
}
student *create()
{
student *head;
int datanum;
char dataname[max];
student *hew;
student *pointer;
int i;
head=(student*)malloc(sizeof(student));
if(head==NULL)
printf("内存分配失败");
else
{
datanum=1;
printf("please input the data name:");
scanf("%s",dataname);
head->number=datanum;
for(i=0;i<=max;i++)
{
head->name[i]=dataname[i];
}
head->next=NULL;
pointer=head;
while(1)
{
datanum++;
hew=(student*)malloc(sizeof(student));
printf("please input the data name:");
scanf("%s",dataname);
if(dataname[0]=='0')
break;
hew->number=datanum;
for(i=0;i<=max;i++)
hew->name[i]=dataname[i];
hew->next=NULL;
pointer->next=hew;
pointer=hew;
}
}
return head;
}
main()
{student *h;
char qname[max];
char inname[max];
char dname[max];
int inno;
h=create();
if(h!=NULL)
print(h);
getchar();
printf("please input the name that you want to query:");
gets (qname);
if(!query(h,qname))
printf("没有找到");
printf("please input the name after that you want to inset:");
gets(qname);
printf("please input the name that you want to insert:");
gets(inname);
printf("please input the number that you want to insert:");
scanf("%d",&inno);
if(insert(h,qname,inname,inno))
{
printf("成功");
print(h);
}else
printf("没有找到");
getchar();
printf("please input the name that you want to delete:");
gets(dname);
h=delete(h,dname);
printf(h);
}
运行后是名字会出现负数,最后删除的地方会出现屯屯屯