可以帮忙看一下这些代码吗?该代码的目的是实现物品
的输入,然后把输入的物品用链表串联起来,老师我我们写一个物品竞拍的管理系统,
刚开始就遇到问题了,求大神帮帮;
#include
#include
#include
#include
#include
#include
#include
typedef struct things //物品信息:编号,名称,价格,朝代,简介
{
int id;
char name[30];
int price;
char dynasty[10];
char introduce[100];
struct things *next;
struct user *user;
struct UserThings *UserThings;
}things,*thing;
//定义全局变量
thing head;
void insert_thing(thing thing1) //物品结构体插入
{
thing p1,s;
p1 = head;
s = thing1;
if(p1->next == NULL)
{
printf("jdj");
s->next = p1->next;
p1->next = s;
}
else
{
while(p1->next->id < s->id && p1->next != NULL)//按id从小到大排序
{
p1 = p1->next;
}
if(p1->next != NULL && p1->next->id == s->id)//如果有相同的编号
{
printf("该编号已存在,请从新输入\n");
}
else
{
s->next = p1->next;
p1->next = s;
}
}
}
void input_thing() //物品信息录入
{
thing thing1;
thing1 = (things*)malloc(sizeof(things));
printf("\n请输入物品数据,输入0结束输入.");
printf("\n请输入物品ID(8位数字):");
scanf("%d",&thing1->id);
while(thing1->id!=0)
{
//initialize and create
printf("\n请输入物品名称:");
scanf("%s",thing1->name);
printf("\n请输入物品朝代(如唐代):");
scanf("%s",thing1->dynasty);
printf("\n请输入物品价格:");
scanf("%d",&thing1->price);
printf("\n请输入物品介绍(140字以内):");
scanf("%s",thing1->introduce);
thing1->next=NULL;
thing1->user=NULL;//
insert_thing(thing1);
//select_n(p1);
printf("\n请输入物品ID(8位数字):");
scanf("%d",&thing1->id);
}
}
int main()
{
head = (things*)malloc(sizeof(things));//刚开始忘了给头指针分配空间
head->next = NULL;
input_thing();
printf("%d",head->next->id);//输出第一个节点(用来测试的)
}
问题:这个链表串联不起来,可能是我在使用指针 的过程中理解错了一些东西,求大神帮帮,谢谢。而且我在最后输入id跳出循环的时候,主函数的printf("%d",head->next->id)输出的是最后输入的id(也就是0,因为我跳出循环的条件是0)