I am trying to print an array of linked lists. I am having trouble getting it to print. Here is my struct.

typedef struct VERTEXTAG
{
    char c;
    bool isvisited;
    struct EDGETAG* p;
}VERTEX;

typedef struct EDGETAG
{
    VERTEX* v;
    struct EDGETAG* q;
    //cookies rock
    //I like cookies
}EDGE;

Here are my variable declarations

VERTEX v[100];
EDGE *e;
EDGE* temp;
int counter = 0;
int addcounter = 0;
int i = 0;

Here is where I try to create the linked lists. I have an even case and an odd case.

                //even case
                if(counter - i == 1 && flag == 0)
                {
                    vertices[addcounter] = (char)c;
                    //printf("The vertice is %c :\n", vertices[addcounter]);
                    e = (EDGE*) malloc(sizeof(EDGE));
                    v[addcounter].p=e;
                    v[addcounter].c= (char)c; 
                    v[addcounter].isvisited=false;
                    v[addcounter].p=NULL;  
                    addcounter++;
                }

                //odd case
                if(counter - i == 1 && flag == 0)
                {
                    vertices[addcounter] = (char)c;
                    //printf("The vertice is %c :\n", vertices[addcounter]);
                    e = (EDGE*) malloc(sizeof(EDGE));
                    v[addcounter].p=e;
                    v[addcounter].c= (char)c;
                    v[addcounter].isvisited=false;
                    v[addcounter].p=NULL; 
                    (*e).v= &v[addcounter];
                    e->q = NULL;
                    addcounter++;
                }

Here is where I try to print my linked list. For some reason temp is equal to NULL so it is not printing. I know I am correctly passing my variables to each case with vertices array. It prints out correctly. I am not sure if I am correctly creating the linked list of arrays since it will not print out.

temp = v[0].p;
if(temp == NULL)
{
    printf("Temp is Null\n");
}

while(temp != NULL)
{
    printf("While loop");
    printf("%c", (*(*temp).v).c);
    temp = temp->q;
}

printf("The vertice is %s :\n", vertices);

Recommended Answers

All 2 Replies

. For some reason temp is equal to NULL so it is not printing.

It's most likely because you don't create the array correctly.

//odd case
if(counter - i == 1 && flag == 0)

This is the SAME as the condition of even case, so it definitely doesn't work correctly.
Besides it would be more straight forward to use 'if (i % 2 == 0)' or something like that.

I don't see a linked list in the code you posted -- just a couple arrays.

Why do you have a two structures that contain circular references? EDGE has a pointer to VERTEX which has a pointer back to EDGE??? That makes no sense. Maybe you need to rethink the organization of those structures and what they are doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.