微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自一个简单 Java 程序的链表 ProductOperation()

如何解决来自一个简单 Java 程序的链表 ProductOperation()

我正在大学学习数据结构课程,但我无法理解为什么我的链表 ProductOperation() 有几个错误,尤其是在 addProduct()deleteProduct() 和 {{1 }} 方法。 这是我的节点类:

displayProduct()

这是我的产品代码

public class Node {

    private Product info;
    private Node link;

    public Node(Product product) {
        this.info = product;
        link = null;
    }

    public Product getInfo() {
        return info;
    }

    public Node getLink() {
        return link;
    }

    public void setProduct(Product product) {
        info = product;
    }

    public void setLink(Node newLink) {
        this.link = link;
    }
}

这是我的 public class Product { private String name; private int quantity; private double price; private double total; public Product(String name,int quantity,double price) { this.name = name; this.quantity = quantity; this.price = price; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public int getQuantity() { return this.quantity; } public double getPrice() { return this.price; } public double getDouble() { return this.total; } } 代码

ProductOperation()

如果这是一个基本问题,请提前抱歉。我问过我的教授,他给我发了一个 YouTube 链接,但实际上并没有帮助。 感谢您抽出宝贵时间。

解决方法

是的,很多错误,这是我一见钟情的东西,我不提供修复,因为如果你自己做它会更有用。

public void searchProduct(String target) {
    //You start with head,but never check if head is null
    Node current = head;
    //product is initialized to null and remains null,since there is no other assigment to it
    Product product = null;
    //With this condition you skip the last valid node of the list (that has link == null)
    while (current.getLink() != null) {
        //product here is null,get it first from the current node
        //Strings must be compared with equals(),not with ==
        if (product.getName() == target) {
            System.out.println("The price of " + product.getName() + " = " + product.getPrice());
        //Here you should return,since the search is successful
        }
    }
    System.out.println("Sorry,the product " + product.getName() + " does not exist.");
}

关于删除产品:

public void deleteProduct(String target) {
    Product product;
    //Why a new Node? You are deleting
    Node newNode = new Node(product);
    if (head == null) {
        System.out.println("List is empty");
    //Again,must compare with equals(...),moreover you are comparing a Node with a String
    //First,get the product then compare product's name with target
    } else if (head.getInfo() == target) {
        head = head.getLink();
    } else {
        Node before = null;
        Node current = head;
        //Same error as above,getInfo() returns a node,target is a string
        while ((current.getInfo() != target) && (current != null)) {
            before = current;
            current = current.getLink();
        }
        //Same error,plus here current may be null and you may get a NPE
        if (current.getInfo() == target) {
            before.setLink(current.getLink());
        } else {
            System.out.println("The target does not exist");
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。