java - 协助仅使用节点创建 java 链表

标签 java data-structures linked-list

使用 Java 从事这个项目已有一段时间了。建议我为我的程序使用链接列表或数组列表,这非常有意义。然而,教授说我们必须利用节点制作和使用我们自己的链表。尽管进行了一些研究并在类里面四处询问,但使用 Nodes 还是让我感到非常困惑。我确定这是我所缺少的简单东西,但我现在完全不知所措。这是存储列表的类(我认为)。之所以命名为 Aircraft,是因为我们正在创建一个列表来存储多架飞机以及与它们相关的一些详细信息(航类名称、速度、高度、飞机类型)。我有一个用户与之交互的主类(未列出)——我已经很好地处理了该类。

package airTraffic;

public class Aircraft  {

public static String name;
public static String type;
public static int speed;
public static int alt;

Aircraft nextCraft;

public Aircraft (String n, String t, int s, int a) {
    name = n;
    type = t;
    speed = s;
    alt = a;
}

public Aircraft() {

}

public static void setName(String n) {
    name = n;
}

public static String getName (String lookUp) {
    return name;
}

public static void removeName () {
    //remove the flight - not sure what to place here
}

public static void setType (String t) {
    type = t;
}

public static String getType () {
    return type;
}

public static void setSpeed (int s) {
    speed = s;
}

public static int getSpeed () {
    return speed;
}

public static void setAlt(int a) {
    alt = a;
}

public static int getAlt () {
    return alt;
}

public Aircraft next = null;

//auto generated method from ATControl 
public static void add(String s) {

}

//auto generated methods from ATControl - what goes here???
public static void remove() {

}

public Object getNext() {
    // TODO Auto-generated method stub
    return null;
}

public void setNext(Object next2) {
    // TODO Auto-generated method stub

}
 }

下面是我认为创建和存储节点的类。这是我非常困惑并认为我错了的地方。我不确定如何调用节点来实际向其添加和存储数据。我还需要能够获取节点(通过航类名称)并删除节点(通过航类名称)

package airTraffic;

import java.util.*;
import airTraffic.Aircraft;

public class ATControl {


static Main m = new Main ();
Aircraft aircraft = new Aircraft ();

//declare node names for list
public static Aircraft head = new Aircraft ();
public static Aircraft tail = new Aircraft ();

// stores data
private static final int INITIAL_ALLOCATION = 20;
private static int size = INITIAL_ALLOCATION; 

// tells list to add nodes
public static void Nodes (String s, int n) {
    n = size;
    head.next = tail;
    tail.next = tail;
    Aircraft temp = head;
    for (int i= 0; i < size; ++i) {
        temp.next = new Aircraft ();
        temp = temp.next;
    }
    temp.next = tail;
}

public static void addNodes (int n) {
    n = size;
    Aircraft temp = new Aircraft ();
    Aircraft current = head;
    for (int i = 1; i < n && current.getNext() != null; i++) {
        current = (Aircraft) current.getNext();
        temp.setNext(current.getNext());
        current.setNext (temp);
        size++;
    }
}

//add plane and details
public static void addToList (Scanner in) {
    // should add new aircraft to new node on linked list
    System.out.printf("Enter flight number: ");
    String add = in.next();
    Aircraft.setName (add);
    ATControl.addNodes (Integer.parseInt(add));

    //type of plane
    System.out.printf("Enter type of plane: ");
    String plane = in.next();
    Aircraft.setType (plane);

    //plane speed
    System.out.printf("Enter current speed: ");
    int speed = in.nextInt();
    Aircraft.setSpeed (speed);
    ATControl.addNodes (Integer.parseInt(add));


    //add Altitude 
    System.out.printf("Enter current altitude: ");
    int alt = in.nextInt();
    Aircraft.setAlt(alt);
    ATControl.addNodes (Integer.parseInt(add));  // I am fairly certain this is wrong
}

//show flight
public static void showFlight (Scanner in) {
    System.out.printf("Enter flight number for details: ");
    String lookUp = in.next();
    Aircraft.getName(lookUp);

}
// display all flights
public static void displayAll (Scanner in) {
    System.out.printf("All flights: " );

}
//remove flight
public static void removeFlight (Scanner in) {
    System.out.printf("Enter flight number to be removed: ");

}
}

最佳答案

你越来越近了。首先,链表是一个对象列表,通常称为节点,每个节点都有一个或多个指向其他对象的链接。在您的例子中,节点是飞机。

这应该对您有所帮助:Wikipedia:Linked List

到目前为止,您的主要问题是您的 Aircraft 类中没有链接。由于这是一个链表,您需要在列表中包含对下一个元素的引用。在 Aircraft 类中,您应该有一个类型为 Aircraft 的名为 next 的属性,它将您链接到列表中的下一个 Aircraft。这允许您调用 myAircraft.next,就像您目前在您的代码中一样,这将允许您按顺序向下移动列表。我会让你自己解决剩下的问题,这是家庭作业,但如果你需要更多解释,请随时发表评论。

关于java - 协助仅使用节点创建 java 链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11751227/

相关文章:

java - 为什么linkedlist.java不导出Node类

c++ - 在其自己的析构函数中删除链表是否安全?

java - Animate ball JPanel subclass is not abstract错误

java - java中的类字典(数据结构)

java - 如何创建一个与我的桌面看起来一样的 Java Web 应用程序?

algorithm - 如何从中间搜索尝试

c++ - 这样使用嵌套 vector/multimap/map 可以吗?

c - 从多类型堆栈中弹出并获取值(value)

java - 我如何学习*实用的*自然语言处理?

java - 智能 : Setting Remote host Mappings: Local path is out of project