linked list - struct problem

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
 
S

santosh

Erik said:
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);

You've not passed the first parameter for SearchBookNode as you've
defined it below. The compiler should warn you about argument mismatch.
If you declare a function to take N parameters, you *must* pass N
arguments to it whenever you call it.
 
E

Erik

yeah, that's an error for me! sorry, didn't noticed it... but it still
doesn't works...
 
S

santosh

Erik said:
the original problem, the value set in ReserveBook doesn't remain set...

Please quote the post to which you're replying. Not all readers of
Usenet can access the previous articles. For methods to do so with
Google Groups, and general information regarding this group, please
take time to read the information given at the URLs mentioned at the
end of this post. Also please try to reduce your code the minimal
compilable version that still exhibits your problem. Then post it here.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>
<http://en.wikipedia.org/wiki/USENET>
 
K

Keith Thompson

Erik said:
the original problem, the value set in ReserveBook doesn't remain set...

You need to provide context when you post a followup. Read
<http://cfaj.freeshell.org/google/>. Also read
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>, particularly
the section that deals with Google Groups.

So you've corrected the problem that was pointed out. Are we supposed
to guess *how* you corrected it? If you'll post a small, complete,
self-contained program that illustrates your problem, we might be able
to help. If you post code fragments, we *might* be able to help, but
it's less likely.
 
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( 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);
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;

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
 
F

Flash Gordon

Erik said:
Hi, i have this struct and this linked list

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.
/* 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);

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.
/* if book is found.*/
if (found != NULL) {

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.
/* 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);

curr does not contain most of the members you show, so this does not
compile.
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;

newNode does not contain the above fields, so the above does not compile.
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

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.
 
B

Barry Schwarz

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( char *code) {
bookNode *cur = booksHead;

booksHead not yet defined.
while (cur != NULL) {

Header for NULL not included.
if (strcmp(code, cur->book->code) == 0) {

Header for strcmp not included.
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);

cur is a bookNode*, not a Book*. None of the members here are present
in the bookNode cur points to.
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;

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

You don't run this sample. It doesn't compile. Show us your real
code.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top