View Full Version : linked lists with classes
GSXdan
10-07-2003, 06:05 PM
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.***
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 :)
doctorgonzo
10-07-2003, 08:16 PM
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.
GSXdan
10-07-2003, 08:24 PM
http://www.cs.bgsu.edu/zimmerman/cs215/cs215.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
10-07-2003, 08:40 PM
is it just x.setTitle()...etc, just the same as if I were assigning something to a normal class object?
GSXdan
10-07-2003, 09:22 PM
ok got that part, just working on printing out all the info.... is there an easy way to do this?
thanks
doctorgonzo
10-07-2003, 09:38 PM
To iterate through the list, you can do something like this:
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.
GSXdan
10-07-2003, 09:59 PM
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:
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
doctorgonzo
10-07-2003, 11:26 PM
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.
GSXdan
10-08-2003, 06:30 AM
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....
doctorgonzo
10-08-2003, 09:07 AM
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.
GSXdan
10-08-2003, 11:17 AM
i have ptFirstAll set to null in the constructor, and this is the code i have in the addbook function:
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:
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.
doctorgonzo
10-08-2003, 11:41 AM
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
10-08-2003, 11:43 AM
Here is a program I threw together that illustrates everything working properly. Just add the attributes that you need:
#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;
}
GSXdan
10-08-2003, 12:20 PM
nevermind, i got it figured out, but thanks for your help.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.