C 使用字符串邻接列表的图

C 使用字符串邻接列表的图,c,string,data-structures,graph,adjacency-list,C,String,Data Structures,Graph,Adjacency List,我知道如何用邻接表表示整数图。但是,对于字符串,这有点棘手。我正在尝试创建一个朋友图。其中,源位于指针数组的索引处。然后使用链表(如哈希)添加源的好友 现在,我创建了一个指针数组,与整数程序不同,整数程序使用索引作为图形值,我不能使用字符串列表 指针数组存储初始字符串 请检查它,任何解决方案都会很好 // A C Program to demonstrate adjacency list // representation of graphs #include <stdio.h>

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

指针数组存储初始字符串

请检查它,任何解决方案都会很好

// 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");
        }
    }
//演示邻接列表的C程序
//图的表示
#包括
#包括
#包括
#包括
#包括
#定义maxNode 4
int i;
intz;
定义表结点
{
字符顶点字符串[10];
结构节点*下一步;
}节点;
节点*dest,*tmp;
类型定义结构列表
{
节点*头;
字符顶点[10];
结构节点*下一步;
}名单;
List*adjlist[maxNode]={0};
void addNode(常量字符s[10],常量字符d[10]);
作废打印列表();
int main()
{
int i;
对于(i=0;ihead=NULL;
}
addNode(“ALM”、“Shaib”);
addNode(“ZEE”、“PEE”);
addNode(“ALOO”,“ALOO”);
打印列表();
_getch();
}
void addNode(常量字符s[10],常量字符d[10])
{
对于(i=0;i<4;i++);
{
如果(adjlist[i]->vertexS==s)
{
dest=(Node*)malloc(sizeof(Node));
对于(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;
}
}
对于(z=0;z<4;z++);
{
如果(调整列表[z]==0)
{
dest=(Node*)malloc(sizeof(Node));
对于(int L=0;L<10;L++)
{
dest->vertexString[L]=d[L];
}
dest->next=NULL;
tmp=调整列表[z]->头部;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=dest;
}
}
}
作废打印列表()
{
int i;
对于(i=0;i头部;
printf(“顶点%s的邻接列表\n”,邻接列表[i]->vertexS);
p=p->next;
while(p)
{
printf(“%s”,p->vertexString);
p=p->next;
}
printf(“\n”);
}
}

与其复制邻接列表中的数据,不如简单地包含对描述节点的结构的引用(数组索引或指针)

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

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 */
};
typedef  struct adjacency_list  adjacency_list;
struct adjacency_list {
    struct adjacency_list  *next;
    struct node_list       *edge_from;
    struct node_list       *edge_to;
};
要查找现有节点或添加新节点,可以使用

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;
}
因此,添加边(不检查重复项)与

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;
}
当然,如果任何
邻接列表
对象引用了
节点列表
,则必须注意不要重新分配或释放该节点列表,因为重新分配可能会移动该对象。(同样,您可以通过检查所有对象来调整
邻接列表
对象,但如果您认为可以这样做,通常最好使用指针数组而不是链表,并使用数组索引而不是指针本身来引用每个节点。)

如果您想以Graphviz点格式输出由
邻接列表
链表描述的(子)图,但只输出列表中包含的节点,则
节点列表
结构中的
标志
成员很方便。例如:

void 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");
}

然后,您可以使用Graphviz工具,例如
dot
twopi
circo
,来创建图形的图像。

我不能说我完全得到了您想要的,但是否可以只维护名称和整数之间的双射,并简单地坚持整数图形解决方案?欢迎使用。这看起来有点可疑:
for(z=0;z<4;z++);{
这是一个带有空主体的循环。代码中有两个循环。