Мне не удается распечатать значение данных первого узла. Может ли кто-нибудь показать мне, что я делаю не так?
#include <stdio.h>
#include <stdlib.h>
typedef struct S_node{
int data;
struct S_node *next;
} node;
int main () {
node* first;
first->data = 7;
first->next = NULL;
printf("\nData: %d", first->data);
}
Я обновил свой код:
#include <stdio.h>
#include <stdlib.h>
typedef struct S_node{
int data;
struct S_node *next;
} node;
void PrintList(node* start);
int main () {
node* first = malloc(sizeof(node));
node* second = malloc(sizeof(node));
node* third = malloc(sizeof(node));
first->data = 7;
first->next = second;
second->data = 6;
second->next = third;
third->data = 5;
third->next = NULL;
PrintList(first);
free(first);
free(second);
free(third);
}
void PrintList(node* start) {
node* currentNode = start;
while(currentNode =! NULL) {
printf("Data: %d", currentNode->data);
currentNode = currentNode->next;
}
}
Я пытаюсь распечатать данные от первого до последнего узла, все еще получая предупреждение «назначение из несовместимого типа указателя» при выполнении
first->next = second;
и
second->next = third;
Когда я запускаю программу, ничего не печатается.