当前位置: 首页 > 知识库问答 >
问题:

图使用邻接表的实现是否正确?

濮阳研
2023-03-14

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

下面我给出了我认为邻接列表是如何实现的整个代码。

我从头开始创建了一个SiinglyLinkedlist。并且我正在使用一个Hashmap来改进时间复杂性。

Hashmap中的整数键充当顶点&在其值中包含一个Linkedlist。

在顶点中,我存储了整数ID,在Linkedlist中,我存储了该ID的所有好友名。

图中有3种方法-

1]insertVertice(int ID)-这将在hashmap中的给定ID处创建一个空linkedlist。

2]insertDataAtID(int ID,String data)-这将以给定的ID在linkedlist中插入数据。

3]printAllDataAtID(int ID)-这将打印链接列表中以给定ID/键显示的所有好友名称或数据。

你能检查一下实施过程并给出任何错误的建议吗?或者更好的一些建议如何更有效地实现邻接列表?

谢谢你的努力。

import java.util.HashMap;

public class demo {

    static HashMap 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 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 NullPointerExcepthtml" target="_blank">ion {
         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;
    }
}

共有1个答案

梁丘琛
2023-03-14

如果要维护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;
    }
}
 类似资料:
  • 对于许多点,我有2D的协定子,例如点a=x,y 我想做一个图的实现使用邻接列表列表和连接某些点的无方向图在最有效的方式(不使用地图或哈希表)

  • 我正在学习技术面试和图表对我来说有点困难。我对邻接矩阵很容易,但对邻接列表的实现很困惑。 问题是我在网上看到的邻接列表的大多数实现(Example1、Example2和Example3)根本不使用节点。他们只是使用一个由int和linkedlists组成的HashMap。这是正确的吗?因为定义(维基百科)说它由顶点或节点组成。此外,大多数使用邻接矩阵的图的实现都使用节点,而不是INT。例4。我是不

  • 本文向大家介绍C++实现图的邻接矩阵表示,包括了C++实现图的邻接矩阵表示的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C++实现图的邻接矩阵表示代码,供大家参考,具体内容如下 1.遇到的问题:教材中写着子类Graphmtx(我用GrapMatrix)继承基类Graph 但是我在子类GraphMatrix中使用父类Graph的保护成员属性:maxVertices 显示没有声明(如下

  • 在斯坦福的一门算法课程中,教授为图的邻接表表示列出了以下成分: 顶点数组或列表 边的数组或列表 顶点列表中的每个顶点都指向其上的边。 边列表中的每个边都指向其边点。 这种表示法与图形的“关联表”表示法相同吗?如果是,为什么本文认为“邻接表”和“发生率表”是分开的?

  • 作为一项练习,我必须建立一个卫星导航系统,规划从一个位置到另一个位置的最短和最快路线。它必须尽可能快,而不需要使用太多内存。 我很难决定使用哪种结构来表示图形。我知道矩阵更适合密集图,列表更适合稀疏图。我更倾向于使用列表,因为我认为添加顶点将是这个程序中最累人的部分。 我只是想听听你们的意见。如果我把一个典型的路线图看作一个图形,其中不同的位置是节点,道路是边缘。你认为它是稀疏的还是密集的?在这种