分享
 
 
 

一个简单的链表程序

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

/******************************************************************************/

/* 作者: 神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;

/**************/

它老是自己在装载完之后又多加了一条空记录进来,我想了半天都没想明白到底是怎么回事。所以只好在装载完之后又多加了 删除多余记录 的代码。真是无奈啊。请大家帮我分析分析。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有