首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个建立学生链表的程序,在程序那写一个fwrite函数,写入test.txt文件内?解决方法

2012-03-09 
一个建立学生链表的程序,在程序那写一个fwrite函数,写入test.txt文件内?stu* insert(stu*head)//插入函数

一个建立学生链表的程序,在程序那写一个fwrite函数,写入test.txt文件内?
stu* insert(stu*head)//插入函数上,写个例子!!!!!

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct stu_x{    int student_id;    char name[20];    struct stu_x* next;}stu;stu* create(){    stu* head=NULL,*tail=NULL,*new=NULL;    char y;    head=NULL;    tail=NULL;    do{        new = (stu*)malloc(sizeof(stu));        printf("input new student on the table\n");        printf("input the students' id:");        scanf("%d",&new->student_id);        printf("input the students' name:");        scanf("%*c");        scanf("%s",new -> name);        new -> next = NULL;        if(head==NULL&&tail==NULL)        {            head=new;            tail=new;        }        else        {            tail->next=new;            tail=new;        }        printf("choose Y to go ahead,choose N to quit:");//.....        scanf("%*c");        scanf("%c",&y);    }while(y='y');    return head;}stu*del(stu*head){    stu*p=NULL;    stu*cur=NULL;    int n;    char y;    do{        if(head==NULL)        {            printf("Nobody here,....can't delete");            return NULL;        }        else        {            printf("Enter your deleted students'id");            scanf("%d",&n);            for(cur=head;cur;cur=cur->next)            {                if(cur==head&&cur->student_id==n)                {                    printf("the no.%d student has been deleted\n",n);//....                    free(cur);                }                else if(cur->student_id==n)                {                    p=cur;                    cur=cur->next;                    printf("the no.%d student has been deleted\n",n);                    free(cur);                }                else printf("Sorry,it don't find your want to delete student");//......            }        }        printf("choose Y to go ahead,choose No to quit");        scanf("%*c");        scanf("%c",&y);    }while(y='y');}stu* insert(stu*head)//插入函数上,写个例子!!!!!{    stu*cur=NULL;    stu* p=NULL;    stu* pre=NULL;    int n;    char y;    cur=head;    do{        p = (stu*)malloc(sizeof(stu));        printf("input the no. of node you would like to insert");        scanf("%d",&p->student_id);        printf("input the name of node you would like to insert:");        scanf("%*c");        scanf("%s",p->name);        p->next=NULL;        if(head==NULL)        {            cur=head=p;            printf("No one before the students,this is the first student\n");        }        else        {            printf("input the student position you would like to insert");            scanf("%d",&n);            cur=head;            while(cur!=NULL&&n>0)            {                if(cur->student_id==p->student_id)                {                    printf("student already exist\n");//....                    break;                }                else if(cur->student_id==n)                {                    pre = cur -> next;                    pre == NULL?printf("student's in the end\n"):printf("student's in the between\n");//....                    cur->next=p;                    p->next=pre;                    break;                }                pre=cur;                cur=cur->next;            }            if(cur==NULL)            {                printf("position not found.please input students'information in the last position\n");                pre->next=p;            }        }        printf("choose Y to go ahead, choose N to quit\n");        scanf("%*c");        scanf("%c",&y);    }while(y=='y');    return head;}void print(stu* head){    stu* cur=NULL;    int i=1;    cur = head;    if(head==NULL)    {        printf("Nobody here\n");        return;    }    do{        printf("the %d man information",i);        printf("student id:%d,",cur->student_id);        printf("name:%s\n",cur->name);        cur = cur->next;        i++;    }while(cur!=NULL);}int main(){    stu*s=NULL;    int n;    do{        printf("please choose ");        printf("1.print and 2.insert and 3.del and 4.create or 5.exit\n");//.....        printf("please input your choice");        scanf("%d",&n);        switch(n)        {        case 1:print(s);break;        case 2:s=insert(s);break;        case 3:s=del(s);break;        case 4:s=create();break;        case 5:printf("bye bye!!!\n");exit(0);        }    }while(1);} 



[解决办法]
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct stu_x{    int student_id;    char name[20];    struct stu_x* next;}stu;stu* create(){    stu* head=NULL,*tail=NULL,*new=NULL;    char y;    head=NULL;    tail=NULL;    do{        new = (stu*)malloc(sizeof(stu));        printf("input new student on the table\n");        printf("input the students' id:");        scanf("%d",&new->student_id);        printf("input the students' name:");        scanf("%*c");        scanf("%s",new -> name);        new -> next = NULL;        if(head==NULL&&tail==NULL)        {            head=new;            tail=new;        }        else        {            tail->next=new;            tail=new;        }        printf("choose Y to go ahead,choose N to quit:");//.....        scanf("%*c");        scanf("%c",&y);    }while(y='y');    return head;}stu*del(stu*head){    stu*p=NULL;    stu*cur=NULL;    int n;    char y;    do{        if(head==NULL)        {            printf("Nobody here,....can't delete");            return NULL;        }        else        {            printf("Enter your deleted students'id");            scanf("%d",&n);            for(cur=head;cur;cur=cur->next)            {                if(cur==head&&cur->student_id==n)                {                    printf("the no.%d student has been deleted\n",n);//....                    free(cur);                }                else if(cur->student_id==n)                {                    p=cur;                    cur=cur->next;                    printf("the no.%d student has been deleted\n",n);                    free(cur);                }                else printf("Sorry,it don't find your want to delete student");//......            }        }        printf("choose Y to go ahead,choose No to quit");        scanf("%*c");        scanf("%c",&y);    }while(y='y');        save(head);//删除的时候调用下保存函数就OK了 }stu* insert(stu*head)//插入函数上,写个例子!!!!!{    stu*cur=NULL;    stu* p=NULL;    stu* pre=NULL;    int n;    char y;    cur=head;    do{        p = (stu*)malloc(sizeof(stu));        printf("input the no. of node you would like to insert");        scanf("%d",&p->student_id);        printf("input the name of node you would like to insert:");        scanf("%*c");        scanf("%s",p->name);        p->next=NULL;        if(head==NULL)        {            cur=head=p;            printf("No one before the students,this is the first student\n");        }        else        {            printf("input the student position you would like to insert");            scanf("%d",&n);            cur=head;            while(cur!=NULL&&n>0)            {                if(cur->student_id==p->student_id)                {                    printf("student already exist\n");//....                    break;                }                else if(cur->student_id==n)                {                    pre = cur -> next;                    pre == NULL?printf("student's in the end\n"):printf("student's in the between\n");//....                    cur->next=p;                    p->next=pre;                    break;                }                pre=cur;                cur=cur->next;            }            if(cur==NULL)            {                printf("position not found.please input students'information in the last position\n");                pre->next=p;            }        }        printf("choose Y to go ahead, choose N to quit\n");        scanf("%*c");        scanf("%c",&y);    }while(y=='y');    save(head);//插入的时候调用下保存函数就OK了     return head;}void print(stu* head){    stu* cur=NULL;    int i=1;    cur = head;    if(head==NULL)    {        printf("Nobody here\n");        return;    }    do{        printf("the %d man information",i);        printf("student id:%d,",cur->student_id);        printf("name:%s\n",cur->name);        cur = cur->next;        i++;    }while(cur!=NULL);}void save(stu* head){    stu* cur=NULL;    FILE *sout=fopen("test.txt","w+");//这儿加个定义    int i=1;    cur = head;    if(head==NULL)    {        printf("Nobody here\n");        return;    }    if (!sout) //加个判断    {           printf ( "open file wrong!\n");          return;       }    do{        fprintf(sout, "the %d man information",i);        fprintf(sout, "student id:%d,",cur->student_id);        fprintf(sout, "name:%s\n",cur->name);        cur = cur->next;        i++;    }while(cur!=NULL);}int main(){    stu*s=NULL;    int n;    do{        printf("please choose ");        printf("0.save and 1.print and 2.insert and 3.del and 4.create or 5.exit\n");//.....        printf("please input your choice");        scanf("%d",&n);        switch(n)        {        case 0:save(s);break;        case 1:print(s);break;        case 2:s=insert(s);break;        case 3:s=del(s);break;        case 4:s=create();break;        case 5:printf("bye bye!!!\n");exit(0);        }    }while(1);} 

热点排行