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"));
//********************************************************************
// 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);
}
//********************************************************************
// 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;
}
/*
* 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.
*/