古怪的链表行为Java Eclipse Mars.1



完整的代码是一个2-3树的实现,要求我能够通过任何键(ISSN、Author[0]、Author[1]、Author[0]+"、"+Author[1]或Title)搜索一本书。树的工作原理是为每本书创建5个副本,每个副本都用前面提到的一个键指定,并将它们插入各自的叶子中。如果插入的新书与已经插入到树中的密钥匹配(即,James Henry和James Joyce都会将他们的书的副本插入到带有密钥"James"的树中),则该新书不会被添加到树底部的叶节点中,而是被添加到已经包含所有带有密钥=="James"书籍的副本的节点内的链表中。

等级树23:

package tree23;
import bookClasses.*;
/**
 * @author Chris
 * @usage This Class builds a 2-3 tree that sorts Book entries based on ISSN value
 */
 public class Tree23 {
    public Node23 Root;

                // PUBLIC FUNCTIONS //
    public Tree23(){
        Root = new Node23(true);
    }
    public void Insert(int issn, String title, String author){
        String issnKey = null;
        String titleKey = null;
        String authorKey = null;
        String author0Key = null;
        String author1Key = null;
        issnKey = String.valueOf(issn);
        titleKey = title.toLowerCase();
        authorKey = author.toLowerCase();
        if(author.split(",").length == 2){
            author0Key = author.split(",")[0].toLowerCase();
            author1Key = author.split(",")[1].toLowerCase();
        }
        Book[] newBooks = new Book[5];
        for(int i = 0; i < 5; i++){
            newBooks[i] = new Book(issn, title, author);
        }

        InsertByKey(newBooks[0], issnKey);
        InsertByKey(newBooks[1], titleKey);
        InsertByKey(newBooks[2], authorKey);
        if(author.split(",").length == 2){
            InsertByKey(newBooks[3], author0Key);
            InsertByKey(newBooks[4], author1Key);
        }
    }

    public BookList GetBooksByAttribute(char attribute, String key){
        BookList matches = null;
        switch(attribute){
            case 'i':
                matches = GetBooksByISSN(Integer.valueOf(key));
                break;
            case 'n':
                matches = GetBooksByAuthor(key.toLowerCase());
                break;
            case 't':
                matches = GetBooksByTitle(key.toLowerCase());
                break;
            default:
                break;
        }
        return matches;
    }
            // PRIVATE FUNCTIONS //
    /*Insert Functions*/
    private void InsertByKey(Book newBook, String key){
        if(key.compareTo("james") == 0){
            if(newBook.Title.toLowerCase().compareTo("finnegans wake") == 0){
                System.out.print("");
            }
        }
        //check for a node with matching key
        Node23 targetNode = GetNodeByKey(Root, key);
        //else find leaf for insert
        if(targetNode == null){
            targetNode = GetLeafByKey(Root, key);
        }       
        //insert into target node
        targetNode.AddBook(newBook, key);
        if(key.compareTo("james") == 0){
            System.out.print("");
        }
        if(targetNode.GetSize() == 3){
            BalanceNodeOverflow(targetNode);
        }       
    }
    /*Search Functions*/
    private BookList GetBooksByKey(Node23 n, String key){
        BookList bList = null;
        int match = n.GetMatchingBookListByKey(key);
        if(match != -1){
            bList = n.Books[match];
        }
        else{
            int nextChild = DetermineNextTraversal(n, key);
            bList = GetBooksByKey(n.Children[nextChild], key);
        }
        return bList;
    }
    private Node23 GetSuccessorLeaf(Node23 n, int index){
        Node23 leaf = n.Children[index + 1];
        while(!leaf.IsLeaf()){
            leaf = leaf.Children[0];
        }
        return leaf;
    }
    private BookList GetBooksByISSN(int issn){
        BookList keyMatches = GetBooksByKey(Root, String.valueOf(issn));
        BookList issnMatches = null;
        if(keyMatches != null){
            issnMatches = keyMatches.GetBooksByISSN(issn);
        }
        return  issnMatches;
    }
    private BookList GetBooksByAuthor(String author){
        BookList keyMatches = GetBooksByKey(Root, String.valueOf(author));
        BookList authorMatches = null;
        if(keyMatches != null){
            authorMatches = keyMatches.GetBooksByAuthor(author);
        }
        return authorMatches;
    }
    private BookList GetBooksByTitle(String title){
        BookList keyMatches = GetBooksByKey(Root, String.valueOf(title));
        BookList titleMatches = null;
        if(keyMatches != null){
            titleMatches = keyMatches.GetBooksByTitle(title);
        }
        return titleMatches;
    }
    private Node23 GetLeafByKey(Node23 n, String key){
        //assumes that there is no node matching key, traverses from root to leaf node by key
        //return key
        Node23 leaf = n;
        if(!n.IsLeaf()){
            int nextChild = n.GetNextChildByKey(key);
            leaf = GetLeafByKey(n.Children[nextChild], key);
        }
        return leaf;
    }
    private Node23 GetNodeByKey(Node23 n, String key){
        Node23 match = null;
        int nextChild;
        int matchingBookList = n.GetMatchingBookListByKey(key);
        if(matchingBookList >= 0){
            match = n;
        }
        else{
            nextChild = n.GetNextChildByKey(key);
            if(nextChild != -1){
                match = GetNodeByKey(n.Children[nextChild], key);
            }
        }
        return match;
    }
    private int DetermineNextTraversal(Node23 n, String key){
        int nextChild;
        if(n.IsLeaf()){
            nextChild = -1;
        }
        else if(n.GetSize() == 0){
            nextChild = -1;
        }
        else if(n.GetSize() == 1){
            if(n.Books[0].CompareTo(key) > 0){
                nextChild = 0;
            }
            else{
                nextChild = 1;
            }
        }
        else{
            if(n.Books[0].CompareTo(key) > 0){
                nextChild = 0;
            }
            else if(n.Books[1].CompareTo(key) > 0){
                nextChild = 1;
            }
            else{
                nextChild = 2;
            }
        }
        return nextChild;
    }
    /*Delete Functions*/
    private void DeleteBookFromTree(Book b){
        Node23 match;
        int originalSize;
        /*findNodeMatchingISSN*/
        match = GetNodeByKey(Root, String.valueOf(b.ISSN));
        originalSize = match.GetSize();
        match.RemoveMatchingBook(b, String.valueOf(b.ISSN));
        if(originalSize > match.GetSize()){
            BalanceNodeUnderflow(match);
        }
        /*author[0]*/
        match = GetNodeByKey(Root, b.Author[0]);
        originalSize = match.GetSize();
        match.RemoveMatchingBook(b, b.Author[0]);
        if(originalSize > match.GetSize()){
            BalanceNodeUnderflow(match);
        }
        if(b.Author.length == 2){
            /*author[1]*/
            match = GetNodeByKey(Root, b.Author[1]);
            originalSize = match.GetSize();
            match.RemoveMatchingBook(b, b.Author[1]);
            if(originalSize > match.GetSize()){
                BalanceNodeUnderflow(match);
            }
            /*author[0],author[1]*/
            match = GetNodeByKey(Root, new String(b.Author[1] + "," + b.Author[2]));
            originalSize = match.GetSize();
            match.RemoveMatchingBook(b, new String(b.Author[1] + "," + b.Author[2]));
            if(originalSize > match.GetSize()){
                BalanceNodeUnderflow(match);
            }
        }
        /*title*/
        match = GetNodeByKey(Root, b.Title);
        originalSize = match.GetSize();
        match.RemoveMatchingBook(b, b.Author[1]);
        if(originalSize > match.GetSize()){
            BalanceNodeUnderflow(match);
        }
    }

    /*Tree Balancing Functions*/
    private void BalanceNodeOverflow(Node23 fullNode){  
        Node23[] splitNodes;
        if(fullNode == Root){
            Node23 newRoot = new Node23(false);
            fullNode.Parent = newRoot;
            splitNodes = fullNode.SplitNode();
            fullNode.Parent.AddBookList(fullNode.RemoveBookList(0));;
            newRoot.AddSplitNodeChildAt(0, splitNodes);
            Root = newRoot;
        }
        else{
            int position = fullNode.WhichChildAmI();
            splitNodes = fullNode.SplitNode();
            fullNode.Parent.AddBookList(fullNode.RemoveBookList(0));
            fullNode.Parent.AddSplitNodeChildAt(position, splitNodes);
            if(fullNode.Parent.GetSize() == 3){
                BalanceNodeOverflow(fullNode.Parent);
            }
        }
    }
    private void RotateLeft1To0(Node23 parent){
        Node23[] children = parent.Children;
        children[0].AddBookList(parent.RemoveBookList(0));
        parent.AddBookList(children[1].RemoveBookList(0));
        children[1].ShiftBooksLeftInto(0);
    }
    private void RotateLeft2To1(Node23 parent){
        Node23[] children = parent.Children;
        children[1].AddBookList(parent.RemoveBookList(1));
        parent.AddBookList(children[2].RemoveBookList(0));
        children[2].ShiftBooksLeftInto(0);
    }
    private void RotateRight0To1(Node23 parent){
        Node23[] children = parent.Children;
        children[1].AddBookList(parent.RemoveLeftMostBookList());
        parent.AddBookList(children[0].RemoveRightMostBookList());
    }
    private void RotateRight1To2(Node23 parent){
        Node23[] children = parent.Children;
        children[2].AddBookList(parent.RemoveRightMostBookList());
        parent.AddBookList(children[1].RemoveRightMostBookList());
    }
    private void BalanceNodeUnderflow(Node23 n){
        Node23 leaf;
        int emptyPosition = n.FirstNullBookList();
        if(!n.IsLeaf()){
            leaf = GetSuccessorLeaf(n, emptyPosition);
            n.AddBookList(leaf.RemoveBookList(0));
        }
        else{
            leaf = n;
        }
        if(leaf.GetSize() == 0){
            FillHole(leaf);
        }
    }
    private void RedistributeParent3(Node23 hole, int holePosition){
        Node23 parent = hole.Parent;
        Node23[] siblings = parent.Children;
        switch(holePosition){
            case 0:
                if(siblings[1].GetSize() == 2){
                    RotateLeft1To0(parent);
                    if(!hole.IsLeaf()){
                        hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
                        siblings[1].ShiftChildrenLeftInto(0);
                    }
                }
                else if(siblings[2].GetSize() == 2){
                    RotateLeft1To0(parent);
                    RotateLeft2To1(parent);
                    if(!hole.IsLeaf()){
                        hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
                        siblings[1].ShiftChildrenLeftInto(0);
                        siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
                        siblings[2].ShiftChildrenLeftInto(0);
                    }
                }
                else{
                    RotateLeft1To0(parent);
                    hole.AddBookList(parent.RemoveBookList(0));
                    parent.ShiftBooksLeftInto(0);
                    siblings[1].AddBookList(siblings[2].RemoveBookList(0));
                    if(!hole.IsLeaf()){
                        hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
                        siblings[1].ShiftChildrenLeftInto(0);
                        hole.AddChildAt(2, siblings[1].RemoveLeftMostChild());
                        siblings[1].AddChildAt(0, siblings[2].RemoveLeftMostChild());
                        siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
                    }
                    parent.RemoveRightMostChild();
                }
                break;
            case 1:
                if(siblings[0].GetSize() == 2){
                    RotateRight0To1(parent);
                    if(!hole.IsLeaf()){
                        siblings[1].ShiftChildrenRightInto(1);
                        siblings[1].AddChildAt(0, siblings[0].RemoveRightMostChild());
                    }
                }
                else if(siblings[2].GetSize() == 2){
                    RotateLeft2To1(parent);
                    if(!hole.IsLeaf()){
                        siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
                        siblings[2].ShiftBooksLeftInto(0);
                    }
                }
                else{
                    RotateLeft2To1(parent);
                    siblings[1].ShiftBooksLeftInto(0);
                    RotateLeft1To0(parent);
                    parent.ShiftBooksLeftInto(0);
                    if(!hole.IsLeaf()){
                        siblings[0].AddChildAt(2, siblings[1].RemoveLeftMostChild());
                        siblings[1].AddChildAt(0, siblings[2].RemoveLeftMostChild());
                        siblings[1].AddChildAt(1, siblings[2].RemoveRightMostChild());
                    }
                    parent.RemoveRightMostChild();
                }
                break;
            case 2:
                if(siblings[0].GetSize() == 2){
                    RotateRight1To2(parent);
                    RotateRight0To1(parent);
                    if(!hole.IsLeaf()){
                        siblings[2].ShiftChildrenRightInto(1);
                        siblings[2].AddChildAt(0, siblings[1].RemoveRightMostChild());
                        siblings[1].ShiftBooksRightInto(1);
                        siblings[1].AddChildAt(0, siblings[0].RemoveRightMostChild());
                    }
                }
                else if(siblings[1].GetSize() == 2){
                    RotateRight1To2(parent);
                    if(!hole.IsLeaf()){
                        siblings[2].ShiftChildrenRightInto(1);
                        siblings[2].AddChildAt(0, siblings[1].RemoveRightMostChild());
                    }
                }
                else{
                    RotateLeft1To0(parent);
                    siblings[1].AddBookList(parent.RemoveRightMostBookList());
                    if(!hole.IsLeaf()){
                        siblings[0].AddChildAt(2, siblings[1].RemoveLeftMostChild());
                        siblings[1].ShiftChildrenLeftInto(0);
                        siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
                    }
                    parent.RemoveRightMostChild();
                }
                break;
            default:
                break;
        }
    }
    private void RedistributeParent2(Node23 hole, int holePosition){
        Node23 parent = hole.Parent;
        Node23[] siblings = parent.Children;
        switch(holePosition){
            case 0:
                hole.AddBookList(parent.RemoveBookList(0));
                parent.AddBookList(siblings[1].RemoveBookList(0));
                siblings[1].ShiftBooksLeftInto(0);
                break;
            case 1:
                hole.AddBookList(parent.RemoveBookList(0));
                parent.AddBookList(siblings[0].RemoveBookList(1));
                break;
            default:
                break;
        }
    }
    private void Merge(Node23 hole, int holePosition){
        Node23 parent = hole.Parent;
        Node23[] sibling = parent.Children;
        if(holePosition == 0){
            sibling[0].AddBookList(sibling[1].RemoveBookList(0));
        }
        sibling[0].AddBookList(parent.RemoveBookList(0));
        if(!hole.IsLeaf()){
            sibling[0].AddChildAt(1, sibling[1].RemoveLeftMostChild());
            sibling[0].AddChildAt(2, sibling[1].RemoveRightMostChild());
        }
        parent.RemoveRightMostChild();
        if(parent.GetSize() == 0){
            BalanceNodeUnderflow(parent);
        }
    }
    private void FillHole(Node23 hole){
        Node23[] siblings;
        Node23 parent;
        int holePosition;
        if(hole == Root){
            Root = hole.Children[0];
        }else{
            parent = hole.Parent;
            siblings = parent.Children;
            holePosition = hole.WhichChildAmI();
            if(parent.GetSize() == 2){
                RedistributeParent3(hole, holePosition);
            }
            else{
                if(siblings[0].GetSize() == 2 || siblings[1].GetSize() == 2){
                    RedistributeParent2(hole, holePosition);
                }else{
                    Merge(hole, holePosition);
                }
            }
        }
    }
}

