java数据结构表示公交线路用途代码
时间: 2023-07-07 16:45:07 浏览: 52
以下是使用Java数据结构表示公交线路的示例代码:
1. 数组表示公交线路
```java
String[] busLine = {"A站", "B站", "C站", "D站", "E站"};
```
2. 链表表示公交线路
```java
class BusStop {
String name;
BusStop prev;
BusStop next;
public BusStop(String name) {
this.name = name;
this.prev = null;
this.next = null;
}
}
BusStop a = new BusStop("A站");
BusStop b = new BusStop("B站");
BusStop c = new BusStop("C站");
BusStop d = new BusStop("D站");
BusStop e = new BusStop("E站");
a.next = b;
b.prev = a;
b.next = c;
c.prev = b;
c.next = d;
d.prev = c;
d.next = e;
e.prev = d;
```
3. 栈表示公交线路
```java
Stack<String> busLine = new Stack<>();
busLine.push("A站");
busLine.push("B站");
busLine.push("C站");
busLine.push("D站");
busLine.push("E站");
```
4. 队列表示公交线路
```java
Queue<String> busLine = new LinkedList<>();
busLine.offer("A站");
busLine.offer("B站");
busLine.offer("C站");
busLine.offer("D站");
busLine.offer("E站");
```
5. 图表示公交线路
```java
class BusStop {
String name;
List<BusStop> neighbors;
public BusStop(String name) {
this.name = name;
this.neighbors = new ArrayList<>();
}
}
BusStop a = new BusStop("A站");
BusStop b = new BusStop("B站");
BusStop c = new BusStop("C站");
BusStop d = new BusStop("D站");
BusStop e = new BusStop("E站");
a.neighbors.add(b);
b.neighbors.add(a);
b.neighbors.add(c);
c.neighbors.add(b);
c.neighbors.add(d);
d.neighbors.add(c);
d.neighbors.add(e);
e.neighbors.add(d);
```