动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C/C++教程 ]的信息



本月文章推荐
.C/C++中的整型常识.
.纵横捭阖C++之从异步谈起.
.如何关闭除自己程序外的所有其他.
.C++ Builder 设计工具.
.C语言的特点.
.Linux 下面使用 mtrace 来检查一.
.在 C++ 中控制Windows关机.
.C++Builder中"异形"按.
.C++ Builder 给窗体传.
..
.exit()子程序终止函数与return().
.关于初始化C++类成员.
.C++习题与解析-重载.
.DBGrid中的下拉列表和查找字段编.
.C++对象的放置.
.完整的读写函数.
.VC下调用ACM音频编程接口压缩Wav.
.用汇编写系统服务程序.
.C程序开发经典实例之2.
.钩子的应用: 程序运行监视.

一个简单的链表程序

发表日期:2008-3-8 |


/******************************************************************************/
/*                      作者: 神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;
/**************/
它老是自己在装载完之后又多加了一条空记录进来,我想了半天都没想明白到底是怎么回事。所以只好在装载完之后又多加了 删除多余记录 的代码。真是无奈啊。请大家帮我分析分析。
上一篇:探索C++的秘密之详解extern "C" 人气:218
下一篇:C++将DBGrid中数据导出到Word和Excel 人气:295
浏览全部C/C++的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-25 WikyBlog v1.7.0.1 多国语言版
2008-7-25 乐彼网上开店系统(56770 Eshop)
2008-7-25 赛特网站管理系统sitecms v3.6.0
2008-7-25 Modoer多功能点评系统 v1.0.1 Bu
2008-7-25 Shangducms Teamsuit! v1.1.0 开
2008-7-25 幻影动漫网视频系统(Ppdong) v1.
2008-7-25 acteecompany企业网站建设系统 v
2008-7-25 恒浪整合管理系统 ims v4.1 ACCE
2008-7-25 艺术图库系统 v1.0 beta
2008-7-19 UltraEdit 简体中文增强版 14.10
2008-7-19 CentOS 5.2 i386 LiveCD
2008-7-19 Snapture多功能相机 v1.4
2008-7-19 iAcces中文输入法 v1.0Build016
2008-7-19 Cookbook烹饪秘籍 v2.5
2008-7-19 苹果专用DVD转换工具 v1.1.59汉化
2008-7-19 Modem修复软件ZiPhone修改版04.0
2008-7-19 AgileMessenger即时通讯工具美化
2008-7-19 Sketches画图软件 v0.7b6破解版


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号