Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-07-2003, 05:05 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Question linked lists with classes

I am a little confused on this little program i have to write. I need to input a book name, isbn number and catagory(A, F,C, or O), then create a linked list with all that info. So here is what i have so far.

It inputs the info in main, then sends it to the Library class function add(). there are 5 pointers, ptFirstAll, which is the LL for all the books, and ptFirstA, F, C, O to put each catagory into its own LL. In the add function:

***There is a Book class that has the variables for the book attributes.***

Code:
ptFirstAll = new Book;
ptFirstAll->next = NULL;
would i just create one instance of book at the top and use that to reference the book attributes? since this function is of the library class, will it let me use the book variables(since they are private they shouldnt be able to, right?)? what would the syntax be for assigning all three values for each block of the LL?

Thanks - I have no idea why i am so confused w/ this stuff, it doesnt seem that hard, I just havent been able to grasp the concept. If anyone knows of some good tutorials on LLs, please post them.

- if you need any of the code, just ask

Last edited by GSXdan; 10-07-2003 at 05:07 PM.
GSXdan is offline   Reply With Quote
Old 10-07-2003, 07:16 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I don't think I get what you are asking.

The standard format of a linked list is like this:

class Books {
public:
char title[];
int ISBN;
Books* ptrNext;
...
};

All the attributes that you want to access should be public. To access all the books, you should just need one instance of Books. You should set the next pointer to null in the constructor.

What problems are you trying to solve? The C++ book I have explains it well, but it isn't online.
doctorgonzo is offline   Reply With Quote
Old 10-07-2003, 07:24 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
http://www.cs.bgsu.edu/zimmerman/cs2...5.fa03.p5a.pdf is the assignment.

i dont think i get what i am saying either.... ok....

so in the addbook function where i will be adding each book to the LL, i create one instance of book, Book x. Then i create a new element, ptFirstAll = new Book. I then set that to null, ptFirstAll-> = NULL. i got that part(as long as its correct).

how do i assign the title, isbn, catagory to that element is what i am trying to ask.

thanks
GSXdan is offline   Reply With Quote
Old 10-07-2003, 07:40 PM   #4
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
is it just x.setTitle()...etc, just the same as if I were assigning something to a normal class object?
GSXdan is offline   Reply With Quote
Old 10-07-2003, 08:22 PM   #5
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
ok got that part, just working on printing out all the info.... is there an easy way to do this?

thanks
GSXdan is offline   Reply With Quote
Old 10-07-2003, 08:38 PM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
To iterate through the list, you can do something like this:

Code:
Books* ptrBooks;

ptrBooks=ptrFirstAll;
while(ptrBooks!=NULL) {
cout<<ptrBooks->GetTitle; (or whatever method you want to use for output)
...
ptrBooks = ptrBooks->next;
}
It will loop through the linked list until it gets to the last item, which should have next set to null. Pretty simple.
doctorgonzo is offline   Reply With Quote
Old 10-07-2003, 08:59 PM   #7
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Well im not getting any error when i put that in, put nothing comes out Whats weird is when i try to put it in my main function i get a segmentation fault, but in the print function it runs? so i think it is not getting read in correctly. this is the code for my addbook function:

Code:
void Library:: AddBook(char * T, long I, char C) {

        Book x;
        ptFirstAll = new Book;
        ptFirstAll->next = NULL;

        x.setTitle(T);
        x.setisbn(I);
        x.setCatagory(C);



        if(C=='F') {
                ptFirstF = new Book;
                ptFirstF->next = NULL;
                x.setCatagory(C);
        }else if(C=='S') {
                ptFirstS = new Book;
                ptFirstS->next = NULL;
                x.setCatagory(C);
        }else if(C=='C') {
                ptFirstC = new Book;
                ptFirstC->next = NULL;
                x.setCatagory(C);
        }else if(C=='O') {
                ptFirstO = new Book;
                ptFirstO->next = NULL;
                x.setCatagory(C);
}

}
do you see anything wrong w/ that?

thanks
GSXdan is offline   Reply With Quote
Old 10-07-2003, 10:26 PM   #8
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
You aren't linking the list to anything. You simply create a new Book object on the heap, and set its next pointer to null. But the current objects don't point to the new object.

In order to add it properly, do something like this:

