new->prei = NULL;
//while(new->stu_score >= 0)
//while((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
while(1)
{
n = n+1;
if(n == 1)
{
head = new;
tail = head;
}
new = (stu_info*)malloc(sizeof(stu_info));
if(new == NULL) return;
printf("\ninut the name:");
scanf("%20s",new->stu_name);
if((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
break;
printf("\ninut the score:");
scanf("%d",&new->stu_score);
new->index = n;
new->prei = NULL;
new->next = NULL;
tail->next = new;
new->prei = tail;
tail = new;
}
return head;
}
static void stu_info_list_show_index(stu_info *head,int index)
{
stu_info *p = head;
while(p->index != index)
{
p = p->next;
}
printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
p = p->prei;
printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
}
static void stu_info_list_show(stu_info *head)
{
stu_info *p = head;
while(p != NULL)
{
printf("\n%d name:%-10s score:%d",p->index,p->stu_name,p->stu_score);
p = p->next;
}
printf("\n");
return;
}
static void destroy_list(stu_info *head)
{
stu_info *p = head;
stu_info *q;
while(p != NULL)
{
q = p->next;
free(p);
p->next = NULL;
p = q;
}
free(head);
head = NULL;
return;
}
static void write_stu_info_to_file(stu_info* head)
{
FILE *fp;
stu_info *p = head;
fp = fopen("stu_info.txt","w+");
fputs("the info of student:\n",fp);
while(p != NULL)
{
fprintf(fp,"%5d,%-10s,%3d\n",p->index,p->stu_name,p->stu_score);
p = p->next;
}
fclose(fp);
return;
}
void main()
{
stu_info *head;
/* create the list for stu_info */
head = stu_info_list_create();
if(head == NULL) return;
//show the stu_info have input
stu_info_list_show(head);
/* write the stu_info to the file */
write_stu_info_to_file(head);
stu_info_list_show_index(head,4);
destroy_list(head);
}
原文:http://blog.csdn.net/zhangliang_571/article/details/22579191