附上代码
#include<stdio.h>
#include<stdlib.h>
#define MAXDT 50
typedef struct node
{
int data;
struct node *next;
} Node;
Node *Initialize();
int menu(Node *head);
void ReadFile(Node *head);
void Create(Node *head);
void Traverse(Node *head);
void Insert(Node *head);
void Delete(Node *head);
void Sort(Node *head);
void Save(Node *head);
int tag=1;
int count = 0;
char FileName[20]="ListData";
Node Element[MAXDT];
int menu(Node *head)
{
system("cls");
if(tag)
{
tag=0;
ReadFile(head);
}
int relect;
printf("1.创建链表\n");
printf("2.遍历链表\n");
printf("3.插入元素\n");
printf("4.删除元素\n");
printf("5.排序\n");
printf("6.保存\n");
printf("输入:");
scanf("%d",&relect);
return relect;
}
//读文件
void ReadFile(Node *head)
{
printf("%d",count);
system("color a");
FILE *ListData;
Node *p=head->next;
if((ListData=fopen(FileName,"a+b"))==NULL)//fopen出现,记得fclose
{
printf("Can*t open %s file\n", FileName);
exit (1);
}
rewind(ListData);
while(count<MAXDT && fread(&Element[count],sizeof(Node),1,ListData)==1)
{
count++;
}
fclose(ListData);
printf("Read [%s] successfully! There are %d datas.\n",FileName,count);
}
//初始化单链表
Node *Initialize()
{
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
return head;
}
//创建链表
void Create(Node *head)
{
Node *tail=head;
int n,i,weight=0;
count = 0;
printf("输入元素个数");
scanf("%d",&n);
for(i=0; i<n; i++)
{
Node *element = (Node *)malloc(sizeof(Node));
printf("Input data of node:");
scanf("%d",&weight);
element->data = weight;
Element[i] = *element;
tail->next = element;
element->next =NULL;
tail = element;
count++;
}
printf("共有%d个元素",i);
system("pause");
}
//遍历
void Traverse(Node *head)
{
int i;
if(count == 0)
{
printf("数据为空");
}
printf("共有%d个元素",count);
for(i=0; i<count; i++)
{
printf("%d ",Element[i].data);
}
printf("\n");
}
void Insert(Node *head)
{
}
void Delete(Node *head)
{
}
void Sort(Node *head)
{
}
void Save(Node *head)
{
Node *p=head->next;
FILE *Data;
Data=fopen(FileName,"wb");
Data=fopen(FileName,"r+b");
fwrite(Element,sizeof(Node),count,Data);
fclose(Data);
printf("保存数据成功!");
exit(0);
}
int main(void)
{
Node *head=NULL;
head=Initialize();
while(1)
{
switch(menu(head))
{
case 1:
Create(head);
break;
case 2:
Traverse(head);
system("pause");
break;
case 3:
Insert(head);
break;
case 4:
Delete(head);
break;
case 5:
Sort(head);
break;
case 6:
Save(head);
break;
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define MAXDT 50
typedef struct node
{
int data;
struct node *next;
} Node;
Node *Initialize();
int menu(Node *head);
void ReadFile(Node *head);
void Create(Node *head);
void Traverse(Node *head);
void Insert(Node *head);
void Delete(Node *head);
void Sort(Node *head);
void Save(Node *head);
int tag=1;
int count = 0;
char FileName[20]="ListData";
Node Element[MAXDT];
int menu(Node *head)
{
system("cls");
if(tag)
{
tag=0;
ReadFile(head);
}
int relect;
printf("1.创建链表\n");
printf("2.遍历链表\n");
printf("3.插入元素\n");
printf("4.删除元素\n");
printf("5.排序\n");
printf("6.保存\n");
printf("输入:");
scanf("%d",&relect);
return relect;
}
//读文件
void ReadFile(Node *head)
{
printf("%d",count);
system("color a");
FILE *ListData;
Node *p=head->next;
if((ListData=fopen(FileName,"a+b"))==NULL)//fopen出现,记得fclose
{
printf("Can*t open %s file\n", FileName);
exit (1);
}
rewind(ListData);
while(count<MAXDT && fread(&Element[count],sizeof(Node),1,ListData)==1)
{
count++;
}
fclose(ListData);
printf("Read [%s] successfully! There are %d datas.\n",FileName,count);
}
//初始化单链表
Node *Initialize()
{
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
return head;
}
//创建链表
void Create(Node *head)
{
Node *tail=head;
int n,i,weight=0;
count = 0;
printf("输入元素个数");
scanf("%d",&n);
for(i=0; i<n; i++)
{
Node *element = (Node *)malloc(sizeof(Node));
printf("Input data of node:");
scanf("%d",&weight);
element->data = weight;
Element[i] = *element;
tail->next = element;
element->next =NULL;
tail = element;
count++;
}
printf("共有%d个元素",i);
system("pause");
}
//遍历
void Traverse(Node *head)
{
int i;
if(count == 0)
{
printf("数据为空");
}
printf("共有%d个元素",count);
for(i=0; i<count; i++)
{
printf("%d ",Element[i].data);
}
printf("\n");
}
void Insert(Node *head)
{
}
void Delete(Node *head)
{
}
void Sort(Node *head)
{
}
void Save(Node *head)
{
Node *p=head->next;
FILE *Data;
Data=fopen(FileName,"wb");
Data=fopen(FileName,"r+b");
fwrite(Element,sizeof(Node),count,Data);
fclose(Data);
printf("保存数据成功!");
exit(0);
}
int main(void)
{
Node *head=NULL;
head=Initialize();
while(1)
{
switch(menu(head))
{
case 1:
Create(head);
break;
case 2:
Traverse(head);
system("pause");
break;
case 3:
Insert(head);
break;
case 4:
Delete(head);
break;
case 5:
Sort(head);
break;
case 6:
Save(head);
break;
}
}
return 0;
}