void loadfacility(node* head)
{
FILE* file = fopen(".u.info", "r");
if (!file) {
printf("没有学生文件,跳过读取\n");
return;
}
node* fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL;
node* move = head->next;
node* p = move->next;
while (fread(&fresh->facility, sizeof(Facility), 1, file) == 1)
{
if (head->next == NULL)
{
move->next = fresh;
move = fresh;
fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL; continue;
}
if (fresh->facility.type != p->sibling->facility.type)
{
p->sibling = fresh;
p = fresh;
fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL;
}
move->next = fresh;
move = fresh;
fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL;
}
free(fresh);
fclose(file);
printf("读取成功");
}
void savefacility(node* head)
{
FILE* file = fopen(".u.info", "w");
if (file == NULL)
{
printf("打开文件失败");
return;
}
node* move = head->next;
node* p = move->next;
while (move->sibling != NULL)
{
if (fwrite(&move->facility, sizeof(Facility), 1, file) != 1)
{
printf("写入失败\n");
return;
}
while (p->next != NULL)
{
if (fwrite(&p->facility, sizeof(Facility), 1, file) != 1)
{
printf("写入失败\n");
return;
}
p = p->next;
}
move = move->sibling;
p = move->next;
}
fclose(file);
}
void insert(node*& head)
{
node* st[100];
int front, rear;
front = rear = -1;
node* fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL;
printf("请输入设备的编号,名字,类型,库存\n");
scanf("%d %s %s %d", &fresh->facility.number, fresh->facility.name, fresh->facility.type, &fresh->facility.kc);
node* move = head;
node* check = head->next;
node* p = head->sibling;
if (check == NULL)
check = fresh;
while (p != NULL)
{
if (p->facility.type == fresh->facility.type)
{
p->next = fresh; break;
}
else if (p->sibling == NULL)
p = fresh;
p = p->sibling;
}
rear = (rear + 1) % 100;
st[rear] = move;
while (rear != front)
{
front = (front + 1) % 100;
if (st[front]->next != NULL)
{
rear = (rear + 1) % 100;
st[rear] = st[front]->next;
if (st[rear]->facility.number == fresh->facility.number)
{
printf("编号以重复请重新输入编号:\n");
scanf("%d", &fresh->facility.type);
}
while (st[front]->sibling != NULL)
{
rear = (rear + 1) % 100;
st[rear] = st[front]->sibling;
if (st[rear]->facility.number == fresh->facility.number)
{
printf("编号以重复请重新输入编号:\n");
scanf("%d", &fresh->facility.type);
}
p = st[front]->sibling;
}
}
}
savefacility(head);
system("cls");
}
为什么这个head->next后面的值为什么没有改变,我在insert函数中改变了head->next的指向,在打印函数中prints中head->next的指向还是空,我void insert(node*& head)没有整体改变head的值嘛
在函数insert中改变了head->next指向后,head指针本身并没有被改变。因此在其他函数中调用head时,其指向始终没有发生改变,仍然指向原来的位置,这是您需要考虑的一点。如果您想要改变head指针本身的位置,建议将参数改为**head,这样可以传递head指针的地址,让函数执行后能够改变head指针指向的位置。
下面是对insert函数进行修改,使其能够改变head指针本身的位置的示例:
void insert(node** head) // 将参数改为二级指针
{
node* fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
fresh->sibling = NULL;
printf("请输入设备的编号,名字,类型,库存\n");
scanf("%d %s %s %d", &fresh->facility.number, fresh->facility.name, fresh->facility.type, &fresh->facility.kc);
node* move = *head;
node* check = (*head)->next;
node* p = (*head)->sibling;
if (check == NULL)
check = fresh;
while (p != NULL)
{
if (p->facility.type == fresh->facility.type)
{
p->next = fresh; break;
}
else if (p->sibling == NULL)
p = fresh;
p = p->sibling;
}
// 改变head指针的位置
if (move == NULL)
*head = fresh;
else
{
while (move->sibling != NULL)
move = move->sibling;
move->sibling = fresh;
}
savefacility(*head);
system("cls");
}
在这个示例中,我们将函数参数从node*& head
改为node** head
,并在函数修改head指针的位置时使用二级指针。同时再函数结尾处也需要相应地改为传递指针的地址。这样可以保证函数执行后能够改变head指针指向的位置。