【问题标题】:Is the implementation for Graph using Adjacency list correct?使用邻接列表的 Graph 实现是否正确?
【发布时间】:2021-01-08 08:03:18
【问题描述】:

我是 DSA 的初学者,从最近几天开始,我一直在尝试使用邻接列表为 Graph 找到正确的实现。

下面我提供了我认为应该实现邻接列表的方式的完整代码。

我从头开始创建了一个单链表。 我正在使用 Hashmap 来提高时间复杂度。

Hashmap 中的 Integer 键充当 VERTICE,其 VALUE 中包含一个 LinkedList。

在顶点中,我存储整数 ID,在 LinkedList 中,我存储该特定 ID 的所有朋友名称。

Graph 有 3 种方法:

  1. InsertVertice(int ID ) - 在 hashmap 的给定 ID 处创建一个空的 LinkedList。

  2. insertDataAtID (int ID, String data) - 将数据插入到给定 ID 的 LinkedList。

  3. printAllDataAtID(int ID) - 在 Hashmap 中的给定 ID/键处打印 LinkedList 中存在的所有朋友姓名或数据。

能否请您检查一下任何错误的实施和建议? 或者就如何更有效地实施邻接列表提出更好的建议?

感谢您的努力。


import java.util.HashMap;

public class demo {

    static HashMap<Integer,SinglyLinkedlist> graph = new HashMap<>();


    public static void main(String[] args){

      Graph graph = new Graph();


      graph.insertVertice(101);
      graph.insertDataAtID(101,"Deepesh");
      graph.insertDataAtID(101,"Kiran");
      graph.insertDataAtID(101,"Aryan");


      graph.insertVertice(201);
      graph.insertDataAtID(201,"Carl");
      graph.insertDataAtID(201,"Arun");
      graph.insertDataAtID(201,"Kishan");
      graph.insertDataAtID(201,"Betth");


      graph.printAllDataAtID(101);
      graph.printAllDataAtID(201);


    }

}

import java.util.HashMap;

public class Graph{

     HashMap<Integer,SinglyLinkedlist> maplist = new HashMap<>();


    void insertVertice(Integer id ){

        maplist.put(id,new SinglyLinkedlist());

    }

     void insertDataAtID(Integer id, String data){

        if (maplist.get(id)==null){
            System.out.println("No such Vertice exist with id : " + id);
            System.out.println("Create the Vertice first by calling insertVertice() method.");
        }

        SinglyLinkedlist linkedlist = maplist.get(id);
        linkedlist.insertNode(data);

    }


     void printAllDataAtID(Integer id) throws NullPointerException {
         if (maplist.get(id) == null) {
             System.out.println("No such Vertice exist with id : " + id);
             System.out.println("Create the Vertice first by calling insertVertice() method.");
         } else {

             SinglyLinkedlist linkedlist = maplist.get(id);
             linkedlist.printAll();

         }
     }

}

public class SinglyLinkedlist {

    Node head;
    Node tail;

    public static class Node {
        Node next;
        String data;
    }



    void insertNode(String data) {

        Node newNode = new Node();
        newNode.data = data;

        if (head == null) {
            head = tail = newNode;
            newNode.next = null;
        } else {

            Node temp = head;

            while (temp.next != null) {
                temp = temp.next;
            }

            temp.next = newNode;
            newNode.next = null;
            tail = newNode;

        }
    }


    void removeLastNode() {
        Node temp = head;

        while (temp.next.next != null) {
            temp = temp.next;
        }

        Node removedNode = temp.next;
        tail = temp;
        tail.next = null;

        System.out.println("Removed value : " + removedNode.data);

    }


    void printAll() {
        if (head == null) {
            System.out.println("List is Empty !");
        } else {
            Node temp = head;

            while (temp != null) {
                System.out.print(temp.data + "  ");
                temp = temp.next;
            }
        }
    }


    boolean search(String data) {
        if (head == null) {
            System.out.println("List is Empty !");
        } else {
            Node temp = head;

            while (temp != null) {

                if (temp.data.equals(data)) {
                    System.out.println("Value found !");
                    return true;
                }
                temp = temp.next;

            }

            System.out.println("Value not found !");

        }
        return false;
    }
}

【问题讨论】:

    标签: java data-structures


    【解决方案1】:

    如果您维护tail节点,则无需遍历列表插入。

    void insertNode(String data) {
        Node newNode = new Node();
        newNode.data = data;
        if (head == null) {
            head = tail = newNode;
            newNode.next = null;
        } else {
            tail.next = newNode;
            newNode.next = null;
            tail = tail.next;
        }
    }
    

    【讨论】:

    • 哦,是的,忘记了。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    相关资源
    最近更新 更多