|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
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; 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. |
|
|
|
|
|
#2 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#3 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#4 |
|
Member (10 bit)
|
is it just x.setTitle()...etc, just the same as if I were assigning something to a normal class object?
|
|
|
|
|
|
#5 |
|
Member (10 bit)
|
ok got that part, just working on printing out all the info.... is there an easy way to do this?
thanks |
|
|
|
|
|
#6 |
|
Professional gadfly
|
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;
}
|
|
|
|
|
|
#7 |
|
Member (10 bit)
|
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);
}
}
thanks |
|
|
|
|
|
#8 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#9 |
|
Member (10 bit)
|
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.... |
|
|
|
|
|
#10 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#11 |
|
Member (10 bit)
|
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. |
|
|
|
|
|
#12 |
|
Professional gadfly
|
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? |
|
|
|
|
|
#13 |
|
Professional gadfly
|
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;
}
|
|
|
|
|
|
#14 |
|
Member (10 bit)
|
nevermind, i got it figured out, but thanks for your help.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|