链表的问题
这是我写的链表,编译没错误就是有警告请教各位大虾帮我看看!还有怎么运行的时候只能输入两个数据,然后就不能输入呢!不明白。请教各位了。#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int number;
char name[10];
char sex;
int age;
struct student *next;
};
int n;
struct student *creat()
{
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
head=NULL;
while(p1->age!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
}
p2->next=NULL;
return head;
}
struct student *del(struct student *head,int age)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("list null\n");
return head;
}
p1=head;
while(age!=p1->age && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(age==p1->age)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
}
else
printf("%d not been found:\n",age);
return head;
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p1,*p2,*p0;
p0=stu;
p1=head;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while(p1->number<p0->number && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p0->number==p1->number)
{
if(head==p1)
head=p0;
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
n=n+1;
}
return head;
}
void main()
{
struct student *head,*stu;
int age;
head=creat();
print(head);
scanf("%d",&age);
while(age!=0)
{
head=del(head,age);
print(head);
printf(" input the deleted age:\n");
scanf("%d",&age);
}
printf("input the insert nunber:\n");
stu=(struct student*)malloc(LEN);
scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
while(stu->age!=0)
{
head=insert(head,stu);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
}
}
昨天我从新按照书上的程序又改了一遍可还是又错误啊!
我都看不懂了!希望各位大虾指教啊!
[ 本帖最后由 sunmingchun 于 2010-6-8 18:30 编辑 ]