使用字符串邻接表的图

时间:2018-06-28 14:42:46

标签: c string data-structures graph adjacency-list

我知道如何使用邻接表表示整数图。但是,使用字符串有点棘手。我正在尝试创建一个朋友图。源在指针数组的索引处。然后使用链接列表(如哈希)添加来源的朋友 现在,我创建了一个指针数组,这与将索引用作图形值的整数程序不同,我无法使用字符串列表来执行此操作。

指针数组存储初始字符串

请检查一下,任何解决方案都很好

// A C Program to demonstrate adjacency list 
// representation of graphs
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include  <string.h>
#include <string>

#define maxNode 4
int i;
int z;
typedef struct Node
{
    char vertexString[10];
    struct Node *next;
}Node;

Node *dest, *tmp;
typedef struct List
{
    Node*head;
    char vertexS[10];
    struct Node *next;
}List;

List*adjlist[maxNode] = { 0 };

void addNode(const char s[10], const char d[10]);
void printList();

int main()
{
        int i;
        for (i = 0; i < maxNode; i++)
        {
            adjlist[i] = (List *)malloc(sizeof(List));
            adjlist[i]->head = NULL;
        }
        addNode("Alam", "Shaib");
        addNode("ZEE", "PEE");
        addNode("ALOO", ",ALOO");

        printList();
        _getch();
}


void addNode(const char s[10],const char d[10])
{
        for (i = 0; i < 4;i++);
        {
            if (adjlist[i]->vertexS == s)
            {
                dest = (Node *)malloc(sizeof(Node));

                for (int j = 0; j < 10; j++)
                {
                    dest->vertexString[j] = d[j];
                }
                dest->next = NULL;

                tmp = adjlist[i]->head;
                while (tmp->next != NULL)
                    tmp = tmp->next;
                tmp->next = dest;
            }
        }

        for (z = 0; z < 4; z++);
        {
            if (adjlist[z] == 0)
            {
                dest = (Node *)malloc(sizeof(Node));
                for (int L = 0; L < 10; L++)
                {
                    dest->vertexString[L] = d[L];
                }
                dest->next = NULL;

                tmp = adjlist[z]->head;
                while (tmp->next != NULL)
                    tmp = tmp->next;
                tmp->next = dest;
            }
        }
    }

    void printList()
    {
        int i;
        for (i = 0; i < maxNode; i++)
        {
            Node *p;
            p = adjlist[i]->head;
            printf("Adjency list for vertex %s\n", adjlist[i]->vertexS);
            p = p->next;
            while (p)
            {
                printf("%s",p->vertexString);
                p = p->next;
            }

            printf("\n");
        }
    }

1 个答案:

答案 0 :(得分:1)

与其在邻接表中复制数据,不如将引用(数组索引或指针)包括到描述节点的结构中。

例如,假设您使用链接列表来描述节点,例如

typedef  struct node_list  node_list;
struct node_list {
    struct node_list *next;
    size_t            len;
    size_t            flag; /* Not usually needed, but sometimes useful */
    char              data[]; /* C99 flexible array member */
};

要查找现有节点或添加新节点,可以使用

node_list *node_list_node(node_list **rootptr, const char *name)
{
    const size_t  namelen = (name) ? strlen(name) : 0;
    node_list   **prev = rootptr, *curr;

    if (!rootptr)
        return NULL;  /* Error: No reference to node list root! */
    if (namelen < 1)
        return NULL;  /* Error: Empty name! */ 

    while ((curr = *prev))
        if (curr->len == namelen && !memcmp(curr->data, name, namelen))
            return curr;
        else
            prev = &(curr->next);

    curr = malloc(sizeof (node_list) + name_len + 1);        
    if (!curr)
        return NULL; /* Not enough memory */

    curr->next = NULL;
    curr->len  = namelen;
    memcpy(curr->data, name, namelen + 1);

    *prev = curr;

    return curr;
}

然后您可以将图边定义为另一个链接列表,例如

typedef  struct adjacency_list  adjacency_list;
struct adjacency_list {
    struct adjacency_list  *next;
    struct node_list       *edge_from;
    struct node_list       *edge_to;
};

这样添加一条边(无需检查重复项)就很简单

adjacency_list *add_edge(node_list **node, adjacency_list **edge,
                         const char *from, const char *to)
{
    node_list      *node_from, *node_to;
    adjacency_list *newedge;

    if (!node || !edge || !from || !*from || !to || !*to)
        return NULL;

    node_from = node_list_node(node, from);
    node_to   = node_list_node(node, to);
    if (!node_from || !node_to)
        return NULL;

    newedge = malloc(sizeof (adjacency_list));
    if (!newedge)
        return NULL;

    newedge->edge_from = node_from;
    newedge->edge_to   = node_to;

    /* Prepend to the existing adjacency list. */
    newedge->next = *edge;
    *edge = newedge;

    return newedge;
}

当然,如果任何node_list对象引用了adjacency_list,则必须注意不要重新分配或释放它,因为重新分配可能会移动该对象。 (然后再次,您可以通过检查所有adjacency_list对象来调整它们,但是如果您认为可以这样做,通常最好使用指针数组而不是链接列表,并使用数组索引而不是指向每个节点的指针本身。)

如果要以Graphviz点格式输出由adjacency_list链表描述的(子)图,但仅输出包含在列表中的节点,则{ {1}}结构派上用场。例如:

flag

然后,您可以使用Graphviz工具(例如node_listvoid fgraph(FILE *out, adjacency_list *edges) { adjacency_list *curredge; if (!out) return; /* First, clear the 'flag' field in all referred to nodes. (We actually only need one bit, though.) */ for (curredge = edges; curredge != NULL; curredge = curredge->next) { curredge->edge_from->flag = 0; curredge->edge_to->flag = 0; } /* Print a directed graph DOT preamble. */ fprintf(out, "digraph {\n"); /* Print each referred to node, but only once. */ for (curredge = edges; curredge != NULL; curredge = curredge->next) { if (!curredge->edge_from->flag) { node_list *currnode = curredge->edge_from; currnode->flag = 1; fprintf(out, " \"%p\" [ label=\"%s\" ];\n", (void *)currnode, currnode->data); } if (!curredge->edge_to->flag) { node_list *currnode = curredge->edge_to; currnode->flag = 1; fprintf(out, " \"%p\" [ label=\"%s\" ];\n", (void *)currnode, currnode->data); } } /* Print each edge in the (sub)graph. */ for (curredge = edges; curredge != NULL; curredge = curredge->next) { fprintf(out, " \"%p\" -> \"%p\";\n", (void *)(curredge->edge_from), (void *)(curredge->edge_to)); } /* Done. */ fprintf(out, "}\n"); } dot来创建图形的图像。