yuanzhen@debian:~/C_script$ cat simple_list.c
#include <stdio.h>
#include <malloc.h>
//#define LEN sizeof(struct tag_student)
#define LEN sizeof(student)
typedef struct tag_student
{
int number;
float score;
struct tag_student *next;
} student;
int m=0;
student *create()
{
//int m=0;
student *head, *data, *final;
data=(student *)malloc(LEN);
scanf("%d,%f",&data->number, &data->score);
head=NULL;
while(data->number!=0 )
{
m=m+1;
if(m==1)
head=data;
else
{
final->next=data;
}
final=data;
data=(student *)malloc(LEN);
scanf("%d,%f",&data->number, &data->score);
}
final->next=NULL;
printf("The list's length is %d.\n", m);
return head;
}
void print(student *head)
{
student *point;
point=head;
if(head)
{
do
{
printf("The student %d's score is %.2f\n",point->number, point->score);
point=point->next;
}
while(point!=NULL);
}
else printf("the list is zero\n");
}
student *del(student *head, int num)
{
student *data, *final;
if(!head)
printf("\nthis is a zero list!!\n");
else
{
data=head;
while(num!=data->number && data->next!=NULL)
{
final=data;
data=data->next;
}
if(num==data->number)
{
if(data==head)head=head->next;// or head=data->next;
else
{
final->next=data->next;
}
m=m-1;
printf("delete: %d\n",num);
}
else
printf("there is no this num!!\n");
}
return head;
}
student *insert(student *head, int num, float sco)
{
student *data, *final, *temp;
int i;
if(!head)
{
//printf("\nthis is a zero list\n");
data=(student *)malloc(LEN);
data->number=num;
data->score=sco;
data->next=NULL;
head=data;
m=m+1;
return head;
}
else
{
data=head;
if(num<head->number)
{
data=(student *)malloc(LEN);
data->number=num;
data->score=sco;
data->next=head;
m=m+1;
head=data;
return head;
}
else
{
//printf("000000000000000000000");
for(i=0;i<m;i++)
{
printf("print %d,%d\n",i,num);
final=data;
data=data->next;
if(!data)
{
data=(student *)malloc(LEN);
data->number=num;
data->score=sco;
data->next=NULL;
final->next=data;
m=m+1;
return head;
}
else
{
if(num>=final->number && num<data->number)
{
temp=(student *)malloc(LEN);
temp->number=num;
temp->score=sco;
temp->next=data;
final->next=temp;
m=m+1;
return head;
}
}
}
}
}
}
void main()
{
student *head, *data, *final;
int delnum;
int num_in;
float sco_in;
head=create();
print(head);
printf("m is %d!!!",m);
printf("\nPlease, input the num you hope to delete!!\n");
scanf("%d",&delnum);
head=del(head, delnum);
print(head);
printf("m is %d!!!",m);
printf("\nPlease, input the student data:\n");
scanf("%d,%f",&num_in, &sco_in);
//scanf("%d,%f",&data->number, &data->score);
head=insert(head, num_in, sco_in);
//head=insert(head, data->number, data->score);
print(head);
printf("m is %d!!!",m);
}