#define NULL 0
typedef struct book
{
char name[20];
int price;
int sum;
struct book *next;
}node,*list;
node* creatlist(list l)
{
node* p = NULL;
node* q;
int n,i;
l = (list)malloc(sizeof(node));
l->next = NULL;
p = l;
printf("how many book do we have: \n");
scanf("%d",&n);
for(i = 0;i < n; i++)
{
q = (node*)malloc(sizeof(node));
printf("input the name \n");
scanf("%s",q->name);
printf("\n");
printf("input the price \n");
scanf("%d",&q->price);
while(p->next != NULL)
{
p = p->next;
}
q->next = p->next;
p->next = q;
p->sum++;
}
return l;
}
int search(list l,char *p)
{
node* q;
q = l->next;
while(q != NULL)
{
if(!strncmp(q->name,p))
{
printf("price is %d\n",q->price);
return 0;
}
q = q->next;
}
printf("the book isn't here\n");
return -1;
}
list add(list l,char* p)
{
node* q;
list stu;
q = l->next;
while(q != NULL)
{
if((!strncmp(q->name,p))||(!strncmp(q->name,"0")))
{
stu = (list)malloc(sizeof(node));
printf("input the new book name and price\n");
scanf("%s%d",stu->name,&stu->price);
stu->next = q->next;
q->next = stu;
return l;
}
q = q->next;
}
printf("the position not exitst\n");
return -1;
}
int del(list l,char* p)
{
list q,before;
q = l->next;
before = l;
while(q != NULL)
{
if(!strncmp(q->name,p))
{
before->next = q->next;
free(q);
return l;
}
before = q;
q = q->next;
}
printf("the book you want del is not in our store\n");
}
int pri(list l)
{
list p;
p = l->next;
while(p != NULL)
{
printf("book name:%s\n",p->name);
printf("price:%d\n",p->price);
p = p->next;
}
return 0;
}
int main()
{
list l;
int cmd = 0;
l = (list)malloc(sizeof(node));
char name[20] = {0};
while(1)
{
printf("input your cmd\n");
printf("0:creat;1:add;2:del;3:serch;4print\n");
scanf("%d",&cmd);
if((cmd > 4)||(cmd < 0))
{
printf("cmd err\n");
continue;
}
switch(cmd)
{
case 0:
l = creatlist(l);
break;
case 1:
printf("which book you want to add after\n");
scanf("%s",name);
add(l, name);
break;
case 2:
printf("which book you want to del\n");
scanf("%s",name);
del(l,name);
break;
case 3:
printf("which book you want to serch\n");
scanf("%s",name);
search(l,name);
break;
case 4:
pri(l);
break;
default:
printf("cmd err\n");
}
}
}