c - 具有动态链表的图形表示

标签 c graph linked-list

首先我要说我是 C 的新手,现在,我正在为一些非常不直观的错误而苦苦挣扎。我已经尝试了很长一段时间以找到某种解决方案,但我总是走到死胡同。

我正在尝试构建几个用于通过动态链表插入和显示图形的函数。在编译时一切正常,但元素似乎没有很好地显示。实际上,就像下图一样,只显示了节点的第一个元素。

所以问题是是什么导致了这些错误和警告,我应该怎么做才能删除它们?

enter image description here

如果你看一下下面的代码,你会发现它有一些警告(我不知道为什么会出现 - 我在 Ubuntu 中使用带有 GNU 编译器的代码块)并且在显示图的元素。问题很可能出在 display_graph 函数中,但我不知道出在哪里。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct AdjListNode {
    int dest;
    struct LIST_NODE *next;
} LIST_NODE;

typedef struct AdjList {
    struct LIST_NODE *head;
} ADJACENCY_LIST;

LIST_NODE *create_node(int dest) {
    LIST_NODE *nod;
    if(dest<0) exit(0);
    nod = (LIST_NODE*)malloc(sizeof(LIST_NODE));
    if(nod==NULL) {
        printf("Problems at memory allocation!");
        exit(0);
    }
    nod->dest = dest;
    nod->next = NULL;
    return (LIST_NODE*)nod;
}

void display_graph(ADJACENCY_LIST *v) {
    int s, i;
    LIST_NODE *nod;
    s = sizeof(v);
    for(i=0;i<=s;i++) {
        nod = v[i].head;
        //citeste lista cu head in primul nod
        while(nod!=NULL) {
            printf("Data from node: %d \n", nod->dest);
            nod = nod->next;
        }
    }
}

int main()
{
    int n; //number of graph nodes
    int i; //just a counter
    int dest; dest = -1; //it's actually the "name" of the nodes. They  must all be positive so I started negative
    char c;
    ADJACENCY_LIST *t;
    printf("The number of nodes of the graph: ");
    scanf("%d", &n);
    t = (ADJACENCY_LIST*)malloc(n*sizeof(ADJACENCY_LIST));

    /* We make a loop for the nodes and each node has a while thru which I make the links */
    for(i=0;i<n;i++) {
        c = 'D'; // Initializing
        printf("Specify the links of the node %d with the others:\n", i);
        int contor; contor = 0;
        while(c=='D') {
            LIST_NODE *nod;
            printf("The link with node: ");
            scanf("%d%*c", &dest);
            if(dest>=0){
                 nod = create_node(dest);
                 if(contor==0) t[i].head = (LIST_NODE*)nod; // just make the first node a head node
            } else nod = NULL;
            //verificam daca vrem sa continuam
            printf("Do you want to link any other node to %d?(D to add, anything else STOP\n)", i);
            c = getchar();
            contor++; //increment counter
        }
        // inchidem lista
    }
   display_graph(t);
return 0;
}

任何帮助将不胜感激!

编辑: 正如 Christhofe(确认了这个问题)和 Abhishek Vasisht 指出的那样, vector v 的大小实际上返回了指针的大小。

但是,仍然有一些警告,我不知道为什么它们仍然出现......都是

||=== 构建:在 Grafuri1 中调试(编译器:GNU GCC 编译器)===| /home/marianpc/Anul_1/SDA/Grafuri1/main.c||在函数“display_graph”中:| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|33|警告:来自不兼容指针类型的赋值| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|38|警告:来自不兼容指针类型的赋值| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|28|警告:未使用的变量‘s’[-Wunused-variable]| /home/marianpc/Anul_1/SDA/Grafuri1/main.c||在“main”函数中:| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|71|警告:来自不兼容指针类型的赋值| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|76|警告:来自不兼容指针类型的赋值| ||=== 构建完成:0 个错误,5 个警告(0 分钟,0 秒)===|

最主要的是程序现在可以正常运行了。非常感谢你们!真的很有帮助!!

最佳答案

试试这个

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct AdjListNode {
    int dest;
    struct LIST_NODE *next;
} LIST_NODE;

typedef struct AdjList {
    struct LIST_NODE *head;
} ADJACENCY_LIST;

LIST_NODE *create_node(int dest) {
    LIST_NODE *nod;
    if (dest < 0) exit(0);
    nod = (LIST_NODE*)malloc(sizeof(LIST_NODE));
    if (nod == NULL) {
        printf("Problems at memory allocation!");
        exit(0);
    }
    nod->dest = dest;
    nod->next = NULL;
    return (LIST_NODE*)nod;
}

void display_graph(ADJACENCY_LIST *v,int values) {
    int s, i;
    LIST_NODE *nod;

    for (i = 0; i < values; ++i)
    {
        nod = v[i].head;
        printf("Data from node: %d \n", i);
        while (nod != NULL)
        {
            printf("Data : %d \n", nod->dest);
            nod = nod->next;
        }
    }
}

int main()
{
    int n; //number of graph nodes
    int i; //just a counter
    int dest; dest = -1; //it's actually the "name" of the nodes. They  must all be positive so I started negative
    char* c = (char*)(malloc(sizeof(char)));

    ADJACENCY_LIST *t;
    LIST_NODE *last_added;

    printf("The number of nodes of the graph: ");
    scanf("%d", &n);
    t = (ADJACENCY_LIST*)calloc(n,sizeof(ADJACENCY_LIST));

    /* We make a loop for the nodes and each node has a while thru which I make the links */
    for (i = 0; i < n; i++) {
        //c = 'D'; // Initializing
        printf("Specify the links of the node %d with the others:\n", i);
        int contor; contor = 0;
        do {
            LIST_NODE *nod;
            printf("The link with node: ");
            scanf("%d", &dest);
            if (dest >= 0) {
                nod = create_node(dest);
                if (contor == 0)
                {
                    t[i].head = (LIST_NODE*)nod; // just make the first node a head node
                    last_added = nod;
                }
                else
                {
                    last_added->next = nod;
                    last_added = nod;
                }
            }
            //verificam daca vrem sa continuam
            printf("Do you want to link any other node to %d?(D to add, anything else STOP\n)", i);
            fflush(stdin);
            *c = getchar();
            contor++; //increment counter
        } while (*c == 'D');
    }
    display_graph(t,n);
    return 0;
}

关于c - 具有动态链表的图形表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278017/

相关文章:

c - Windows 中的用户空间 APC 队列长度

c - C 中 If 语句中的 Malloc

将 R 代码转换为 C 代码

graph - 在图中,如何找到距离一组节点最近的节点?

C 编程错误,打印链表,在运行时执行代码崩溃

algorithm - 排序双链表的搜索算法

c - 缓冲区溢出/溢出解释?

c - 如何使用 fgets/read/fread 识别 double\n\n

python - matplotlib 缺少样式模块?

带模板的 C++ 链表 - 如何创建 pop()?