包装树23;

导入bookClasses.*;

/***@节点*此节点是为2-3树设计的。*该节点包含两个Book对象,并指向同一类型的三个子节点。*该节点还被设计为容纳溢出的Book和溢出的子节点,直到该节点被拆分。*/

public class Node23 {
    public BookList[] Books;
    private boolean isLeaf;
    private int size;
    public Node23 Parent;
    public Node23[] Children;
    //Constructors
    public Node23(boolean isleaf){
        Books = new BookList[3];
        Children = new Node23[4];
        isLeaf = isleaf;
        size = 0;
    }
    public Node23[] SplitNode(){
        Node23[] splitNodes = new Node23[2];
        splitNodes[0] = new Node23(isLeaf);
        splitNodes[1] = new Node23(isLeaf);
        splitNodes[0].Parent = Parent;
        splitNodes[1].Parent = Parent;
        splitNodes[0].AddBookList(RemoveLeftMostBookList());
        splitNodes[1].AddBookList(RemoveRightMostBookList());
        if(!isLeaf){    
            Children[0].Parent = splitNodes[0];
            Children[1].Parent = splitNodes[0];
            splitNodes[0].Children[0] = Children[0];
            splitNodes[0].Children[1] = Children[1];
            Children[2].Parent = splitNodes[1];
            Children[3].Parent = splitNodes[1];
            splitNodes[1].Children[0] = Children[2];
            splitNodes[1].Children[1] = Children[3];
        }
        return splitNodes;
    }

