Erik wrote:[color=blue]
> Hi, i have this struct and this linked list[/color]
First off, the code you posted is not even close to compiling. Unless
your problem is that you can't get something to compile please provide a
small compile compilable set of sources.
[color=blue]
> /* 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);[/color]
You need to declare functions that return pointers *before* you first
call them. Otherwise, the compiler is *required* to assume the function
returns an int.
[color=blue]
> /* if book is found.*/
> if (found != NULL) {[/color]
The code you posted has not yet included any of the headers that provide
NULL, so this is clearly not the code you compiled and ran. I'm not
going to bother pointing out all the other errors, just a few, because
it may well be that all the errors are due to you not posting the code
you compiled.
[color=blue]
> /* 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( char *code) {
> bookNode *cur = booksHead;
> while (cur != NULL) {
>
> if (strcmp(code, cur->book->code) == 0) {
> return cur;
> }
> cur = cur->next;
> }
> return NULL;
>
> }
>
> /* Prints books informations of nodes in the list*/
> void PrintBookList(bookNode *list) {
> bookNode *cur = list;
> while (cur != NULL) {
> printf("\ncode: %s, author: %s, title: %s, year:%d, reserved:%d",
> cur->code, cur->author, cur->title, cur->year, cur->reserved);[/color]
curr does not contain most of the members you show, so this does not
compile.
[color=blue]
> cur = cur->next;
> }
> }
>
> This is a sample program
>
> //global variable list head pointer
> bookNode *booksHead;
>
> int main() {
>
> bookNode *newNode = malloc(sizeof(bookNode));
> strcpy(newNode->author, "me");
> strcpy(newNode->title, "mybook");
> strcpy(newNode->code, "123");
> newNode->year = 1245;
> newNode->reserved = 0;[/color]
newNode does not contain the above fields, so the above does not compile.[color=blue]
>
> InsertBookNode(newNode);
>
> PrintBookList(booksHead);
>
> if (ReserveBook("123")) {
> printf("\nres book ok");
> }
> else {
> printf("\nres book fail");
> }
>
> PrintBookList(booksHead);
>
> return 0;
> }
>
>
> So when I run this sample, the second call to PrintBookList reports
> that book 123 is not reserved , even if message "res book ok" is
> printed (telling that ReserveBook was successful), I could not figure
> out how this happens... thanks and excuse me[/color]
Based on the above the code could just as easily print, "We all live in
a yellow submarine and we are going to fire you out of the torpedo tube
for posting something that bares little resemblance to the code you
compiled." Although it is more likely that like me you would get a
couple of pages of errors on attempting to compile it.
--
Flash Gordon, living in interesting times.
Web site -
http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc