對不起,這是一個相當大的問題。我不能讓下面的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