#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct weapon
{
int price;
int damage;
struct weapon *next;
};
struct weapon *create()
{
struct weapon *p1,*p2,*head;
int i=0;
p1 = p2 = (struct weapon*)malloc(sizeof(struct weapon));
head = NULL;
scanf_s("%d %d",&p1->price,&p1->damage);
while(0!=p1->price)
{
i++;
if( i = 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct weapon*)malloc(sizeof(struct weapon));
scanf_s("%d %d",&p1->price,&p1->damage);
}
p2->next = NULL;
return (head);
}
int main(void)
{
struct weapon *p;
p = create();
printf("%d,%d",p->price,p->damage);
system("pause");
return 0;
}
建议把i==1写成1==i,这种错误就可以避免了,至少系统会提醒你错了,不能给常量赋值,,,,,,,望采纳
我的问题都有点傻QAQ,if( i = 1)这种写法导致i始终是1然后head=p1一直执行,所以没有产生链表,只是把所有输入的值都赋值给head了