linked lists problem...

Hi.

I am trying to create an alphabetical list from given book titles using strcmp and I can't figure out how to call the compareTo function I've created, or what to use as the parameter. I know it is supposed to be a Book...but I'm lost.

Most of this program was given - my assignment is to create a compareTo function in Book, insert and delet functions in Booklist and use lib.cpp to test.

i am stuck on compareTo - I can't figure out how to pass the next Book title to compare with current title.

//lib.cpp

#include <iostream>
using namespace std;
#include "BookList.h"

int main(int argc, char* argv[]) {
//--------------------------------------------------------------
// Creates a BookList object, adds several books to the list,
// then prints them.
//--------------------------------------------------------------

//Initializes new list
char list[2048];

//Creates empty list - books points to new Booklist object.
BookList *books = new BookList();

books->insert (new Book("F Title"));
books->insert (new Book("D Title"));
books->insert (new Book("G Title"));
books->insert (new Book("A Title"));
books->insert (new Book("E Title"));
books->insert (new Book("H Title"));

cout << "After inserts:\n";
cout << books->getBookList(list) << endl;;

books->delet (new Book("A Title"));
books->delet (new Book("H Title"));
books->delet (new Book("G Title"));
books->delet (new Book("E Title"));

cout << "After deletes:\n";
cout << books->getBookList(list) << endl;;

books->insert (new Book("A Title"));
books->insert (new Book("E Title"));
books->insert (new Book("H Title"));
books->insert (new Book("G Title"));

cout << "After 2nd inserts:\n";
cout << books->getBookList(list) << endl;

return 0;
}

#include <cstring>


//********************************************************************
// Book.h
//
// Represents a single book.
//*******************************************************************

class Book
{

public:
Book () {};
Book (char *newTitle)
{
strcpy( title, newTitle );
}

int compareTo(const char *nextTitle)
{
return strcmp(title, nextTitle);
}

char *getBook()
{
return title;
}
private:
char title[81];
};

//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "Book.h"

class BookNode {
public:
//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
BookNode() { };
BookNode(Book *theBook) {
book = theBook;
next = NULL;
};
friend class BookList;

private:
Book *book;
BookNode *next;
};

class BookList {

//----------------------------------------------------------------
// Sets up an initially empty list of books.
//----------------------------------------------------------------
public:
//void add(Book *);
char* getBookList(char *);
void insert(Book *);
void delet(Book *);

BookList() {
head = NULL;
};

private:
BookNode *head;

};
//********************************************************************
// BookList.cpp
//
// Represents a collection of books.
//*******************************************************************
#include "BookList.h"

//----------------------------------------------------------------
// Inserts a new Book object into linked list alphabetically.
// -book gets made (head is set to null)
// -node is new book title
// -current is declared
// -
//----------------------------------------------------------------
void BookList::insert(Book *newBook)
{
BookNode *node = new BookNode(newBook);
BookNode *current;
Book compCall;

if (head == NULL)
head = node;
else
{
current = head;

if (compCall.compareTo(current->book->getBook()) == -1) //current book < next book
current->next = node;
else
{
while (compCall.compareTo(current->book->getBook()) == 1) //current book > next book
{
current = current->next;
}
current->next = node;
}
}
}

//----------------------------------------------------------------
// Returns this list of books as a string.
//----------------------------------------------------------------
char *BookList::getBookList(char *list) {

list[0] = '\0';
BookNode *current = head;

while (current != NULL) {
strcat( list, current->book->getBook() );
strcat( list, "\n" );
current = current->next;
}

return list;
}
//----------------------------------------------------------------
// Deletes specified books from list.
//----------------------------------------------------------------
void BookList::delet(Book *newBook)
{
BookNode *node = new BookNode(newBook);
BookNode *current = NULL;
BookNode *previous;


/*
* Visit each node, maintaining a pointer to
* the previous node we just visited.
*/
for (current = head; current != NULL; previous = current, current = current->next)

{
if (current == node) { /* Found it. */
if (head == NULL)
{
/* Fix beginning pointer. */
node = current->next;
}
else
{
/*
* Fix previous node's next to
* skip over the removed node.
*/

previous->next=current->next;
}

delete current; /* Deallocate the node. */
}
}

}
getBook() returns a pointer to the string that you would pass to compareTo().
omg, thankyou!

I can now rejoin the living.

I compile. Logic is wrong, but I think I can figure it out from here.
Topic archived. No new replies allowed.