简单的链表和文件
2015-01-09 11:38
120 查看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE (S *)malloc(sizeof(S))
typedef struct stu
{
int no;
char name[20];
struct stu *next;
}S;
S *head;
void choice();
void init();
void bianli(S *head);
void save(S *head);
void menu();
void menu()
{
printf("\t\t\t菜单 \n");
printf("\t\t\t1.创建链表\n");
printf("\t\t\t2.遍历 \n");
printf("\t\t\t3.保存 \n");
}
void init()
{
S *p1,*p2;
head=SIZE;
head->next=NULL;
p1=p2=head;
while(1)
{
p1=SIZE;
printf("请输入学号\n");
scanf("%d",&p1->no);
if(p1->no==0)
{
p2->next=NULL;
break;
}
printf("请输入姓名\n");
scanf("%s",p1->name );
p2->next=p1;
p2=p1;
}
}
void bianli(S *head)
{
S *p1;
p1=head;
while(p1->next !=NULL)
{
p1=p1->next;
printf("%d %s",p1->no ,p1->name );
}
}
void save(S *head)
{
S *p1;
p1=head;
FILE *fp;
fp=fopen("qing.txt","w");
if(fp==NULL)
{
printf("not open the file");
exit(0);
}
while(p1->next )
{
p1=p1->next ;
fwrite(p1,sizeof(S),1,fp);
}
}
void choice()
{
int n;
while(1)
{
menu();
printf("请输入你要进行的操作\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
init();
break;
}
case 2:
{
bianli(head);
break;
}
case 3:
{
save(head);
break;
}
}
}
}
int main()
{
choice();
return 0;
}
#include<string.h>
#include<stdlib.h>
#define SIZE (S *)malloc(sizeof(S))
typedef struct stu
{
int no;
char name[20];
struct stu *next;
}S;
S *head;
void choice();
void init();
void bianli(S *head);
void save(S *head);
void menu();
void menu()
{
printf("\t\t\t菜单 \n");
printf("\t\t\t1.创建链表\n");
printf("\t\t\t2.遍历 \n");
printf("\t\t\t3.保存 \n");
}
void init()
{
S *p1,*p2;
head=SIZE;
head->next=NULL;
p1=p2=head;
while(1)
{
p1=SIZE;
printf("请输入学号\n");
scanf("%d",&p1->no);
if(p1->no==0)
{
p2->next=NULL;
break;
}
printf("请输入姓名\n");
scanf("%s",p1->name );
p2->next=p1;
p2=p1;
}
}
void bianli(S *head)
{
S *p1;
p1=head;
while(p1->next !=NULL)
{
p1=p1->next;
printf("%d %s",p1->no ,p1->name );
}
}
void save(S *head)
{
S *p1;
p1=head;
FILE *fp;
fp=fopen("qing.txt","w");
if(fp==NULL)
{
printf("not open the file");
exit(0);
}
while(p1->next )
{
p1=p1->next ;
fwrite(p1,sizeof(S),1,fp);
}
}
void choice()
{
int n;
while(1)
{
menu();
printf("请输入你要进行的操作\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
init();
break;
}
case 2:
{
bianli(head);
break;
}
case 3:
{
save(head);
break;
}
}
}
}
int main()
{
choice();
return 0;
}
相关文章推荐
- 循环双链表加目录IO实现简单的文件检索
- 简单的学生成绩管理系统 (用链表 多个文件)
- 链表中文件的简单操作
- 链表的一个简单应用---目录下的文件并排序
- aspx 文件上传(简单)
- 删除文件就这么简单
- 让客户端把ASP文件以XML的方式来处理最简单的实现方法.
- 一个查找替换文件的简单工具
- 简单封装的一个文件操作的类【原创】
- web.config文件自定义配置节的使用方法的一个简单例子
- 【原创】Jsp读取文件(简单的计数器)
- C#编一个简单的文件上传程式
- 用文件写入建立简单的网上调查
- struts简单例子的分析(含代码注释和配置文件说明)
- 简单的XML操作:XML文件创建
- 今天刚发现的好东西!让系统自动查找删除文件的办法!(简单!!!!)
- 【头文件】c++实现链表
- 最简单的XML格式Excel表格文件
- 简单链表
- 今天查了查msdn,做了个简单的批量文件更名的程序!