    //Book Functions
    public int FirstNullBookList(){
        int emptyPos = -1;
        for(int i = 0; i < 2; i++){
            if(Books[i] == null){
                emptyPos = -1;
            }
        }
        return emptyPos;
    }
    public int GetNextChildByKey(String key){       
        int nextChild = -1;
        if(!isLeaf){
            if(size == 1){
                if(Books[0].CompareTo(key) > 0){
                    nextChild = 0;
                }
                else{
                    nextChild = 1;
                }
            }
            else{
                if(Books[0].CompareTo(key) > 0){
                    nextChild = 0;
                }
                else if(Books[1].CompareTo(key) > 0){
                    nextChild = 1;
                }
                else{
                    nextChild = 2;
                }
            }
        }
        return nextChild;
    }
    public int GetMatchingBookListByKey(String key){
        int match = -1;
        for(int i = 0; i < size; i++){
            if(Books[i].CompareTo(key) == 0)
                match = i;
        }
        return match;
    }
    public void TakeAllBookLists(Node23 n){
        for(int i = 0; i < 3; i++){
            if(n.Books[i] != null){
                AddBookList(n.RemoveBookList(i));
            }
        }
    }
    public void ShiftBooksRightFrom(int shiftStart){
        for(int i = 2; i > shiftStart; i--){
            Books[i] = Books[i - 1];
        }
        Books[shiftStart] = null;
    }
    public void ShiftBooksRightInto(int shiftEnd){
        for(int i = shiftEnd; i > 0; i--){
            Books[i] = Books[i - 1];
        }
        Books[0] = null;
    }
    public void ShiftBooksLeftFrom(int shiftStart){
        for(int i = 0; i < shiftStart; i++){
            Books[i] = Books[i+1];
        }
        Books[shiftStart] = null;
    }
    public void ShiftBooksLeftInto(int shiftEnd){
        for(int i = shiftEnd; i < 2; i++){
            Books[i] = Books[i + 1];
        }
        Books[2] = null;
    }
    public void AddBookList(BookList newBookList){
        if(size == 0){
            Books[0] = newBookList;
        }
        else{
            if(newBookList.CompareTo(Books[0]) < 0){
                    ShiftBooksRightFrom(0);
                    Books[0] = newBookList;
            }
            else{
                if(size == 1){
                    Books[1] = newBookList;
                }
                else{
                    if(newBookList.CompareTo(Books[1]) < 0){
                        ShiftBooksRightFrom(1);
                        Books[1] = newBookList;
                    }
                    else{
                        Books[2] = newBookList;
                    }
                }
            }
        }
        size++;
    }
    public void AddBook(Book newBook, String key){
        if(size == 0){
            AddBookList(new BookList(newBook, key));
        }
        else{
            int match = GetMatchingBookListByKey(key);
            if(match != -1){
                Books[match].AddBook(newBook);
            }
            else{
                AddBookList(new BookList(newBook, key));
            }
        }       
    }
    public BookList RemoveRightMostBookList(){
        BookList bList = null;
        for(int i = 2; i > 0; i--){
            if(Books[i] != null){
                bList = Books[i];
                Books[i] = null;
                size--;
                break;
            }
        }
        return bList;   
    }
    public BookList RemoveLeftMostBookList(){
        BookList bList = null;
        bList = Books[0];
        ShiftBooksLeftInto(0);
        size--;
        return bList;
    }
    public BookList RemoveBookList(int position){
        BookList bList = Books[position];
        ShiftBooksLeftInto(position);
        size--;
        return bList;
    }
    public void RemoveMatchingBook(Book b, String key){
        if(Books[0] != null){
            if(Books[0].CompareTo(key) == 0){
                Books[0].DeleteBooksByISSN(b.ISSN);
            }
        }
        else if(Books[1] != null){
            if(Books[1].CompareTo(key) == 0){
                Books[1].DeleteBooksByISSN(b.ISSN);
            }
        }
        else if(Books[2] != null){
            if(Books[2].CompareTo(key) == 0){
                Books[2].DeleteBooksByISSN(b.ISSN);
            }
        }
    }

