您的位置:首页 > 其它

简单的链表和文件

2015-01-09 11:38 120 查看
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define SIZE (S *)malloc(sizeof(S))

typedef struct stu

{

    int no;

    char name[20];

    struct stu *next;

}S;

S *head;

void choice();

void init();

void bianli(S *head);

void save(S *head);

void menu();

void menu()

{

    printf("\t\t\t菜单      \n");

    printf("\t\t\t1.创建链表\n");

    printf("\t\t\t2.遍历    \n");

    printf("\t\t\t3.保存    \n");

    

}

void init()

{

    S *p1,*p2;

    head=SIZE;

    head->next=NULL;

     p1=p2=head;

    while(1)

    {

    p1=SIZE;

    printf("请输入学号\n");

    scanf("%d",&p1->no);

        if(p1->no==0)

        {

            p2->next=NULL;

            break;

        }

        printf("请输入姓名\n");

        scanf("%s",p1->name );

        p2->next=p1;

        p2=p1;

    }

}

void bianli(S *head)

{

    S *p1;

    p1=head;

    while(p1->next !=NULL)

    {

    

        p1=p1->next;

        printf("%d %s",p1->no ,p1->name );

    }

    

}

void save(S *head)

{

    S *p1;

    p1=head;

    FILE *fp;

    fp=fopen("qing.txt","w");

    if(fp==NULL)

    {

        printf("not open the file");

        exit(0);

    }

    while(p1->next )

    {

        p1=p1->next ;

        fwrite(p1,sizeof(S),1,fp);

    

    }

}

void choice()

{

    int n;

    while(1)

    {

    menu();

    printf("请输入你要进行的操作\n");

    scanf("%d",&n);

    switch(n)

    {

    case 1:

        {

            init();

            break;

        }

        case 2:

        {

            bianli(head);

            break;

        }

        case 3:

        {

            save(head);

            break;

        }

    }

    }

}

int main()

{

choice();

    return  0;

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: