链表的操作很容易理解,直接上代码
#include
#include
#include
#include
#define NULL 0
using namespace std;
struct Book {
int id;
char name[20];
char author[20];
char publish[20];
float price;
struct Book *next;
};
struct Book *filein(void)
{
struct Book *p1,*p2,*head;
int i=0;
FILE *fp;
char file[20]="d:\\list.txt";
if ((fp=fopen(file,"rb"))==NULL)
{
printf("不能打开文件:%s",file);
exit(0);
}
head=p2=(struct Book *)malloc(sizeof(*p1));
head->next=NULL;
p1=(struct Book *)malloc(sizeof(*p1));
while (fread(p1,sizeof(*p1),1,fp)==1)
{
i++;
p2->next=p1;
p2=p1;
p1->next=NULL;
p1=(struct Book *)malloc(sizeof(*p1));
}
free(p1);
fclose(fp);
if (i==0){
printf(" 没有任何记录%s!");
}
return(head);
};
struct Book *creat(){
struct Book *head,*p,*q;
int a=1;
int id=1;
q=head=(struct Book*)malloc(sizeof(struct Book));
printf(".........................................输入图书信息...................................\n");
while(a!=0){
p=(struct Book*)malloc(sizeof(struct Book));
printf(" 输入图书名称\n");
scanf("%s",&p->name);
printf(" 输入作者名字\n");
scanf("%s",&p->author);
printf(" 输入出版社\n");
scanf("%s",&p->publish);
printf(" 输入售价\n");
scanf("%f",&p->price);
p->id=id;
p->next=NULL;
q->next=p;
q=p;
id++;
printf("\n");
printf("\n");
printf("....................是否要继续加入信息...................\n");
printf(".........................是请按1...................\n");
printf(".........................否请按0...................\n");
scanf("%d",&a);
}
return head;
};
void print(struct Book *head){
struct Book *p;
p=head->next;
printf("....................书单列表为...................\n");
printf(" ID 书名 作者 出版社 售价\n");
while(p!=NULL){
printf(" %d",p->id);
printf(" %s",p->name);
printf(" %s",p->author);
printf(" %s",p->publish);
printf(" %f\n",p->price);
p=p->next;
}
};
void delet(struct Book *head){
struct Book *p,*q;
p=head->next;
q=head;
int id;
int a=1;
printf("....................输入你想要删除的图书的ID...................\n");
scanf("%d",&id);
while(a){
while(p->next !=NULL && p->id!=id){
q=p;
p=p->next;
}
if(id==p->id){
q->next=p->next;
free(p);
printf("!!!!!!!!!删除成功!!!!!!!!!!!!\n");
}else{
printf("!!!!!!!!没有找到这本书!!!!!!!!!!!!\n");
}
printf("\n");
printf("\n");
printf("....................是否要继续删除信息...................\n");
printf(".........................是请按1...................\n");
printf(".........................否请按0...................\n");
scanf("%d",&a);
}
};
void save(struct Book *head)
{
struct Book *p1;
FILE *fp;
char file[20]="d:\\list.txt";
if ((fp=fopen(file,"wb"))==NULL)
{
printf("不能打开文件:%s/n",file);
exit(0);
}
p1=head->next;
while (p1!=NULL)
{
fwrite((void *)p1,sizeof(struct Book),1,fp);
p1=p1->next;
}
fclose(fp);
};
void insert(struct Book *head)
{
struct Book *p,*q;
int a =1;
int id=1;
printf(".........................................输入图书信息...................................\n");
q=head;
while(a!=0){
p=(struct Book*)malloc(sizeof(struct Book));
printf(" 输入图书名称\n");
scanf("%s",&p->name);
printf(" 输入作者名字\n");
scanf("%s",&p->author);
printf(" 输入出版社\n");
scanf("%s",&p->publish);
printf(" 输入售价\n");
scanf("%f",&p->price);
while(q->next!=NULL){
q=q->next;
id++;
}
p->id=id;
p->next=q->next;
q->next=p;
printf("\n");
printf("\n");
printf("....................是否要继续加入信息...................\n");
printf(".........................是请按1...................\n");
printf(".........................否请按0...................\n");
scanf("%d",&a);
}
}
int main()
{
int note;
struct Book *head;
do{
printf("........................功能列表...................\n");
printf(".............. 1.创建数组 ................\n");
printf(".............. 2.删除数据 ................\n");
printf(".............. 3.打印数据 ................\n");
printf(".............. 4.保存数据 ................\n");
printf(".............. 5.提取数据 ................\n");
printf(".............. 6.添加数据 ................\n");
printf(".............. 7.退出 ................\n");
scanf("%d",¬e);
switch(note){
case 1: head=creat();
break;
case 2: delet(head);
break;
case 3: print(head);
break;
case 4: save(head);
break;
case 5: head=filein();
break;
case 6: insert(head);
break;
case 7:
exit(0);
}
printf("%d",note);
}while(note<7);
return 0;
}
有不对的欢迎指出,代码有注释,讲述得不清楚得地方欢迎指出