设为首页 加入收藏

TOP

C语言实现链表(二)
2014-11-23 23:11:47 来源: 作者: 【 】 浏览:4
Tags:语言 实现
;
struct student *p2;
struct student *head;
int i = 0;
FILE *fp;

char file[20] = "d:/list/node.txt";
printf("\nFileName is: %s\n", file);
if((fp=fopen(file, "r"))==NULL)
{
printf("Can not open file: %s", file);
exit(0);
}
p2 = (struct student*)malloc(sizeof(*p1));
head = p2;
p1 = (struct student*)malloc(sizeof(*p1));
while(fread(p1, sizeof(*p1), 1, fp)==1)
{
i++;
p2->next = p1;
p2 = p1;
p1->next = NULL;
p1 = (struct student*)malloc(sizeof(*p1));
}
free(p1);
fclose(fp);
if(i==0)
{
printf("\nNo recored in %s ", file);
}
return head;
}

/* save the list result to file */
void savefile(struct student* head)
{
struct student *p1;
FILE *fp;
char file[20] = "d:/list/node.txt";
printf("\nFileName: %s", file);
printf("\nPlease wait.../n");
if((fp=fopen(file,"w"))==NULL)
{
printf("\nCan not open file: %s", file);
exit(0);
}
p1 = head->next;
while(p1!=NULL)
{
fwrite(p1, sizeof(struct student), 1, fp);
p1 = p1->next;
}
fclose(fp);
}

/* print the operate menu */
void showMenu()
{
printf("|-------------------------------------------------|\n");
printf("| Link List Opearte Menu |\n");
printf("|-------------------------------------------------|\n");
printf("| 1: Insert |\n");
printf("| 2: Delete |\n");
printf("| 3: Search |\n");
printf("| 4: FileIn |\n");
printf("| 5: Savefile |\n");
printf("| 6: Print List |\n");
printf("| 7: Print Menu |\n");
printf("| 8: Exit |\n");
printf("|-------------------------------------------------|\n");
}

/* main funtion */
void main()
{
int a=0;
struct student *la;
la = create();
/*
la = (struct student*)malloc(sizeof(struct student));
la->next = NULL;
*/
while(1)
{
showMenu();
scanf("\n%d", &a);
switch(a)
{
case 1: insert(la); break;
case 2: del(la); break;
case 3: search(la); break;
case 4: filein(); break;
case 5: savefile(la); break;
case 6: print(la); break;
case 7: showMenu(); break;
case 8: exit(1);
default: printf("\nInput error,Try again.");break;
}
}
}

作者“萧萧易水寒”

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇struct数据对齐与#pragma pack(n) 下一篇调试程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: