对不起,这是一个相当大的问题。我不能让下面的C++的工作,我总是得到关于从visiual工作室的代码没有一个struct /类/联合错误。我应该按照字母顺序将书放入链接列表中,但到目前为止,我的插入方法已损坏。链表的插入,删除,排序
//********************************************************************
// BookList.cpp
//
// Represents a collection of books.
//*******************************************************************
#include "BookList.h"
//----------------------------------------------------------------
// Creates a new Book object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
void BookList::add(Book *newBook) {
BookNode *node = new BookNode(newBook);
BookNode *current;
if (head == NULL)
head = node;
else {
current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
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;
}
void BookList::insert(Book *newBook) {
BookNode *node = new BookNode(newBook);
BookNode *current;
if (head == NULL) {
head = node;
}
else {
current = head;
int result = *newBook.compareTo(current->book->getBook());
if (result == -1) {
current->next = node;
}
else {
while (result == 1) {
current = current->next;
result = *newBook.compareTo(current->book->getBook());
}
current->next = node;
}
}
}
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "Book.h"
class BookNode {
public:
BookNode() { };
BookNode(Book *theBook) {
book = theBook;
next = NULL;
};
friend class BookList;
private:
Book *book;
BookNode *next;
};
class BookList {
public:
void add(Book *);
char* getBookList(char *);
void delet(Book *);
void insert(Book *);
BookList() {
head = NULL;
};
private:
BookNode *head;
};
#include <cstring>
//********************************************************************
// Book.h
//
// Represents a single book.
//*******************************************************************
class Book {
public:
Book (char *newTitle) {
strcpy(title, newTitle);
}
int compareTo(Book *newBook) {
int compvar;
compvar = strcmp(newBook->getBook(), title);
return compvar;
}
char *getBook() {
return title;
}
private:
char title[81];
};
当然,还有许多问题与此代码,所以任何帮助任何人都可以提供将是真棒。提前致谢!
它是否给一个行数看?如果是这样,你能否在来源中指出有问题的路线?这会有很大的帮助。 – Anthony 2012-02-20 22:15:35
你想要[代码评论](http://codereview.stackexchange.com),还是有一个你可以描述的具体问题? – Useless 2012-02-20 22:15:51
如果不调试它,你不应该让它变大。 *永远不要添加到不起作用的代码。* – Beta 2012-02-20 22:16:08