    //Child-Node Functions
    public Node23 RemoveLeftMostChild(){
        Node23 n = null;
        int i = 0;
        while(n == null && i <= 3){
            if(Children[i] != null){
                n = Children[i];
                Children[i] = null;
            }
            i++;
        }
        return n;
    }
    public Node23 RemoveRightMostChild(){
        Node23 n = null;
        int i = 3;
        while(n == null && i >= 0 ){
            if(Children[i] != null){
                n = Children[i];
                Children[i] = null;
            }
            i--;
        }
        return n;
    }
    public void AddChildAt(int index, Node23 newChildNode){
        Children[index] = newChildNode;
    }
    public void AddSplitNodeChildAt(int position, Node23[] splitNode){
        ShiftChildrenRightFrom(position + 1);
        AddChildAt(position, splitNode[0]);
        AddChildAt(position + 1, splitNode[1]);
    }

    public void ShiftChildrenRightFrom(int shiftStart){
        for(int i = 3; i > shiftStart; i--){
            Children[i] = Children[i - 1];
        }
        Children[shiftStart] = null;
    }
    public void ShiftChildrenRightInto(int shiftEnd){
        for(int i = shiftEnd; i > 0; i--){
            Children[i] = Children[i - 1];
        }
        Children[0] = null;
    }
    public void ShiftChildrenLeftFrom(int shiftStart){
        for(int i = 0; i < shiftStart; i++){
            Children[i] = Children[i + 1];
        }
        Children[shiftStart] = null;
    }
    public void ShiftChildrenLeftInto(int shiftEnd){
        for(int i = shiftEnd; i < 2; i++){
            Children[i] = Children[i + 1];
        }
        Children[2] = null;
    }

