BOOK* add(BOOK *head) { BOOK *p=NULL,*tail=head; p=(BOOK *)malloc(sizeof(BOOK)); printf("请输入ISBN编号:"); scanf("%s",p->isbn); printf("请输入书名:"); scanf("%s",p->name); printf("请输入作者:"); scanf("%s",p->author); printf("请输入出版社:"); scanf("%s",p->publisher); printf("请输入单价:"); scanf("%f",&(p->price)); p->next=NULL; if(head==NULL) { head=p; } else { while(tail->next!=NULL) tail=tail->next; tail->next=p; } return head; }
时间: 2024-04-05 21:29:33 浏览: 9
这段代码是一个添加图书信息的函数,使用链表来存储图书信息。它首先定义了一个指针p和tail,tail指向链表的尾部。然后使用malloc函数为p分配内存空间。接下来提示用户输入图书的各项信息,包括ISBN编号,书名,作者,出版社和单价,并将这些信息存储到p指向的结构体中。然后将p的next指针置为NULL。如果链表为空,将head指向p,否则将p添加到链表的尾部。最后返回head指针,即链表的头指针。
相关问题
struct node *head=NULL,*tail=NULL,p=NULL;
This code is not valid in C.
The "*" symbol is not used correctly.
Here is a corrected version:
```
struct node *head=NULL,*tail=NULL,*p=NULL;
```
This code declares three pointers to a struct node: head, tail, and p. All three pointers are initialized to NULL.
head = tail = p 这个的赋值顺序
这个赋值语句的执行顺序是从右往左。也就是先将 `p` 赋值给 `tail`,再将 `tail` 赋值给 `head`。这是因为在 C 语言中,赋值运算符 `=` 的优先级比较低,而赋值运算符的结合性是从右往左的。因此,这个赋值语句的执行顺序是先执行 `head = tail`,然后再执行 `tail = p`。