代码写出来了,不知道错砸哪里,请大神指点
(1)立有头指针的链表,输入4个商品的名称和价格,输出链表内容。(2)函数求出商品价格总和
(3)在该链表中查找某商品,找到输出商品信息,找不到显示:找不到。
程序代码:#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define N sizeof(struct goods)
struct goods//声明结构体数组struct goods
{
char name[20];//商品的名字
int price;//商品的价格
struct goods *next;
};
int n;//定义全局变量n
struct goods *creat(void)//定义函数,此函数返回一个指向链表的头指针
{
struct goods *head;
struct goods *p1,*p2;
n=0;
p1=p2=(struct goods *)malloc(N);//开辟一个新单元
scanf("%s,%d",p1->name,&p1->price);//输入第一件商品的名字和价格
head=NULL;
while(p1->price)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct goods *)malloc(N);//开辟动态储存区,把起始地址赋给p1
scanf("%s,%d",p1->name,&p1->price);//输入其他商品的信息
}
p2->next=NULL;
return head;
}
void print(struct goods *head)//定义输出链表的函数
{
struct goods *a;//定义struct goods类型的变量a
printf("Their name and price are :\n");
a=head;
if(head!=NULL)
do
{
printf("%s%4f",a->name,a->price);
a=a->next;
}
while(a!=NULL);
}
int sum(struct goods *p)//定义求所有商品价格总和函数
{
struct goods *q;//定义struct goods类型的变量p
int add=0;
q=p;
while(q)
{
add+=q->price;
q=q->next;
}
return add;
}
void search(struct goods *head)//定义查找函数
{
char s[20];
scanf("%s",s);
struct goods *b=NULL;
b=head;
while(b!=NULL)
{
if(strcmp(b->name,s)==0)
printf("%s%4f",b->name,b->price);//如果有该商品则输出该商品信息
b=b->next;
}
if(b==NULL)
printf("Don't have the goods.\n");//如果没有则输出没有
}
void main()
{
struct goods *head;//定义struct goods类型的变量p
int t;
head=creat();
print(head);
t=sum(head);
search(head);
printf("The sum of price is %f",t);//输出各商品价格总和
}