    //Attribute Functions
    public int CountChildren(){
        int count = 0;
        for(int i = 0; i < 3; i++){
            if(Children[i] != null){
                count++;
            }
        }
        return count;
    }
    public boolean IsRoot(){
        boolean isRoot = false;
        if(Parent == null){
            isRoot = true;
        }
        return isRoot;
    }

    public int WhichChildAmI(){
        int childNum = -1;
        if(!IsRoot()){
            for(int i = 0; i < Parent.size + 1; i++){
                if(Parent.Children[i] == this){
                    childNum = i;
                }
            }
        }
        return childNum;
    }
    public boolean IsLeaf(){
        return isLeaf;
    }
    public int GetSize(){
        return size;
    }
}

Class Book:这是Book类,用于在每个Node23 中创建链接列表的链接

package bookClasses;
public class Book{
    public int ISSN;
    public String Title;
    public String[] Author;
    public Book Next;
    public Book(){}
    /**
     * @param ISSN
     * @param Title
     * @param Author
     */
    public Book(int issn, String title, String author){
        ISSN = issn;
        Title = title;
        Author = author.split(",");
        Next = null;
    }

    private String AuthorToString(){
        String author = Author[0];
        if(Author.length == 2){
            author = author + ", " + Author[1];
        }
        return author;
    }
    public void Display(){
        System.out.println("tISSN: " + ISSN);
        System.out.println("tTITLE: " + Title);
        System.out.println("tAUTHOR: " + AuthorToString());
    }
    public boolean MatchAuthor(String author){
        boolean match = false;
        if(Author.length == 2){
            if(author.compareTo(Author[0].toLowerCase()) == 0 
                    || author.compareTo(Author[1].toLowerCase()) == 0){
                match = true;
            }
        }
        if(author.split(",") == Author){
            match = true;
        }
        return match;
    }
}

