E
Erik
Hi, i have this struct and this linked list
/* structure describing a book.*/
typedef struct {
char code[40];
char author[50];
char title[100];
int year;
int reserved;
} Book;
/* linked list of books.*/
struct booklist_node {
Book *book;
struct booklist_node *next;
struct booklist_node *previous;
};
typedef struct booklist_node bookNode;
This procedure is signing book as reserved:
* Reserves book with given code.
Returns 0 if operation successful.
Returns -1 if book already reserved or failure. */
int ReserveBook(char *bookCode) {
bookNode *found;
found = SearchBookNode(bookCode);
/* if book is found.*/
if (found != NULL) {
/* if book isn't already reserved.*/
if (found->book->reserved == 0) {
found->book->reserved = 1;
/* reserve book and communicate success.*/
return 0;
}
else {
/* book already reserved, communicate failure.*/
return -1;
}
}
else {
/* book not found, communicate failure.*/
return -1;
}
}
/* Searches book with given code in books list
Returns bookNode pointer if book found.
Returns NULL if book not found.*/
bookNode *SearchBookNode(bookNode *list, char *code) {
bookNode *cur = list;
while (cur != NULL) {
if (strcmp(code, cur->book->code) == 0) {
return cur;
}
cur = cur->next;
}
return NULL;
}
The problem is that right after I've called ReserveBook(code), if I
print the list the book with code <code> is signed as
reserved(book->reserved=1) but when after some time i print the list
again, the book is now signed as not reserved(book->reserved = 0)... I
can't figure out how this happens, as I do not modify the list in
meanwhile.
thanks
/* structure describing a book.*/
typedef struct {
char code[40];
char author[50];
char title[100];
int year;
int reserved;
} Book;
/* linked list of books.*/
struct booklist_node {
Book *book;
struct booklist_node *next;
struct booklist_node *previous;
};
typedef struct booklist_node bookNode;
This procedure is signing book as reserved:
* Reserves book with given code.
Returns 0 if operation successful.
Returns -1 if book already reserved or failure. */
int ReserveBook(char *bookCode) {
bookNode *found;
found = SearchBookNode(bookCode);
/* if book is found.*/
if (found != NULL) {
/* if book isn't already reserved.*/
if (found->book->reserved == 0) {
found->book->reserved = 1;
/* reserve book and communicate success.*/
return 0;
}
else {
/* book already reserved, communicate failure.*/
return -1;
}
}
else {
/* book not found, communicate failure.*/
return -1;
}
}
/* Searches book with given code in books list
Returns bookNode pointer if book found.
Returns NULL if book not found.*/
bookNode *SearchBookNode(bookNode *list, char *code) {
bookNode *cur = list;
while (cur != NULL) {
if (strcmp(code, cur->book->code) == 0) {
return cur;
}
cur = cur->next;
}
return NULL;
}
The problem is that right after I've called ReserveBook(code), if I
print the list the book with code <code> is signed as
reserved(book->reserved=1) but when after some time i print the list
again, the book is now signed as not reserved(book->reserved = 0)... I
can't figure out how this happens, as I do not modify the list in
meanwhile.
thanks