Создание взвешенного графика из Java с нулями - PullRequest
0 голосов
/ 23 апреля 2019

Я делаю взвешенный график для реализации алгоритма Дейкстры.

Подробности взвешенного графа: каждая вершина является строкой "MSW" "WEL" "POR" Вес каждой вершины - целое число 200, 104, 30 и т. Д. *

Мне нужно сделать это, создав собственный класс Node and 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);

    }
}

Я не думаю, что я на правильном пути, и я просто нахожусь в тупике, любая обратная связь или помощь будут удивительными и спасающими жизнь

...