Я пытался реализовать граф, используя список смежности. Когда инициализация главы как NULL в цикле for в методе CreateGraph, голова реферируется с использованием. вместо '->'. Я не понимаю разницы. Когда мы должны использовать '.' и когда «->». И почему это дает ошибку, когда я использую -> оператор для головы.Когда использовать. и -> в случае указателей
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
**for (i = 0; i < V; ++i)
graph->array[i]->head = NULL;**
return graph;
}
'graph-> массив [я]' не является указатель. – tkausl
Ответы на такие основные вопросы можно найти в любом учебнике. Если у вас его еще нет, [The Definitive C++ Book Guide and List] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - это хорошее место для Начало. –