首页 > 学院 > 开发设计 > 正文

一个简单的链表程序

2019-11-17 05:19:07
字体:
来源:转载
供稿:网友
/******************************************************************************/
/*                      作者: 神vlinux飘飘                                    */
/*                        bbs.bc-cn.net                                       */
/*                      时间:2005年1月13日                                    */
/*                      版权没有,盗版不究                                    */
/******************************************************************************/#include <stdio.h>/*数据结构*/
strUCt card
{
        char name[30];
        char tel[30];
        char zip[255];
        struct card *front_point;
        struct card *next_point;
};struct card *head_point; /*头节点*/
struct card *end_point;  /*尾节点*/
struct card *now_point;  /*当前节点,很多操作都是围绕这个节点来完成*//*命令函数区*/
void uppoint();         /*当前节点上移一条记录*/
void downpoint();       /*当前节点下移一条记录*/
void save();            /*保存文件*/
void new();             /*在当前节点之后创建一个新的记录,并把当前节点指向新记录*/
void ver();             /*显示版本号,无聊....*/
void del();             /*删除当前节点的记录*/
void list();            /*显示所有的记录*/
void point();           /*显示当前节点所指向的记录*/
void quit();            /*退出程序(推荐选项)*/
void find();            /*查找记录,并且把节点指向找到的记录.有两种查找方式(按名字,按电话)*/
void cls();             /*清屏*/
/*功能函数区*/
void load();                    /*装载文件*/
void commandline();             /*命令行,你所有的指令都由它来理解,执行*/
void show(struct card *);       /*打印记录*/
void error(int );               /*错误系统,好象显得有点多余*/
void trade(struct card*,struct card*); /*交换两个记录在链表中的位置uppoint和downpoint用*/
struct card *search(char *,int);      /*查找记录*/
void main()
{
        ver();
        load();
        commandline();
}
void commandline()
{
char command[100];        PRintf("Card Master!/nWrite by vlinux/n");
        for(;;)
        {
                printf("CMD:");
                gets(command);                if( strcmp(command,"new")==0 )          new();
                else if( strcmp(command,"del")==0 )     del();
                else if( strcmp(command,"find")==0 )    find();
                else if( strcmp(command,"list")==0 )    list();
                else if( strcmp(command,"point")==0 )   point();
                else if( strcmp(command,"quit")==0 )    quit();
                else if( strcmp(command,"cls")==0 )     cls();
                else if( strcmp(command,"ver")==0 )     ver();
                else if( strcmp(command,"save")==0 )    save();
                else if( strcmp(command,"uppoint")==0 ) uppoint();
                else if( strcmp(command,"downpoint")==0)downpoint();
                else                      error(0);
        }
}

void show(struct card *show_point)
{
        printf("/n_______________________________/n");
        printf("NAME : %s/n",show_point->name);
        printf("TEL  : %s/n",show_point->tel);
        printf("ZIP  : %s",show_point->zip);
        printf("/n_______________________________/n");
}
void list()
{
struct card *list_point;
int count=0;        if( head_point->next_point == end_point )
        {
                printf("This is an empty Card!/n");
                return;
        }        list_point=head_point->next_point;
        for(;list_point->next_point!=NULL;list_point=list_point->next_point)
        {
                show(list_point);
                count++;
        }
        printf("Total %d/n/n",count);
}void point()
{
        show(now_point);
}
void new()
{
struct card *new_point;        new_point=(struct card*)malloc(sizeof(struct card));        new_point->next_point=now_point->next_point;
        new_point->front_point=now_point;
        now_point->next_point=new_point;        now_point=new_point;        printf("Enter NAME:");
        gets(new_point->name);        printf("Enter TEL:");
        gets(new_point->tel);        printf("Enter ZIP:");
        gets(new_point->zip);        printf("Creat a new card/n");
        show(new_point);
        printf("/n");
}

