我正在制作一个加权图来实现Dijkstra的算法。
加权图的详细信息: 每个顶点是一个字符串“ MSW”,“ WEL”,“ POR” 每个顶点的权重是整数200、104、30等...
我需要通过创建自己的Node和Linked List类来做到这一点。我无法导入软件包。
我一直在努力使其正常运行,但是在将整个组件组合在一起时遇到了麻烦。我认为我已经接近了,但我很有可能会离开很多地方。
我的想法是制作一个具有Edge Node类型的链接列表,并制作一个具有String类型的单独的链接列表,并为两者创建一个邻接列表。
public class WeightedGraph
{
class LinkedListE
{
class Enode{
Edge data;
Enode next;
public Enode (Edge d) {
data = d;
}
public Enode (Edge d, Enode n) {
data = d;
next = n;
}
public void EsetData (Edge newData) {
data = newData;
}
public void EsetNext (Enode newNext) {
next = newNext;
}
public Edge EgetData () {
return data;
}
public Enode EgetNext () {
return next;
}
public void EdisplayNode () {
System.out.print (data);
}
}
private Enode first;
public LinkedListE() {
first = null;
}
public boolean empty () {
return (first == null);
}
public int size () {
int count = 0;
Enode current = first;
while (current != null) {
count++;
current = current.EgetNext();
}
return count;
}
public void EinsertFirst (Edge newData) {
Enode newFirst = new Enode (newData);
newFirst.EsetNext(first);
first = newFirst;
}
public Enode EdeleteFirst () {
Enode temp = first;
first = first.EgetNext();
return temp;
}
public boolean search (Edge key) {
boolean result = false;
Enode current = first;
while (current != null) {
if (current.EgetData () == key) {
result = true;
return result;
}
else
current = current.EgetNext();
}
return result;
}
public void traverse () {
System.out.print ("Current list: ");
Enode current = first;
while (current != null) {
current.EdisplayNode ();
System.out.print (" ");
current = current.EgetNext();
}
System.out.println ();
}
}
class LinkList {
class Node {
private String data;
private Node next;
public Node () {
this("", null);
}
public Node (String d) {
data = d;
}
public Node (String d, Node n) {
data = d;
next = n;
}
public void setData (String newData) {
data = newData;
}
public void setNext (Node newNext) {
next = newNext;
}
public String getData () {
return data;
}
public Node getNext () {
return next;
}
public void displayNode () {
System.out.print (data);
}
}
private Node first;
public LinkList () {
first = null;
}
public boolean empty () {
return (first == null);
}
public int size () {
int count = 0;
Node current = first;
while (current != null) {
count++;
current = current.getNext();
}
return count;
}
public void insertFirst (String newData) {
Node newFirst = new Node (newData);
newFirst.setNext(first);
first = newFirst;
}
public Node deleteFirst () {
Node temp = first;
first = first.getNext();
return temp;
}
public boolean search (String key) {
boolean result = false;
Node current = first;
while (current != null) {
if (current.getData () == key) {
result = true;
return result;
}
else
current = current.getNext();
}
return result;
}
public void traverse () {
System.out.print ("Current list: ");
Node current = first;
while (current != null) {
current.displayNode ();
System.out.print (" ");
current = current.getNext();
}
System.out.println ();
}
}
class Edge{
int source, destination, weight;
public Edge(int source, int destination, int weight)
{
this.source = source;
this.destination = destination;
this.weight = weight;
}
}
int n;
LinkList dongman[];
ELinkList adjacencylist[];
class Edge{
int source, destination, weight;
public Edge(int source, int destination, int weight)
{
this.source = source;
this.destination = destination;
this.weight = weight;
}
}
public void Graph(int n)
{
this.n = n;
adjacencylist = new ELinkList[n];
for (int i = 0; i < n; i++)
{
adjacencylist[i] = new ELinkList();
}
}
public void addEdge(int source, int destination, int weight)
{
Edge edge = new REdge(source, destination, weight);
adjacencylist[source].EinsertFirst(edge);
}
}
我不认为自己走在正确的道路上,我只是处于精神障碍,任何反馈或帮助都将是惊人的,挽救生命