Class BookList:这是每个节点内的链表,包含带有匹配密钥的所有书籍的副本

package bookClasses;
public class BookList {
    public String Key;
    public Book Head;
    private int Size;
    public BookList(){
        Head = null;
        Size = 0;
    };
    public BookList(Book b, String key){
        Size = 1;
        Key = key;
        Head = b;
    }
    //Insert Functions
    public void AddBook(Book newBook){  
        newBook.Next = Head;
        Head = newBook;
        Size++;
    }

    //Search Functions
    public BookList GetBooksByISSN(int issn){
        BookList books = new BookList();
        Book b = Head;
        for(int i = 0; i < Size; i++){
            if(issn == b.ISSN){
                books.AddBook(b);
            }
        }
        return books;
    }
    public BookList GetBooksByTitle(String title){
        BookList matches = new BookList();
        Book b = Head;
        for(int i = 0; i < Size; i++){
            if(title.compareTo(b.Title.toLowerCase()) == 0){
                matches.AddBook(b);
            }
        }
        return matches;
    }
    public BookList GetBooksByAuthor(String author){
        BookList books = new BookList();
        Book b = Head;
        for(int i = 0; i < Size; i++){
            if(b.MatchAuthor(author)){
                books.AddBook(b);
            }
            b = b.Next;
        }
        return books;
    }
    //Delete Functions
    public void DeleteBooksByAuthor(String author){
        Book b = Head;
        if(b.MatchAuthor(author)){
            Head = b.Next;
            Size--;
        }
        else{
            while(b.Next != null){
                if(b.Next.MatchAuthor(author)){
                    b.Next = b.Next.Next;
                    Size--;
                }
                else{
                    b = b.Next;
                }
            }   
        }   
    }
    public void DeleteBooksByTitle(String title){
        Book b = Head;
        if(b.Title.toLowerCase().compareTo(title.toLowerCase()) == 0){
            Head = b.Next;
            Size--;
        }
        else{
            while(b.Next != null){
                if(b.Next.Title.toLowerCase().compareTo(title.toLowerCase()) == 0){
                    b.Next = b.Next.Next;
                    Size--;
                }
                else{
                    b = b.Next;
                }
            }   
        }   
    }
    public void DeleteBooksByISSN(int issn){
        Book b = Head;
        if(b.ISSN == issn){
            Head = b.Next;
            Size--;
        }
        else{
            while(b.Next != null){
                if(b.Next.ISSN == issn){
                    b.Next = b.Next.Next;
                    Size--;
                }
                else{
                    b = b.Next;
                }
            }   
        }   
    }

