关于简单链表的小问题

来源:互联网  时间:2016/7/16 11:43:10

关于网友提出的“ 关于简单链表的小问题”问题疑问,本网通过在网上对“ 关于简单链表的小问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 关于简单链表的小问题
描述:

问题:随便输入一个要删除的值,总是找不到,诚心求解,小弟拜谢
#include 
#include 
//#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
char sex;
int age;
struct student *next;
};
int n,loop;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\nnow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld,%s,%c,%d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}while(p!=NULL);
}
struct student *delete(struct student *head,int age)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");
goto loop;
}
p1=head;
while(p1->next!=NULL && age!=p1->age)
{
p2=p1;
p1=p1->next;
}
if(age==p1->age)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%d\n",age);
n=n-1;
}
else
printf("%d not been found!\n",age);
loop:
return(head);
}
void main()
{
struct student *head;
int delete_age;
printf("please input records:\n");
head=creat();
print(head);
printf("\nplease input the deleted age:");
scanf("%d",&delete_age);
while(delete_age!=0)
{
head=delete(head,delete_age);
print(head);
printf("input the deleted age:");
scanf("%d",&delete_age);
}
}


解决方案1:

LZ的 问题在于 输入流。。
scanf("%ld,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
这句 改成scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
用空格间隔就OK了。。
否则,就是 name以后包括逗号 都赋给 name字符数组了。
delete的函数名 改成 del..delete是C已有的函数。
你的代码已修改好,如下。。VS2008编译运行都OK了。


#include 
#include 
//#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
char sex;
int age;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld %s %c %d",&p1->num,p1->name,&p1->sex,&p1->age);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld %s %c %d",&p1->num,p1->name,&p1->sex,&p1->age);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\nnow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld,%s,%c,%d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}while(p!=NULL);
}
struct student *del(struct student *head,int age)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");
goto loop;
}
p1=head;
while((p1->next!=NULL) && (age!=p1->age))
{
p2=p1;
p1=p1->next;
}
if(age==p1->age)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%d\n",age);
n=n-1;
}
else
printf("%d not been found!\n",age);
loop:
return(head);
}
void main()
{
struct student *head;
int delete_age;
printf("please input records:\n");
head=creat();
print(head);
printf("\nplease input the deleted age:");
scanf("%d",&delete_age);
while(delete_age!=0)
{
head=del(head,delete_age);
print(head);
printf("input the deleted age:");
scanf("%d",&delete_age);
}
}

上一篇刚开始学c语言,一句看不懂
下一篇统计从键盘输入Tab键的个数
明星图片
相关文章
《 关于简单链表的小问题》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)