简体   繁体   English

LinkedList 头尾问题

[英]LinkedList head and tail issues

I'm trying to figure out why some of the loops inside of InventoryList.java don't work, such as all the remove methods.我试图弄清楚为什么 InventoryList.java 中的某些循环不起作用,例如所有删除方法。 By don't work, I mean that the loop I implemented doesn't work and I'm looking for solutions on how to get it fixed.不工作,我的意思是我实现的循环不起作用,我正在寻找如何修复它的解决方案。 Also another issue that isn't too important is the toString() in InventoryList.java isn't set up properly, but i'd rather know why my loops aren't working.另一个不太重要的问题是 InventoryList.java 中的 toString() 设置不正确,但我想知道为什么我的循环不起作用。 For more information, here are the documentations I'm using to implement these classes: http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2020/12/p7/doc/package-summary.html有关更多信息,这里是我用来实现这些类的文档: http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2020/12/p7/doc/package-summary .html

Thank you for reading感谢您阅读

/**
 * This class models a Box used for inventory
 * 
 * @author mouna
 *
 */
public class Box {
  private static int nextInventoryNumber = 1; // generator of unique inventory numbers
  private Color color; // color of this box
  private final int INVENTORY_NUMBER; // unique inventory number of this box

  /**
   * Creates a new Box and initializes its instance fields color and unique inventory number
   * 
   * @param color color to be assigned of this box. It can be any of the constant color values
   *              defined in the enum Color: Color.BLUE, Color.BROWN, or Color.YELLOW
   */
  public Box(Color color) {
    this.color = color;
    this.INVENTORY_NUMBER = nextInventoryNumber++;
  }

  /**
   * Returns the color of this box
   * 
   * @return the color of this box
   */
  public Color getColor() {
    return color;
  }

  /**
   * returns the inventory number of this box
   * 
   * @return the unique inventory number of this box
   */
  public int getInventoryNumber() {
    return this.INVENTORY_NUMBER;
  }


  /**
   * Returns a String representation of this box in the format "color INVENTORY_NUMBER"
   *
   * @return a String representation of this box
   */
  @Override
  public String toString() {
    return this.color + " " + this.INVENTORY_NUMBER;
  }

  /**
   * This method sets the nextInventoryNumber to 1. This method must be used in your tester methods
   * only.
   */
  public static void restartNextInventoryNumber() {
    nextInventoryNumber = 1;
  }
}
/**
 * This Enumeration groups the name of constants representing the three colors BLUE, BROWN, and YELLOW
 *
 */
public enum Color{
  BLUE, BROWN, YELLOW;
}
public class LinkedBox {
    private Box box;
    private LinkedBox next;
    
  //constructors
    public LinkedBox(Box box) {
    this.box = box;
    }
    
    public LinkedBox(Box box, LinkedBox next) {
    this.box = box;
    this.next = next;
    }
    
    //getters
    public Box getBox() {
        return box;
    }
    
    public LinkedBox getNext() {
        return next;
    }
    
    //setters
    public void setNext(LinkedBox next) {
        this.next = next;
    }
    
    public String toString() {
        if (next == null) return box.toString() + " -> END"; //one arg
        return box.toString() + " -> " + this.next; //two+ args
    }
}
public class InventoryList {
  private LinkedBox head;
  private LinkedBox tail;
  private int size;
  private int yellowCount;
  private int blueCount;
  private int brownCount;

  //TODO remove these 2 methods later
  public LinkedBox getHead() {
    return this.head;
  }
  
  public LinkedBox getTail() {
    return this.tail;
  }

  //getters
  public int getBlueCount() {
    return this.blueCount;
  }

  public int getBrownCount() {
    return this.brownCount;
  }

  public int getYellowCount() {
    return this.yellowCount;
  }

  public Box get(int index) {
    int count = 0;
    Box box = new Box(Color.BLUE);
    while (head != null) {
      if (count == index) box = this.head.getBox();
      head = head.getNext();
      count++;
    }
    return box;
  }

  public int size() {
    return this.size;
  }

