473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

linked list - struct problem

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

May 29 '06 #1
9 2234
Erik wrote:
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.

May 29 '06 #2
yeah, that's an error for me! sorry, didn't noticed it... but it still
doesn't works...

May 29 '06 #3
Erik said:
yeah, that's an error for me! sorry, didn't noticed it... but it still
doesn't works...


What still doesn't work?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 29 '06 #4
the original problem, the value set in ReserveBook doesn't remain set...

May 29 '06 #5
Erik wrote:
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>

May 29 '06 #6
"Erik" <er********@gmail.com> writes:
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 29 '06 #7
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

May 30 '06 #8
Erik wrote:
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.
--
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
May 30 '06 #9
On 30 May 2006 01:58:09 -0700, "Erik" <er********@gmail.com> wrote:
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
May 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: PRadyut | last post by:
In this code i tried to add the elements in ascending order but the output is only 0 1 2 the rest of the elements are not shown. the code...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
11
by: bofh1234 | last post by:
Hello, I am having a problem with linked lists. My program is based on a client server model. The client sends some packets of data to the server. The server reads those packets and is...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.