编辑: 枪械砖家 | 2013-01-09 |
??? float price;
??? char pub[15];
??? struct node *link;
? };
?struct node *add(struct node *head 函数功能:创建结点,输入结点数据? ?void print(struct node *head)函数功能:链表输出? ?void save(struct node *head)函数功能:链表存储? void search(struct node *head)函数功能:链表结点查找? ?struct node *del(struct node *head)函数功能:结点删除? struct node *load(void)函数功能:从文件中读入数据,创建链表? ?struct node *mhdel(struct node *head)函数功能:结点模糊删除? void mhsearch(struct node *head)函数功能:链表结点模糊查找? 函数功能:创建结点,输入结点数据? 输入参数:链表头指针? 函数输出:链表头指针? ?struct node *add(struct node *head )? ?{ ?? int? i;
?? float jg;
?? struct node *p, *pnew;
?? pnew=(struct node *)malloc(sizeof(struct node));
?? ?? printf("Please input book name,price,publisher\n");
?? printf("name:");
scanf("%s",pnew->name);
?? printf("price:");
scanf("%f",&jg);
pnew->price=jg;
?? printf("publisher:");
scanf("%s",pnew->pub);
?? pnew->link=NULL;
?? if (head==NULL)return (pnew);
?? p=head;
?? while (p->link!=NULL)找到尾结点 */ ??? p=p->link;
?? p->link=pnew;
?? return (head);
} 函数功能:链表输出? 输入参数:链表头指针? 函数输出:无? ?void print(struct node *head)? ?{ ? struct node *p;
? p=head;
?? printf("name\tprice\tpublisher\n");
? while (p!=NULL) ?? { ?printf("%s\t%-5.
2f\t%-s\n",p->name,p->price,p->pub);
???? p=p->link;
?? } ? printf("\n\t\tPress any keys!");
?} 函数功能:链表存储? 输入参数:链表头指针? 函数输出:无? void save(struct node *head)?? { ? FILE *fp;
? struct node *p;
? if ((fp=fopen("library","wb"))==NULL) ?? {? printf("Can't open file!\n");
????? exit(1);
?? } ? p=head;
? while (p!=NULL) ?? { ???? fwrite(p,sizeof(struct node),1,fp);
???? p=p->link;
} ? fclose(fp);
} 函数功能:从文件中读入数据,创建链表? 输入参数:无? 函数输出:链表头指针? struct node *load(void){ ? FILE *fp;
? struct node *p,*pnew,*phead;
? if ((fp=fopen("library","rb"))==NULL) ?? {? printf("Can't open file!\n");
????? exit(1);
?? } ?? pnew=(struct node *)malloc(sizeof(struct node));
?? fread(pnew, sizeof(struct node),1,fp);
?? pnew->link=NULL;
?? phead=pnew;
? p=pnew;
? while (!feof(fp)) ? {? pnew=(struct node *)malloc(sizeof(struct node));
?if (1!=fread(pnew, sizeof(struct node),1,fp)) break;
?pnew->link=NULL;
?p->link=pnew;
?p=p->link;
? } ? fclose(fp);
? return(phead);
函数功能:链表结点查找? 输入参数:链表头指针? 函数输出:无? void search(struct node *head) { ? char sname[15];
? struct node *p;
? printf("Please input book name\n");
? printf("name:");
scanf("%s",sname);
? p=head;
? while (p!=NULL) ?? { if (strcmp(p->name,sname)==0) ? {??? printf(name??? price?? publshier\n");
??? printf("Book Found:? %s????? %5.2f??? %s\n",p->name,p->price,p->pub);
??? printf("\n\t\tPress any keys!");
??? return;
? } ???? p=p->link;
?? } ??? printf("Book Not Exist!\n");
??? printf("\n\t\tPress any keys !");
?} 函数功能:结点删除? 输入参数:链表头指针? 函数输出:返回链表头指针? ?struct node *del(struct node *head) ?{ ? char sn[15],s;