    //Other Methods
    public void Displayall(){
        Book b = Head;
        for(int i = 0; i < Size; i++){
            b.Display();
            b = b.Next;
        }
    }
    public int CompareTo(BookList bookList){
        return (Key.compareTo(bookList.Key));
    }
    public int CompareTo(String key){
        return (Key.compareTo(key));
    }
    public int Size(){
        return Size;
    }
}

这是输入文件-1指定INSERT

1
60
The Black Dahlia
James,Ellroy
1
98
The Turn of the Screw
Henry,James
1
147
The Wings of the Dove
Henry,James
1
151
A Portrait of the Artist as a Young Man
James,Joyce
1
157
The Postman Always Rings Twice
James,M.
1
184
How Late It Was, How Late
James,Kelman
1
186
A Disaffection
James,Kelman
1
320
The Ambassadors
Henry,James
1
326
What Maisie Knew
Henry,James
1
327
The Golden Bowl The Golden Bowl
Henry,James
1
353
The Charwoman's Daughter
James,Stephens
1
362
Ulysses
James,Joyce
1
478
Finnegans Wake
James,Joyce

插入ulysses 后

The Black Dahlia
The Turn of the Screw
The Wings of the Dove
A Portrait of the Artist as a Young Man
The Postman Always Rings Twice
How Late It Was, How Late
A Disaffection
The Ambassadors
What Maisie Knew
The Golden Bowl The Golden Bowl
The Charwoman's Daughter
Ulysses <==Head

下面是它变得古怪的地方:如果我尝试插入这个条目:

478
Finnegans Wake
James,Joyce

使列表的大小从12变为13,列表的新顺序变为:

    The Black Dahlia
    The Turn of the Screw
    The Wings of the Dove
    A Portrait of the Artist as a Young Man
    The Postman Always Rings Twice
    How Late It Was, How Late
    A Disaffection
    The Ambassadors
    What Maisie Knew
    The Golden Bowl The Golden Bowl
    Ulysses 
    Finnegans Wake <=======HEAD

Charwoman的女儿已经失踪了。当Head=newBook;

所以,我认为这可能与我的硬件有关,而我最初存储这个项目的计算机使用AMD处理器,所以我上了我的英特尔笔记本电脑,通过网络访问AMD桌面。我将项目文件复制到笔记本电脑上的工作空间中——只需简单的拖放即可。首先,我在英特尔笔记本电脑上运行代码,不出所料,它不起作用,然后我在桌面上再次运行代码,现在它起作用了。未进行任何更改。代码在桌面上运行后,突然在笔记本电脑上运行。

相关内容

  • 没有找到相关文章

最新更新