0

I am constructing a adjacency list that I am using to make a directed graph. I read two characters and then make a vertex out of each of them unless a vertex with that char has already been made. I am unfamiliar with structures so this is tripping me up. The values aren't being assigned and I am ending up with an empty list. I am assuming that it is something that has to do with pointers that I am overlooking but I can't seem to find the problem. I hope that someone can help. I will include my code and a sample output.

my.h

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> /* Forward declaration */ struct EDGETAG; typedef struct { char c; bool isVisited; struct EDGETAG* p; } VERTEX; typedef struct EDGETAG { VERTEX* v; struct EDGETAG* q; } EDGE; int main (int argc, char* argv[]); int adjListSearch (char a, int size, VERTEX* adjList); 

adjListSearch.c

#include "my.h" int adjListSearch (char a, int size, VERTEX* adjList) { int i; if(size == 0) { printf("empty\n"); return 1; } for(i = 0; i<size; i++) { if( adjList[i].c = a) { return 0; printf("found\n"); } } printf("notfound\n"); return 1; } 

main.c

#include "my.h" int main (int argc, char* argv[]) { int y = 0; int x = 0; VERTEX *adjList; adjList = (VERTEX*)calloc(26, sizeof(VERTEX)); //adjList = malloc(sizeof(VERTEX)*26); FILE* p; char *fp; char a; char b; int check1 = 0; int check2 = 0; int size = 0; int searchsuccess = 0; int i; //Statements p = fopen( argv[1], "r"); while(fscanf(p," %c %c", &a, &b)!= EOF) { printf("a: %c b: %c\n",a,b); check1 = adjListSearch(a,size,adjList); if(check1==1) { adjList[size].c = a; size = size +1; } //printf("%c\n", adjList[size].c); check2 = adjListSearch(b,size,adjList); if(check2==1) { adjList[size].c = b; size = size +1; } } //End While printf("Size: %d", size); for(i=0;i<size;i++) { printf("%c\n", adjList[size].c); } free(p); return 0; } //End main 

Sample Output

a: A b: B a: B b: C a: E b: X a: C b: D Size: 1 

As you can see none of the characters are printed as being inside a structure.

EDIT 1:

I would want the expected output to look like a: A b: B a: B b: C a: E b: X a: C b: D Size: 6 A B C E X D 

Edit 2 Still lost on this if anyone can help.

2 Answers 2

2

There were a variety of problems.

  1. You should use fclose(p); and not free(p);.
  2. You should print printf("%c\n", adjList[i].c); instead of printf("%c\n", adjList[size].c);.
  3. As brokenfoot pointed out in his answer, you need to compare a instead of assign it in adjListSearch().
  4. There were multiple unused variables (the compilation options I use complain about them).
  5. There is no need to declare main() (though it doesn't do any harm to the program).
  6. You don't check that there is an argv[1] to use.
  7. You don't check that you opened the file.
  8. You should really use while (fscanf(p, " %c %c", &a, &b) == 2), though it would only matter in this program if there were an odd number of characters to process. At least you were testing fscanf() which is more than many questions we see do.

In the code below, I printed more informative status messages.

main.c

#include "my.h" int main(int argc, char *argv[]) { VERTEX *adjList; adjList = (VERTEX *)calloc(26, sizeof(VERTEX)); FILE *p; char a; char b; int check1 = 0; int check2 = 0; int size = 0; int i; if (argc != 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); return 1; } if ((p = fopen(argv[1], "r")) == 0) { fprintf(stderr, "Failed to open file %s for reading\n", argv[1]); return 1; } while (fscanf(p, " %c %c", &a, &b) == 2) { printf("a: %c b: %c\n", a, b); check1 = adjListSearch(a, size, adjList); if (check1 == 1) { printf("Adding a = %c\n", a); adjList[size++].c = a; } check2 = adjListSearch(b, size, adjList); if (check2 == 1) { printf("Adding b = %c\n", b); adjList[size++].c = b; } } printf("Size: %d\n", size); for (i = 0; i < size; i++) { printf("%c\n", adjList[i].c); } fclose(p); return 0; } 

adjListSearch.c

#include "my.h" int adjListSearch(char a, int size, VERTEX *adjList) { int i; if (size == 0) { printf("empty\n"); return 1; } for (i = 0; i < size; i++) { if (adjList[i].c == a) { printf("found %c\n", a); return 0; } } printf("not found %c\n", a); return 1; } 

Data

A B B C E X C D 

Sample output

a: A b: B empty Adding a = A not found B Adding b = B a: B b: C found B not found C Adding b = C a: E b: X not found E Adding a = E not found X Adding b = X a: C b: D found C not found D Adding b = D Size: 6 A B C E X D 
Sign up to request clarification or add additional context in comments.

1 Comment

This was a very informative answer and helped me very much. I appreciate how well you presented the information.
1

In adjListSearch(), change

if( adjList[i].c = a) 

to

if( adjList[i].c == a) 

3 Comments

That helped but I still can't seem to print out the char part of the VERTEX structure to see if there is actually anything there.
@Fluxxxy: Can you post the expected output as well ?
I have added it to my original post at the bottom.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.