  //setters
  public void addBlue(Box blueBox) {
    if (head.getBox().getColor() != Color.YELLOW || tail.getBox().getColor() == Color.BROWN) head = new LinkedBox(blueBox, head); this.blueCount++; this.size++;
  }

  public void addBrown(Box brownBox) {
    tail = new LinkedBox(brownBox, tail);
    this.brownCount++; this.size++;
  }

  public void addYellow(Box yellowBox) {
    head = new LinkedBox(yellowBox, head);
    this.yellowCount++; this.size++;
  }

  //removers
  public Box removeBox(int inventoryNumber) {
    Box box = new Box(Color.BLUE);
    int index = 0;
    while (head != null) {
      if (inventoryNumber == index) {
        box = head.getBox();
        head = null;
      }
      index++;
      head = head.getNext();
    }
    return box;
  }

  public Box removeBrown() {
    Box box;
    if (tail.getBox().getColor() == Color.BROWN) {
      box = tail.getBox();
      tail = null;
      return box;
    }
    return new Box(Color.BROWN);
  }

  public Box removeYellow() {
    Box box;
    if (head.getBox().getColor() == Color.YELLOW) {
      box = head.getBox();
      head = null;
      return box;
    }
    return new Box(Color.YELLOW);
  }

  public void clear() {
    while (head != null) {
      head = null;
      head = head.getNext();
    }
  }

  //miscellaneous
  public boolean isEmpty() {
    if (head == null && tail == null) return true;
    return false;
  }

  public String toString() {
    if (head == null || tail == null) return "";
    return head+" "+tail+" <-TO STRING";
  }
}

If you are not using any integrated development environment (IDE) then use one, like Eclipse .如果您没有使用任何集成开发环境 (IDE),请使用一个,例如Eclipse To understand how code is working it's good to learn how to debug .要了解代码是如何工作的,最好学习如何调试

Your code has multiple issues.您的代码有多个问题。 I'll give you a few hints you do the rest.我会给你一些提示你做 rest。 Fix the toString() methods first so that you'll know what's going on.首先修复toString()方法,以便您知道发生了什么。
In LinkedBox#toString() method, the code is going in recursion and printing entire linked list.LinkedBox#toString()方法中,代码进行递归并打印整个链表。 Because this.next.toString() is getting called recursively.因为this.next.toString()被递归调用。 Change the last line to this:将最后一行更改为:

return box.toString() + " -> " + this.next.getBox(); //

Then implement InventoryList#toString() like this:然后像这样实现InventoryList#toString()

public String toString() {
    if (head == null || tail == null) return "";
        
    LinkedBox temporaryLinkedBox = head; //<-- use temporary variable
    StringBuilder result = new StringBuilder();
    while(temporaryLinkedBox!=null) {
        result.append(temporaryLinkedBox.toString()).append(" | ");
        temporaryLinkedBox = temporaryLinkedBox.getNext();
   }
   result.append(" <-TO STRING");
   return result.toString();
}

Notice how we are not using head to iterate over the list.请注意我们如何不使用head来遍历列表。 We are using temporary variable temporaryLinedBox to iterate.我们正在使用临时变量temporaryLinedBox进行迭代。

Same is the case with InventoryList#get and InventoryList#removeBox and get methods don't use head for iterating the list. InventoryList#getInventoryList#removeBox的情况也是如此,get 方法不使用 head 来迭代列表。 Use temporary variable:使用临时变量:

  public Box removeBox(int inventoryNumber) {
    Box box = null;
    int index = 0;
    LinkedBox temporaryLinkedBox = head;
    while (temporaryLinkedBox != null) {
      if (inventoryNumber == index) {
        box = temporaryLinkedBox.getBox();
        break;     //<-- use 'break' to exit loop
      }
      index++;
      temporaryLinkedBox = temporaryLinkedBox.getNext();
    }
    return box;
  }

Note: this is just a sample code you'll have to debug and fix errors if there are any.注意:这只是一个示例代码,如果有任何错误,您必须调试和修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM