排序功能无法将列表中的项目移动足够远

时间:2019-11-28 09:35:19

标签: c# sorting linked-list computer-science

class MultiLinkedListOfBooks
    {
        Book firstAuthor = null;

        private class Book
        {
            public string author, title;
            public double price;
            public Book next;
            public Book(string author1, string title1, double price1)
            {
                price = price1;
                title = title1;
                author = author1;
                next = null;
            }
            public Book(string author1, string title1, double price1, Book pointer)
            {
                price = price1;
                title = title1;
                author = author1;
                next = pointer;
            }
            // 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;
            }
            while (end == false)
            {
                if (temp.author.Equals(author) && temp.title.Equals(title))
                {
                    return ErrorCode.DuplicateBook;
                }
                if (temp.next == null) break;
                temp = temp.next;
            }
            temp.next = new Book(author, title, price);
            firstAuthor = godknowswhatsort(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();
                    temp = temp.next;
                    if (temp == null) return;
                }
            }
        }
        //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.author.Equals(author) && temp.title.Equals(title))
                {
                    if (before == null)
                    {
                        firstAuthor = null;
                        return ErrorCode.OK;
                    }
                    else
                    {
                        before.next = temp.next;
                        return ErrorCode.OK;
                    }
                }
                else
                {
                    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 godknowswhatsort(Book input)
        {
            bool counter = false;
            while(counter == false)
            {
                counter = true;
                if (input == null) return input;
                if (input.next == null) return input;
                if (String.Compare(input.author, input.next.author) > 0)
                {
                    input = new Book(input.next.author, input.next.title, input.next.price, godknowswhatsort(new Book(input.author, input.title, input.price, input.next.next)));
                    counter = false;
                }
                else if (input.author.Equals(input.next.author))
                {
                    if (String.Compare(input.title, input.next.title) > 0)
                    {
                        input = new Book(input.next.author, input.next.title, input.next.price, godknowswhatsort(new Book(input.author, input.title, input.price, input.next.next)));
                        counter = false;
                    }
                    else input = new Book(input.author, input.title, input.price, godknowswhatsort(new Book(input.next.author, input.next.title, input.next.price, input.next.next)));
                }
                else input = new Book(input.author, input.title, input.price, godknowswhatsort(new Book(input.next.author, input.next.title, input.next.price, input.next.next)));
            }
            return input;
        }
    }

再次提出另一个问题。正在处理一个CS项目,我的排序功能有问题。我正在排序一个链表。如果我可以说3个项目,而这些项目是

{作者:a a,标题:a},

{作者:a,标题:d},

{作者:d d,图块c}

然后我添加

{作者:a,标题:c},

我会得到

{作者:a,标题:a},

{作者:a,标题:d},

{作者:a,标题:c},

{作者:d d,标题:c}。

我的猜测是,它不会多次执行排序循环,应该,但似乎没有。我为while循环设置了两个,似乎无法找出为什么它不会重复多次。

0 个答案:

没有答案