c++ - 将链表存储为数组 C++ 中的索引

标签 c++ graph linked-list

概述:我目前正在尝试编写一个代码,该代码将采用 AOV 网络图并将其放入链接列表中。我怎么会遇到麻烦最好的方法是什么。我在纸上理解如何以链表形式表示图形。

这是我的图表:

enter image description here

我的思考过程/Pusdo 代码如下:

Create a Linked-List class that just adds the nodes and prints them out for the methods. Then for each edge access the index of an array and go to that Linked list. So then my array would have [s,a,d,g,b,e,h,c,f,i,t] where each letter would represent the >Linked list of that vertex. So if i would like to call the S vertex i would have to call the >0 element of the array and that would point to s Linked-List. Is there a easy implementation >of this in C++?

现在这是我的链表类:

class List{
private:
    typedef struct node{
        char vertex;
        node* next;
    }* nodePtr;
    nodePtr head;
    nodePtr curr;
    nodePtr temp;
public:
    List();
    void AddNode(char AddData);
    void printList();
};

List::List(){
    head = NULL;
    curr = NULL;
    temp = NULL;
}

void List::AddNode(char AddData){
    nodePtr n = new node;
    n->next = NULL;
    n->vertex = AddData;

    if(head != NULL){
        curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }
    else{
        head = n;
    }
}

void List::printList(){
    curr = head;
    while(curr != NULL){
        cout << curr->vertex << endl;
        curr = curr->next;
    }
}

如有任何帮助,我们将不胜感激。

编辑: 可以像这样使用双链表吗? enter image description here

最佳答案

有一个名为Adjacent Table的数据结构,用于将图存储在链表结构中

AT中的元素是这样的

struct ATEle {
    char name;
    ATEle * neighborPointer;
    ATEle * nodelistPointer;
}

图中的节点组织在一个链表中,每个节点也是它所有邻居的链表的头

例如我们有一个由 4 个节点组成的图,A、B、C、D,A 与 B、C、D 与 A 相连,B 与 C、D 相连,在相邻的表中它看起来像:

A -> B -> C

|

B -> C ->D

|

C

|

D -> A

关于c++ - 将链表存储为数组 C++ 中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17969825/

相关文章:

c++ - 为什么迭代 std::set 比迭代 std::vector 慢得多?

r - 如何构建自动更新的拖放层次树

algorithm - 如何使用回溯求图着色的时间复杂度?

c++ - C++中的简单链表

c - 在C中的链表的末尾或开头添加节点

java - 基于索引,随机访问和顺序访问之间的区别?

c++ - 'return false' 不从函数返回

c++ - 数组对象初始化,其类具有一些 ctor/dtor

c++ - 系统更新后Cmake无法链接glut库

python-2.7 - 从 DOT 文件在 python 中使用 graphviz 绘制有向图