| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 384 人关注过本帖
标题:如何形成(建立)单链表
只看楼主 加入收藏
穆海
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-4-3
收藏
 问题点数:0 回复次数:0 
如何形成(建立)单链表
要做一个小系统(管理学生信息),文件操作和链表操作都不太会,在vi下编译通过之后,运行却显示"已杀死"问题主要在建立链表和文件操作,求解救
#include"student_info.h"
#define Name "usr_info.conf"
Node * insert(Node *tamp);
void search(void);
void passwd(Node *);
void saveinfo(void);
Node * loading(void);
void fileload(void);
void addinfo(void);
void delete(void);
Node *head;
void saveinfo(void)
{
    Node *write;
    FILE *info;
    info = fopen(Name,"w+");
    if((write = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    write = head;
    while(write != NULL && write->next != NULL)
    {
        if(write == NULL)
            write = write->next;
        fwrite(write,sizeof(Node), 1, info);
        write = write->next;
    }
    free(write);
    fclose(info);
}
void delete(void)
{
    char name[32];
    Node *del;
    if((head = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    del = head;
    printf("Please input the user be deleted!:");
    scanf("%s",name);
    while( del != NULL)
    {
        if(strcmp(name, del->usr_name) == 0)
        {
            del = NULL;
            printf("删除成功!");
        }
        del = del->next;
    }

}
void passwd(Node *x )
{
    int i;
    char a[18], b[18];
    printf("请输入新密码:");
    scanf("%s",a);
    printf("请确认所输入密码:");
    scanf("%s",b);
    if(strcmp(a, b) == 0)
    {
        strcpy(x->password, b);
        insert(x);
        printf("修改成功!");
    }
    else
        passwd(loading());
}
Node *  loading(void)
{
    Node *load, *p;
    int j, i;
    if((p = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    p = head;
    if((load = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    printf("登录用户名:");
    scanf("%s",load->usr_name);
    for(j = 0; j < 2; j++)
    {
        while(strcmp(load->usr_name, p->usr_name) == 0 )
        {
            printf("登录密码:");
            scanf("%s", load->password);
            if(strcmp(load->password, p->password) == 0)
            {
                printf("登录成功!\n");
                printf("%d\t%s\n%d\t%s\t%s\t", p->auth, p->usr_name,  p->id, p->stu_name, p->major);
                for(i = 0; i < 3; i++)
                    printf("%s:%f\n", p->s[i].course_name, p->s[i].score);
                return p;
            }
            else
                continue;
            p = p->next;
        }
    }
    if( j == 3)    printf("用户不存在");
    return NULL;
}

void search(void)
{
    int j, i;
    Node *sear, *t;
    if((t = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    sear = head;
    printf("请输入要查询的用户:");
    scanf("%s",t->usr_name);
    for(j = 0; j < 2; j++)
    {
        while(sear == NULL)
        {
            if(strcmp(t->usr_name, sear->usr_name) == 0)
            {
                printf("%d\t%s\n%d\t%s\t%s\t", sear->auth, sear->usr_name,  sear->id, sear->stu_name, sear->major);
                for(i = 0; i < 3; i++)
                printf("%s:%f\n", sear->s[i].course_name, sear->s[i].score);
            }
            sear = sear->next;
        }
    }
    if(j == 2)
    {
        printf("用户不存在!请重新输入!");
        search();
    }
}
Node * insert(Node *tamp)
{
    Node  *end, *buff;
    if((head = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    if((buff = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    if((end = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    if(head == NULL)
    { head = tamp; head->next = end;}
    while(end == NULL)
    {
        buff = end;
        buff = tamp;
        end = buff->next;
    }
    return head;
    free(tamp);free(buff);free(end);
}
void  addinfo(void)
{
    int i, change = 0;
    Node *add;
    if((add = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    printf("Please input user's group: student(0) or manager(1):");
    scanf("%d", &add->auth);
    printf("Please input user's name:");
    scanf("%s", add->usr_name);
    printf("Please input user's password, the namber of password no more than 18:");
    scanf("%s", add->password);
    if(add->auth == 0)
    {
        printf("Please input student's id number:");
        scanf("%d", &add->id);
        printf("Please input student's major, the number of major no more than 32:");
        scanf("%s", add->major);
        for(i = 0; i < 3; i++)
        {
            printf("Pleaseinput the score of %d:",i+1);
            scanf("%s,%f",add->s[i].course_name, add->s[i].score);
        }
    }
    printf("%d\t%s\n%d\t%s\t%s\t", add->auth, add->usr_name, add->id, add->stu_name, add->major);
    for(i = 0; i < 3; i++)
    printf("%s:%f\n", add->s[i].course_name,add->s[i].score);
    printf("Would you want to change? input any but 0 to change\n");
    scanf("%d",&change);
    if(change != 0)
        addinfo();
    insert(add);
    free(add);
}
void fileload(void)
{
    FILE *info;
    Node *tamp;
    if((tamp = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    info = fopen (Name , "r");
    if(info == NULL)
        info = fopen(Name, "a+");
    fclose(info);
    info = NULL;
    info = fopen(Name, "r+");
    if(info == NULL)
    {
        printf("Open ERROR!\n");
        exit(0);
    }
    while(1)
    {
        fread(tamp, sizeof(Node), 1, info);
        if(tamp == NULL)
            break;
        insert(tamp);
    }
    free(tamp);
    fclose(info);
}
void manu(void)
{
    int select = 0;
    Node *l;
    if((l = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    while(1)
    {
        printf("=======================================================================================\n");
        printf("1 loading\t2 addinfo\t3 search\t4 passwd\t5 delete\t6 saveinfo\t7 exit\n");
        printf("=======================================================================================\n");
        printf("Please input your select:");
        scanf("%d", &select);
        switch(select)
        {
            case 1:
                    if(head == NULL)
                    {
                        printf("have no user!Please Add Information!\n");
                        addinfo();
                    }
                    else
                    {
                        l = loading();
                        manu();
                        break;
                    }
            case 2:
                   if( l->auth == 1)
                   { addinfo(); manu(); break;}
                   else
                       printf("You are limited!Try again!\n");
            case 3:search();manu();break;
            case 4:passwd( l );manu();break;
            case 5:
                   if(l->auth == 1) delete();
                   else
                       printf("You are limited!");
                   manu();
                   break;
            case 6:saveinfo();manu();break;
            case 7:exit(0);break;
            default:
                   printf("Try again!\n");manu;
        }
    }
}
int main(int argc, char *argv[])
{
    if((head = (Node *)malloc(sizeof(Node))) == NULL)
    {
        printf("can't malloc!");
        exit(0);
    }
    fileload(    );
    printf("Administrator of Student Information Manage\n");
    manu();
    return 0;
}
搜索更多相关主题的帖子: search include loading insert write 
2015-04-17 08:08
快速回复:如何形成(建立)单链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019215 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved