带有动态链接列表的图形表示

时间:2022-02-17 07:17:55

I start by saying I am quite new to C and right now, I struggle with some very non intuitive mistakes. I've tried for quite a while now to reach at some solution, but I am always reaching a dead end.

我首先说我是C的新手,现在,我在一些非常直观的错误中挣扎。我已经尝试了很长一段时间才达成某种解决方案,但我总是走到尽头。

I am trying to build a couple of functions for inserting and displaying a graph via dynamic linked lists. At compile time everything works just fine, but the elements seem not to be well displayed. Actually, just like in the image below, only the first element of the node is displayed.

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

So the question is what is causing these errors and warnings and what should I do to remove them?

所以问题是导致这些错误和警告的原因以及如何删除它们?

带有动态链接列表的图形表示

If you take a look at the code below, you will see that it has a few warnings(I don't know why they appear - I am using Code Blocks in Ubuntu with the GNU compiler) and also problems at displaying the elements of the graph. The problem lies most likely in the display_graph function, but I can't realize where.

如果你看看下面的代码,你会看到它有一些警告(我不知道它们出现的原因 - 我在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;
}

Any help will be greatly appreciated!

任何帮助将不胜感激!

EDIT: As Christhofe(confirmed the problem) and Abhishek Vasisht pointed out the size of the vector v returned actually the size of the pointer.

编辑:正如Christhofe(确认问题)和Abhishek Vasisht所指出的那样,矢量v的大小实际上是指针的大小。

But, there are still some warnings which I don't know why they still appear...all are

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

||=== Build: Debug in Grafuri1 (compiler: GNU GCC Compiler) ===| /home/marianpc/Anul_1/SDA/Grafuri1/main.c||In function ‘display_graph’:| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|33|warning: assignment from incompatible pointer type| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|38|warning: assignment from incompatible pointer type| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|28|warning: unused variable ‘s’ [-Wunused-variable]| /home/marianpc/Anul_1/SDA/Grafuri1/main.c||In function ‘main’:| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|71|warning: assignment from incompatible pointer type| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|76|warning: assignment from incompatible pointer type| ||=== Build finished: 0 error(s), 5 warning(s) (0 minute(s), 0 second(s)) ===|

|| === Build:Grafuri1中的Debug(编译器:GNU GCC编译器)=== | /home/marianpc/Anul_1/SDA/Grafuri1/main.c||在函数'display_graph'中: /home/marianpc/Anul_1/SDA/Grafuri1/main.c|33 |warning:从不兼容的指针类型分配/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 |warning:从不兼容的指针类型中分配/home/marianpc/Anul_1/SDA/Grafuri1/main.c|76 |警告:从不兼容的指针类型中分配|| ===构建完成:0个错误,5个警告(0分钟,0秒(秒))=== |

The main thing is the program is functional now. Thanks a lot guys! Really helpful!!

最重要的是该程序现在正在运行。非常感谢!真有帮助!!

2 个解决方案

#1


2  

Try this

尝试这个

#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;
}

#2


0  

the following code compiles cleanly, works.

以下代码编译干净,有效。

It does not have the capability of having a list of nodes for each entry in the first level list of pointers.

它没有能力为第一级指针列表中的每个条目提供节点列表。

You can easily add that feature.

您可以轻松添加该功能。

You also need to add the feature of passing each malloc'd node to free() especially when an error has occurred

您还需要添加将每个malloc节点传递给free()的功能,尤其是在发生错误时

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

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


// declare list of pointers to nodes
static struct AdjListNode **head = NULL;


struct AdjListNode *create_node(int dest)
{
    struct AdjListNode *nod;

    nod = malloc(sizeof(struct AdjListNode));
    if(nod==NULL)
    {
        perror(" malloc failed" );
        printf("Problems at memory allocation!");
        exit(-1);
    }

    // implied else, malloc successful

    nod->dest = dest;
    nod->next = NULL;
    return nod;
} // end function: create_node



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


    printf("The number of nodes of the graph: ");
    if( 1 != scanf("%d", &n) )
    { // then scanf failed
        perror( "scanf for number of nodes failed" );
        exit( -1 );
    }

    // implied else, scanf successful

    // set ptr to list of node pointers
    head = malloc(n*sizeof(struct AdjList*));
    if( NULL == head )
    { // then malloc failed
        perror( "malloc failed for list of pointers to nodes" );
        exit( -1 );
    }

    // implied else, malloc successful

    // initialize list of pointers (makes for easier cleanup, especially when a failure occurs
    memset( head, 0x00, n*sizeof(struct AdjList*) );

    /* 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++)
    {
        printf("Enter Dest value for %d of %d:", i, n);

        if( 1 != scanf("%d", &dest) ) // note %d will skip over leading white space like newlines
        { // then scanf failed
            perror( "scanf for dest value failed" );
            exit(-1);
        }

        // implied else, scanf successful

        if(dest>=0)
        {
             head[i] = create_node(dest);
        }
        else
        {
            printf( "Dest value must be >= 0\n" );
        }

        //verificam daca vrem sa continuam

        // inchidem lista
    } // end for

    return 0;
} // end function: main

#1


2  

Try this

尝试这个

#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;
}

#2


0  

the following code compiles cleanly, works.

以下代码编译干净,有效。

It does not have the capability of having a list of nodes for each entry in the first level list of pointers.

它没有能力为第一级指针列表中的每个条目提供节点列表。

You can easily add that feature.

您可以轻松添加该功能。

You also need to add the feature of passing each malloc'd node to free() especially when an error has occurred

您还需要添加将每个malloc节点传递给free()的功能,尤其是在发生错误时

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

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


// declare list of pointers to nodes
static struct AdjListNode **head = NULL;


struct AdjListNode *create_node(int dest)
{
    struct AdjListNode *nod;

    nod = malloc(sizeof(struct AdjListNode));
    if(nod==NULL)
    {
        perror(" malloc failed" );
        printf("Problems at memory allocation!");
        exit(-1);
    }

    // implied else, malloc successful

    nod->dest = dest;
    nod->next = NULL;
    return nod;
} // end function: create_node



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


    printf("The number of nodes of the graph: ");
    if( 1 != scanf("%d", &n) )
    { // then scanf failed
        perror( "scanf for number of nodes failed" );
        exit( -1 );
    }

    // implied else, scanf successful

    // set ptr to list of node pointers
    head = malloc(n*sizeof(struct AdjList*));
    if( NULL == head )
    { // then malloc failed
        perror( "malloc failed for list of pointers to nodes" );
        exit( -1 );
    }

    // implied else, malloc successful

    // initialize list of pointers (makes for easier cleanup, especially when a failure occurs
    memset( head, 0x00, n*sizeof(struct AdjList*) );

    /* 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++)
    {
        printf("Enter Dest value for %d of %d:", i, n);

        if( 1 != scanf("%d", &dest) ) // note %d will skip over leading white space like newlines
        { // then scanf failed
            perror( "scanf for dest value failed" );
            exit(-1);
        }

        // implied else, scanf successful

        if(dest>=0)
        {
             head[i] = create_node(dest);
        }
        else
        {
            printf( "Dest value must be >= 0\n" );
        }

        //verificam daca vrem sa continuam

        // inchidem lista
    } // end for

    return 0;
} // end function: main