class MultiLinkedListOfBooks
{
Book firstAuthor = null;
private class Book
{
private string author, title;
private double price;
public Book next;
public char authorchar, titlechar;
public Book(string author1, string title1, double price1)
{
price = price1;
title = title1;
author = author1;
next = null;
string[] authorsplit = author.Split(null);
authorchar = (authorsplit[1].Substring(0, 1).ToLower())[0];
titlechar = (title.Substring(0, 1).ToLower())[0];
Console.WriteLine(authorchar + " " + titlechar);
}
// This compares 'this' book to the other book's author and title.
// This version FIRST compares by author, and if they're the same THEN compares by title
//done
public int CompareAuthorTHENTitle(string otherBooksAuthor, string otherBooksTitle)
{
if (author.Equals(otherBooksAuthor))
{
if (title.Equals(otherBooksTitle))
{
return 1;
}
}
return 0;
}
// This compares 'this' book to the other book's author and title.
// This version FIRST compares by title, and if they're the same THEN compares by author
public int CompareTitleTHENAuthor(string otherBooksAuthor, string otherBooksTitle)
{ // You may (or may not) need this method
return 0;
}
//done
// Print out the book info (author, title, price).
public void Print()
{
Console.WriteLine("Author: " + author);
Console.WriteLine("Title: " + title);
Console.WriteLine("Price: " + price);
}
}
//done
public ErrorCode Add(string author, string title, double price)
{
bool end = false;
Book temp = firstAuthor;
if (temp == null)
{
firstAuthor = new Book(author, title, price);
return ErrorCode.OK;
}
if (temp.next == null) end = true;
while (end == false)
{
if (temp.CompareAuthorTHENTitle(author, title) == 1)
{
return ErrorCode.DuplicateBook;
}
temp = temp.next;
}
temp.next = new Book(author, title, price);
firstAuthor = sort(firstAuthor);
return ErrorCode.OK;
// having multiple books with the same author, but different titles, or
// multiple books with the same title, but different authors, is fine.
// two books with the same author & title should be identified as duplicates,
// even if the prices are different.
}
public void PrintByAuthor()
{
// if there are no books, then print out a message saying that the list is empty
if (firstAuthor == null) Console.WriteLine("There are no books!");
else
{
Book temp = firstAuthor;
bool end = false;
while (end == false)
{
temp.Print();
Console.WriteLine();
if (temp.next == null) end = true;
}
}
}
//done
public ErrorCode Remove(string author, string title)
{
// if there isn't an exact match, then do the following:
if (firstAuthor == null) return ErrorCode.BookNotFound;
Book temp = firstAuthor;
Book before = null;
bool end = false;
while (end == false)
{
if (temp.CompareAuthorTHENTitle(author, title) == 1)
{
if (before == null)
{
firstAuthor = temp.next;
}
else
{
before.next = temp.next;
}
}
before = temp;
temp = temp.next;
if (temp.next == null) return ErrorCode.BookNotFound;
}
return ErrorCode.BookNotFound;
// (this includes finding a book by the given author, but with a different title,
// or a book with the given title, but a different author)
}
private Book sort(Book node)
{
if (firstAuthor == null && firstAuthor.next == null) return node;
Book merge(Book a, Book b)
{
Book result;
if (a == null) return b;
if (b == null) return a;
if (a.authorchar == b.authorchar)
{
if (a.titlechar <= b.titlechar && a.authorchar == b.authorchar)
{
result = a;
result.next = merge(a.next, b);
}
else
{
result = b;
result.next = merge(a, b.next);
}
}
else if (a.authorchar < b.authorchar)
{
result = a;
result.next = merge(a.next, b);
}
else
{
result = b;
result.next = merge(a, b.next);
}
return result;
}
Book middle(Book top)
{
if (top == null) return top;
Book slow = top;
Book fast = top;
while (fast.next != null && fast.next.next != null)
{
slow = slow.next;
fast = fast.next.next;
fast = slow.next;
}
return slow;
}
Book mid = middle(node);
Book l = node;
Book r = mid.next;
mid.next = null;
return merge(sort(l), sort(r));
}
}
Above is my code. I get the exception on the "Book mid = middle(node);" line. To give some context the book class has a few fields, the two important ones being author and title. Merge checks author then title to determine how it merges to sort alphabetically first by author then by name. This is all to sort the Book objects alphabetically by author first and then name in the LinkedList. As the title says, I am getting the stackoverflow exception and cannot figure out a way to avoid necessarily.
EDIT: Added code context. Doing this for a cs project where the requirements of the node are:
Has fields for author, title, price
Has print method
Has constructor