首页 > 解决方案 > 来自简单 Java 程序的链表 ProductOperation()

问题描述

我正在大学学习数据结构课程,但我无法理解为什么我的链接列表ProductOperation()有几个错误,尤其是在addProduct(),deleteProduct()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()代码:

public class ProductOperation {

    private Node head;

    public ProductOperation() {
        head = new Node(null;
        }

    public void addProduct(Product product) {
        Node newNode = new Node(product);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.getLink() != null) {
                current = current.getLink();
            }
            current.setLink(newNode);
        }
    }

    public double calculateTotalPrice(Product product) {
        return product.getQuantity() * product.getPrice();
    }

    public void searchProduct(String target) {
        Node current = head;
        Product product = null;
        while (current.getLink() != null) {
            if (product.getName() == target) {
                System.out.println("The price of " + product.getName() + " = " + product.getPrice());
            }
        }
        System.out.println("Sorry, the product " + product.getName() + " does not exist.");
    }

    public void deleteProduct(String target) {
        Product product;
        Node newNode = new Node(product);
        if (head == null) {
            System.out.println("List is empty");
        } else if (head.getInfo() == target) {
            head = head.getLink();
        } else {
            Node before = null;
            Node current = head;
            while ((current.getInfo() != target) && (current != null)) {
                before = current;
                current = current.getLink();
            }
            if (current.getInfo() == target) {
                before.setLink(current.getLink());
            } else {
                System.out.println("The target does not exist");
            }
        }
    }

    public void displayAllProducts() {
        Node node = head;

        System.out.printf("%%10s %30s %20s %5s ", "Name", "Price", "Quantity", "Total");
        System.out.println("====================================================================================");
        System.out.format("%%10s %30.2f %20s %5.2f ", product.getName(), product.getPrice(), product.getQuantity(), product.getDouble());

    }
}

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

标签: javaalgorithmdata-structureslinked-listnodes

解决方案


是的,很多bug,这里是我一眼就能找到的,我不提供修复,因为如果你自己做会更有用。

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");
        }
    }
}

推荐阅读