简体   繁体   English

Java 中的 addAfter() 单链表实现

[英]addAfter() Singly Linked List Implementation in Java

Hi stackoverflow community!嗨,stackoverflow 社区!

I am trying to implement addAfter() method for my SinglyLinkedList and I thought I was doing it correctly, but nothing is printing out.我正在尝试为我的 SinglyLinkedList 实现 addAfter() 方法,我认为我做对了,但没有打印出来。 Would love to see y'alls insight on what I might be missing:很想看到你们对我可能缺少的东西的见解:

public class LinkedList {
private EmployeeNode head;
private EmployeeNode tail;
private int size;

public void addAfter(EmployeeNode node, Employee newEmployee) {
    EmployeeNode newNode = new EmployeeNode(newEmployee);
    newNode.setNext(node.getNext());
    node.setNext(newNode);
    size++;
    if(tail == null) {
        tail = newNode;
    }
  } 
}

*To keep the code brief, I have not added the other methods, but everything else works perfectly fine such as append(), addToFront(), removeFromEnd(), printList(), etc. *为了保持代码简洁,我没有添加其他方法,但其他所有方法都可以正常工作,例如 append()、addToFront()、removeFromEnd()、printList() 等。

Main Method:主要方法:

public class Main {
public static void main(String[] args) {
    Employee janeSmith = new Employee("Jane", "Smith", 44);
    Employee maryJames = new Employee("Mary", "James", 34);
    Employee johnDoe = new Employee("John", "Doe", 78);
    Employee andrewJackson = new Employee("Andrew", "Jackson", 24);
    EmployeeNode node = new EmployeeNode(maryJames);

    LinkedList list = new LinkedList();
    list.addToFront(janeSmith);
    list.addToFront(maryJames);
    list.addToFront(johnDoe);
    list.addAfter(node, andrewJackson);
    list.printList();
}

**Employee Class just has firstName, lastName, and id instance variables as seen in the above instantiations. **Employee 类只有 firstName、lastName 和 id 实例变量,如上述实例所示。

EmployeeNode Class:员工节点类:

public class EmployeeNode {
private Employee employee;
private EmployeeNode next;

public EmployeeNode(Employee employee) {
    this.employee = employee;
}

public Employee getEmployee() {
    return employee;
}

public void setEmployee(Employee employee) {
    this.employee = employee;
}

public EmployeeNode getNext() {
    return next;
}

public void setNext(EmployeeNode next) {
    this.next = next;
}

public String toString() {
    return employee.toString();
}
}

When you say当你说

EmployeeNode node = new EmployeeNode(maryJames); EmployeeNode 节点 = 新 EmployeeNode(maryJames);

You are creating a node that contains maryJames and has no value for next .您正在创建一个包含 maryJames并且对 next 没有值的节点。

Than you say比你说的

addToFront(maryJames) addToFront(玛丽詹姆斯)

You are creating an different EmployeeNode that contains maryJames.您正在创建一个包含 maryJames 的不同EmployeeNode。 This EmployeeNode has the 'next' element set correctly (I assume) by the addFront(method).这个 EmployeeNode 具有通过 addFront(method) 正确设置的“next”元素(我假设)。

When you say当你说

list.addAfter(node, andrewJackson); list.addAfter(node, andrewJackson);

You are using the EmployeeNode that does not have 'next' set.您正在使用没有设置“下一个”的 EmployeeNode。 And the addAfter() method won't work correctly.并且 addAfter() 方法将无法正常工作。

You need to use the instance of EmployeeNode is part of the LinkedList.您需要使用的EmployeeNode 实例是LinkedList 的一部分。

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

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