Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get these syntax errors?

I have the task of writing a LinkedList, without using loops. Anyways, this is my code:

The structs in the header file:

typedef struct Node
{
    Node* last;
    Node* next;
    int val;
}Node;

typedef struct LinkedList
{
    Node* first;
    Node* last;
    int sum;
    int length;
}LinkedList;

In the errors below it says that Node and LinkedList are not known and I don’t understand why.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Node* new_node(int val)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->last = NULL;
    node->val = val;
    node->next = NULL;
    return node;
}

LinkedList* new_list(int val)
{
    LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
    Node* node = (Node*)new_node(val);
    list->first = node;
    list->last = node;
    list->length = 1;
    list->sum = val;
    return list;
}

void delete_list(LinkedList* list)
{
    Node* node;
    Node* next;

    node = list->first;
    while (node != NULL)
    {
        next = node->next;
        free(node);
        node = next;
    }
    free(list);
}

void set_next(LinkedList* list, Node* node)
{
    list->last->next = node;
    list->last = node;
    list->length++;
    list->sum += node->val;
}

void delete_node(LinkedList* list,  Node* node)
{
    node->last->next = node->next;
    node->next->last = node->last;
    list->length--;
    list->sum -= node->val;
    free(node);
}

int size(LinkedList* list)
{
    return list->length;
}

Node* get_first(LinkedList* list)
{
    return list->first;
}

Node* get_last(LinkedList* list)
{
    return list->last;
}

Node* get_next(Node* node)
{
    return node->next;
}

Node* get_last(Node* node)
{
    return node->last;
}

int get_val(Node* node)
{
    return node->val;
}

int get_avg(LinkedList* list)
{
    return list->sum / list->length;
}

My problem is that I get tons of errors about syntax and I don’t understand why:
enter image description here
enter image description here
enter image description here

Can someone tell me why I get these errors?

>Solution :

There are two errors in this code that I can spot right now.
The first one has already been stated by Weather Vane:

In your declaration of Node, you are referencing itself, which is fine, but the typedef is yet incomplete at this point.

That means, you need to put struct in front of the name to identify the structure.

typedef struct Node
{
    struct Node* last;
    struct Node* next;
    int val;
} Node;

The second issue is, that you have two instances of get_last with different parameters.
C does not support overloading (whereas C++ does). As such, you will need to pick unique names, which best express what you are trying to get, such as: getLastNode and getLastElement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading