0

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. Also the second print statement in the while loop creates a segmentation when I take it out of the while loop so the program can reach it.

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);

1 Answer 1

1

Your problem is likely here:

v[addcounter].p=e;
v[addcounter].c= (char)c; 
v[addcounter].isvisited=false;
v[addcounter].p=NULL;  

Why are you setting v[x].p to e, then setting it to NULL a few lines after? Later on when you try to access v[0].p, of course it's going to still be NULL.

Sign up to request clarification or add additional context in comments.

7 Comments

I thought when creating a linked list that you want the last link or node to be equal to NULL? Is that correct? How would I do that?
Where exactly do you think you are creating a linked list? All you do is loop through an array of 100 vectors and set the variables in the vector struct. You never go any deeper than that first level. If v->p is supposed to be the next level in your linked list, then v->p->q is the end of your linked list, not v->p.
I thought this creates the linked list?
e = (EDGE*) malloc(sizeof(EDGE)); v[addcounter].p=e; v[addcounter].c= (char)c; v[addcounter].isvisited=false; v[addcounter].p=NULL;
You have some severe design issues here. If you are already building an array of vertices, what's the point of having a linked list as well? All you gain is a redundant storage/reference of your vertices. You need to back up a bit and look at your design before you jump into the code. Maybe draw up a DSD and work from there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.