void find()
{
struct card *find_point;
char Words[255];        printf("FIND..../n");
        printf("Enter your search about? (name/tel) :");
        gets(words);        if( strcmp(words,"name")== 0 )
        {
                printf("Enter NAME:");
                gets(words);
                find_point=search(words,0);
                if( find_point==NULL )
                {
                        error(1);
                        return;
                }                else
                {
                        show(find_point);
                        now_point=find_point;
                        return;
                }
        }        if( strcmp(words,"tel")== 0 )
        {
                printf("Enter TEL:");
                gets(words);
                find_point=search(words,1);
                if( find_point==NULL )
                {
                        error(1);
                        return;
                }
                else
                {
                        show(find_point);
                        now_point=find_point;
                        return;
                }
        }
        printf("Error!/n/n");
}
struct card * search(char *words,int type)
{
struct card *search_point;        search_point=head_point->next_point;        if( type == 0 )
        {
                for(;search_point->next_point!=NULL;search_point=search_point->next_point)
                {
                        if( strcmp(search_point->name,words) == 0 )
                 {
                                return search_point;
                 }
                }
                return NULL;
        }        if( type == 1 )
        {
                for(;search_point->next_point!=NULL;search_point=search_point->next_point)
                {
                        if( strcmp(search_point->tel,words) == 0 )
                 {
                                return search_point;
                 }
                }
                return NULL;
        }
}

void quit()
{
char words[10];
        printf("Quit,are you sure?(yes/no)");
        gets(words);
        if( (strcmp(words,"yes"))!=0 )
        {
                printf("Drop action!/n/n");
                return;
        }
        exit(0);
}
void error(int type)
{
        if( type == 0)
        {
                printf("Bad command!/n");
                return;
        }        if( type == 1)
        {
                printf("Find Nothing!/n");
                return;
        }
}void del()
{
struct card *temp_point;
char words[255];        if(head_point->next_point->next_point==NULL)
        {
                error(1);
                return;
        }        show(now_point);
        printf("It will delete this card, are you sure?(yes/no) ");
        gets(words);
        if( strcmp(words,"yes")!=0 )
        {
                printf("drop this action!/n/n");
                return;
        }        printf("Del the card:/n/n");        temp_point=now_point->front_point;        now_point->next_point->front_point=now_point->front_point;
        now_point->front_point->next_point=now_point->next_point;
        free(now_point);        now_point=temp_point;
}
void cls()
{
int i;
        for(i=0;i<60;i++)
                printf("/n");
}void ver()
{
        printf("/n____________________________________________________________________________");
        printf("/n                                  Build by vlinux");
        printf("/n                                   @CopyRight");
        printf("/n                                GXDX.NN.GX.China");
        printf("/n----------------------------------------------------------------------------");
        printf("/n/n");
}
void load()
{
FILE *fp;
struct card *load_point;
struct card *temp_point;        if( (fp=fopen("cardsave.mmd","rb"))==NULL )
        {
                printf("Creat a new cardsave!/n");
                if( (fp=fopen("cardsave.mmd","wb"))==NULL)
                {
                        printf("Sorry~Can not creat files!/n/n");
                        return;
                }
                printf("ok!/n");
                fclose(fp);
        }

        head_point=(struct card*)malloc(sizeof(struct card));
        end_point=(struct card*)malloc(sizeof(struct card));
        head_point->front_point=NULL;
        head_point->next_point=end_point;
        end_point->front_point=head_point;
        end_point->next_point=NULL;        strcpy(head_point->name,"HEAD");
        strcpy(head_point->tel,"HEAD");
        strcpy(head_point->zip,"HEAD");        strcpy(end_point->name,"END");
        strcpy(end_point->tel,"END");
        strcpy(end_point->zip,"END");        now_point=head_point;        while(!feof(fp))
        {
                load_point=(struct card*)malloc(sizeof(struct card));
                fread(load_point,sizeof(struct card),1,fp);
                load_point->next_point=now_point->next_point;
                load_point->front_point=now_point;
                now_point->next_point=load_point;
                now_point=load_point;
        }        fclose(fp);        temp_point=now_point->front_point;        now_point->next_point->front_point=now_point->front_point;
        now_point->front_point->next_point=now_point->next_point;        free(now_point);        now_point=temp_point;        printf("Load files finished!/n");
        return;
}
void save()
{
FILE *fp;
struct card *save_point;
char words[10];
        printf("It will change cardsave.mmd file,are you sure?(yes/no)");
        gets(words);
        if( (strcmp(words,"yes"))!= 0)
        {
                printf("Drop action!/n/n");
                return;
        }
        printf("Saving.../n");
        if( (fp=fopen("cardsave.mmd","wb"))==NULL )
        {
                printf("Can't save files!/n");
                return;
        }
        save_point=head_point->next_point;        for(;save_point->next_point!=NULL;save_point=save_point->next_point)
        {
                fwrite(save_point,sizeof(struct card),1,fp);
        }        printf("Save finished!/n/n");
}
void uppoint()
{
        if( now_point->front_point==head_point )
        {
                printf("Sorry can not move TOP!/n/n");
                return;
        }        printf("Trade cards:");
        show(now_point);
        show(now_point->front_point);
        trade(now_point->front_point,now_point);
        printf("Trade finished!/n/n");
}
void downpoint()
{
        if( now_point->next_point==end_point )
        {
                printf("Sorry can not move END!/n/n");
                return;
        }        printf("Trade cards:");
        show(now_point);
        show(now_point->next_point);
        trade(now_point,now_point->next_point);
        printf("Trade finished!/n/n");
}
void trade(struct card *a_point,struct card *b_point)
{
        a_point->front_point->next_point=b_point;
        b_point->next_point->front_point=a_point;        a_point->next_point=b_point->next_point;
        b_point->front_point=a_point->front_point;        a_point->front_point=b_point;
        b_point->next_point=a_point;
}