ptrNewFirst = new Book();
ptrNewFirst->next = ptrFirstAll;
ptrFirstAll=ptrNewFirst;

That way, the new Book points to the old first object, and the head (ptrFirstAll) now points to the new Book object.
doctorgonzo is offline   Reply With Quote
Old 10-08-2003, 05:30 AM   #9
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
ok, but now how do i reference the first object? Whne i try to print it prints the last object and stops.

and for some reason it only copies the first letter of the title....
GSXdan is offline   Reply With Quote
Old 10-08-2003, 08:07 AM   #10
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
In your Library class, you need to have a private variable that is a pointer to the first Book. You can still call it ptrFirstAll. In the Library constructor, set it to null. Then in the AddBook function, use the code that I provided. The code I provided adds things to the head of the list.

It may help to visualize it. This is how it starts off:

ptrFirstAll -> NULL

When you add a book, this is what you want it to look like:

ptrFirstAll -> Book1 -> NULL

When you add another book, this is what happens:

ptrFirstAll -> Book2 -> Book1 -> NULL

ptrFirstAll will always point to the object first in line. Then, you simply iterate through the rest of the list when you need to, like to print it out.

To see why only the first character of the title is being copied, you would need to show the code.
doctorgonzo is offline   Reply With Quote
Old 10-08-2003, 10:17 AM   #11
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
i have ptFirstAll set to null in the constructor, and this is the code i have in the addbook function:

Code:
Book x;
        Book * ptrNew;

        ptrNew = new Book;
        ptrNew->next = NULL;
        ptFirstAll = ptrNew;

        ptrNew->setTitle(T);
        ptrNew->setisbn(I);
        ptrNew->setCatagory(C);


          .....

when i try to print it it still only outputs the last element.

here is the print function:

Code:
void Library:: PrintAll() {

        Book * ptrBook;

        ptrBook = ptFirstAll;


cout << " List of all books: " << endl;
cout << "------------------------" << endl;

while(ptFirstAll!=NULL) {
        cout << "Title: " << ptFirstAll->getTitle()
                << "    ISBN: " << ptFirstAll ->getisbn()
                << "    Catagory: " << ptFirstAll->getCatagory() << endl;

        ptFirstAll = ptFirstAll->next;

sorry i am so confused right now, way too much going on.

Thanks for all your help.
GSXdan is offline   Reply With Quote
Old 10-08-2003, 10:41 AM   #12
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
The error is in your Add function. It should read as I have it before. Specifically, this line is the problem:

ptrNew->next = NULL;

It should read as this:

ptrNew->next = ptFirstAll;

Otherwise, you are breaking the chain.

In the while loop for the printout, don't use ptrFirstAll. use ptrBook. ptrFirstAll should only be used when making changes to the list. It shouldn't be used when iterating through the list.

Also, what is that declaration of Book x doing up there? Are you using it at all?
doctorgonzo is offline   Reply With Quote
Old 10-08-2003, 10:43 AM   #13
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Here is a program I threw together that illustrates everything working properly. Just add the attributes that you need:

Code:
#include <iostream>
#include <stdlib.h>

class Book {
public:
	Book* ptrNext;
	int ID;
	Book(int ID);
};

class Library {
public:
	Book* ptrFirstAll;
	void PrintAll();
	void AddBook(int ID);
};

Book::Book(int ID) {
	Book::ID = ID;
}

void Library::PrintAll() {
	Book* ptrBook;
	ptrBook = ptrFirstAll;
	cout << " List of all books: " << endl;
	cout << "------------------------" << endl;

	while(ptrBook!=(Book*)0) {
 	cout << "ID: " << ptrBook->ID << endl;

    ptrBook = ptrBook->ptrNext;
    }
}

void Library::AddBook(int ID) {
	Book* ptrNew = new Book(ID);
    ptrNew->ptrNext = ptrFirstAll;
    ptrFirstAll = ptrNew;
}

int main(int argc, char *argv[])
{
	Library* lib = new Library();
 	lib->AddBook(10);
 	lib->AddBook(20);
 	lib->AddBook(25);
 	lib->PrintAll();
 	system("PAUSE");
  	return 0;
}
doctorgonzo is offline   Reply With Quote
Old 10-08-2003, 11:20 AM   #14
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
nevermind, i got it figured out, but thanks for your help.
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:07 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2