地铁管理系统 Java数据结构
时间: 2023-06-23 14:56:19 浏览: 70
好的,下面是一个简单的地铁管理系统Java代码,使用了数据结构图(Graph)来表示地铁路线,使用了深度优先搜索(DFS)算法来实现路线查询功能:
```java
import java.util.*;
public class SubwayManagementSystem {
private static class Graph {
private Map<String, List<String>> adjacencyMap;
Graph() {
adjacencyMap = new HashMap<>();
}
void addStation(String stationName) {
adjacencyMap.put(stationName, new ArrayList<>());
}
void addConnection(String station1, String station2) {
adjacencyMap.get(station1).add(station2);
adjacencyMap.get(station2).add(station1);
}
List<String> getConnectedStations(String station) {
return adjacencyMap.get(station);
}
}
private static void DFS(Graph graph, String start, String end, boolean[] visited, List<String> path) {
visited[graph.getConnectedStations(start).indexOf(end)] = true;
path.add(end);
if (start.equals(end)) {
System.out.println(path);
path.remove(path.size() - 1);
visited[graph.getConnectedStations(start).indexOf(end)] = false;
return;
}
for (String station : graph.getConnectedStations(start)) {
if (!visited[graph.getConnectedStations(start).indexOf(station)]) {
path.add(station);
DFS(graph, station, end, visited, path);
path.remove(path.size() - 1);
}
}
visited[graph.getConnectedStations(start).indexOf(end)] = false;
}
public static void main(String[] args) {
Graph subwayMap = new Graph();
subwayMap.addStation("A");
subwayMap.addStation("B");
subwayMap.addStation("C");
subwayMap.addStation("D");
subwayMap.addStation("E");
subwayMap.addStation("F");
subwayMap.addStation("G");
subwayMap.addConnection("A", "B");
subwayMap.addConnection("A", "C");
subwayMap.addConnection("B", "D");
subwayMap.addConnection("C", "D");
subwayMap.addConnection("C", "E");
subwayMap.addConnection("D", "F");
subwayMap.addConnection("E", "F");
subwayMap.addConnection("E", "G");
boolean[] visited = new boolean[7];
List<String> path = new ArrayList<>();
path.add("A");
DFS(subwayMap, "A", "F", visited, path);
}
}
```
这里使用了一个简单的地铁路线图作为示例,你可以根据自己的需求和数据结构进行修改和扩展。