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.