I know how to represent an integer graph using adjacency list. However, with a string, it is a bit tricky. I'm trying to create a friends graph. Where the source is at the index of the pointer array. The source's friends are then added using linked list like hashing Now I created a pointer array, unlike the integer program where I use the index as the graph value, I cannot do that with the string list.
The pointer array stores the initial string
Please review it, and any solution would be great
// A C Program to demonstrate adjacency list
// representation of graphs
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <string>
#define maxNode 4
int i;
int z;
typedef struct Node
{
char vertexString[10];
struct Node *next;
}Node;
Node *dest, *tmp;
typedef struct List
{
Node*head;
char vertexS[10];
struct Node *next;
}List;
List*adjlist[maxNode] = { 0 };
void addNode(const char s[10], const char d[10]);
void printList();
int main()
{
int i;
for (i = 0; i < maxNode; i++)
{
adjlist[i] = (List *)malloc(sizeof(List));
adjlist[i]->head = NULL;
}
addNode("Alam", "Shaib");
addNode("ZEE", "PEE");
addNode("ALOO", ",ALOO");
printList();
_getch();
}
void addNode(const char s[10],const char d[10])
{
for (i = 0; i < 4;i++);
{
if (adjlist[i]->vertexS == s)
{
dest = (Node *)malloc(sizeof(Node));
for (int j = 0; j < 10; j++)
{
dest->vertexString[j] = d[j];
}
dest->next = NULL;
tmp = adjlist[i]->head;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = dest;
}
}
for (z = 0; z < 4; z++);
{
if (adjlist[z] == 0)
{
dest = (Node *)malloc(sizeof(Node));
for (int L = 0; L < 10; L++)
{
dest->vertexString[L] = d[L];
}
dest->next = NULL;
tmp = adjlist[z]->head;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = dest;
}
}
}
void printList()
{
int i;
for (i = 0; i < maxNode; i++)
{
Node *p;
p = adjlist[i]->head;
printf("Adjency list for vertex %s\n", adjlist[i]->vertexS);
p = p->next;
while (p)
{
printf("%s",p->vertexString);
p = p->next;
}
printf("\n");
}
}
for (z = 0; z < 4; z++); {This is a loop with an empty body. You have 2 of them in your code.