【发布时间】:2015-02-06 19:52:26
【问题描述】:
我是 c 新手,正在学习链表,我决定创建一个图书馆管理器来使用链表管理我的书籍,但它似乎没有使用链表将数据保存到结构中。当有人试图添加一本新书时,该函数会点击 checkID 函数来查看具有相同 id 的书是否已经存在,但是当我执行显示信息时,结构中似乎没有任何内容。
void addBook()
{
int bookId;
BOOK *head = NULL, *temp;
temp = (BOOK*) malloc(sizeof(BOOK));
printf("\n Enter Book Details\n");
printf("Enter book ISBN: ");
scanf("%d", &bookId);
int bInd = checkID(bookId);
if (bInd == 0)
{
printf("Enter book title: ");
scanf("%s", &temp->chTitle);
printf("Enter book type (eg. magazine, novel): ");
scanf("%s", &temp->chType);
printf("Enter book publisher (eg. UTA): ");
scanf("%s", &temp->chPublisher);
printf("Enter book's number of pages: ");
scanf("%d", &temp->nPages);
printf("Enter book's price: ");
scanf("%f", &temp->nPrice);
printf("Enter year published: ");
scanf("%d", &temp->nPubYear);
//temp->next=NULL;
if (head == NULL)
{
head = temp;
temp->next = NULL;
}
else{
temp->next = head;
head = temp;
}
//BOOK[count].nStatus = IN;
count++;
}
else
{
printf("\nSorry another book with that id: Try again!\n" );
addBookFunction();
}
}
int checkID(int t)
{
BOOK *head;
while (head != NULL)
{
if (head->nID == t)
return 1;
head = head->next;
}
return 0;
}
【问题讨论】:
-
head已被请求它是一个全局变量。
标签: c linked-list