一直在用 Java 做这个项目。建议我为我的程序使用链接列表或数组列表,这非常有意义。然而,教授说我们必须利用节点制作和使用我们自己的链表。尽管在课堂上进行了一些研究和询问,但使用 Nodes 让我非常困惑。我确定这是我缺少的一些简单的东西,但我现在完全不知所措。这是存储列表的类(我认为)。之所以命名为 Aircraft,是因为我们正在创建一个列表来存储多架飞机以及与它们相关的一些详细信息(航班名称、速度、高度、飞机类型)。我有一个用户与之交互的 Main 类(未列出) - 我已经处理了该类。
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: ");
}
}