读书人

帮忙看看错哪了

发布时间: 2012-03-30 17:32:09 作者: rapoo

帮忙看看哪里错了
#include<stdio.h>
#include<malloc.h>
#define N 3

struct student *cj();
void disp(struct student *head);
void tj(struct student *head,int i);
void del(struct student *head,int i);

struct student{
int xuehao;
char name[10];
struct student *next;
};
void main()
{
struct student *head;
int i;
head=cj();
disp(head);
printf("请输入要添加的位置");
scanf("%d",&i);
tj(head,i);
disp(head);
printf("请输入要删除的位置");
scanf("%d",&i);
del(head,i);
disp(head);
}



struct student *cj()
{
struct student *head,*pnew,*p;
int i=0,k;

head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
p=head;

if(head==NULL)
return NULL;
while(i<3)
{
printf("input xuehao");
scanf("%d",&k);


i++;
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return NULL;
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
pnew->next=p->next;
head->next=pnew;
p=pnew;

}
return head;
}
void disp(struct student *head)
{
struct student *p=head;
while(p->next!=NULL)
{
printf("%d ",p->xuehao);
printf("%s ",&p->name);
p=p->next;
}
}
void tj(struct student *head,int i)
{
int j=0,k;

struct student *p=head;
struct student *pnew;
while(j<i)
{
p=p->next;
j++;
}
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return;
printf("input xuehao");
scanf("%d",&k);
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
pnew->next=p->next;
p->next=pnew;
}
void del(struct student *head,int i)
{
struct student *p=head;
struct student *d;

int j=0;
while(j<i)
{
p=p->next;
j++;
}
d=p->next;
p->next=p->next->next;
free(d);
}





[解决办法]
scanf()与gets()的联合使用在注意的是'\n'的问题,关于这一点,可以看一下我的空间,有这个资料的.
http://617741931.blog.163.com

读书人网 >C语言

热点推荐