使用说明:本程序采用的是双链表的结构,并且头链HEAD和尾链END是指定的,不能更改。
<HEAD>-----<记录1>----<记录2>---- ... ----<记录3>----<END>很多操作都是围绕now_point(当前节点)来进行的。new 在当前节点后插入一个新的记录,并且把当前节点指向新记录;
del 删除当前节点的记录,当前节点自动指向前一个记录;
ver 显示版本号,没什么用,纯粹娱乐;
list 显示所有记录;
point 显示当前节点所指向的记录;
uppoint 当前节点往前移一条记录,不改变当前节点的指向;
downpoint当前节点往后移动一条记录,同样不改变当前节点的指向;
find 可以按照name或者tel来进行查找,假如找到,当前节点自动指向它;
cls 清屏;
quit 退出程序。
由于将近半年的时间没看C语言,而且又曾经误入学习编程的误区(跑去看java),现在回来感到很多东西都忘光了。所以随便写写个代码看起来比较长的程序来练练手。这个程序纯粹是娱乐所做,没什么实际意义。
当然,假如你把数据结构和new函数改改就可以变成图书馆治理系统或者是学生治理系统什么的。由于这个程序是在半夜写的,所以写完之后第二天早上赶忙起来检查了一遍,重新写了中文注释。我想,既然printf("Hellow MM");都可以拿出来show show,那我的应该没问题吧~~哦~还有一个重要的问题:
请大家帮我查查我的load函数的装载文件部分,while(!feof(fp))
        {
                load_point=(struct card*)malloc(sizeof(struct card));
                fread(load_point,sizeof(struct card),1,fp);
                load_point->next_point=now_point->next_point;
                load_point->front_point=now_point;
                now_point->next_point=load_point;
                now_point=load_point;
        }
        fclose(fp);/*删除多余记录*/
        temp_point=now_point->front_point;
        now_point->next_point->front_point=now_point->front_point;
        now_point->front_point->next_point=now_point->next_point;
        free(now_point);
        now_point=temp_point;
/**************/
它老是自己在装载完之后又多加了一条空记录进来,我想了半天都没想明白到底是怎么回事。所以只好在装载完之后又多加了 删除多余记录 的代码。真是无奈啊。请大家帮我分析分析。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表