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.
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:
Can someone tell me why I get these errors?
CodePudding user response:
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
.
CodePudding user response:
The type used by a typedef
isn't visible until the compiler reaches the final semi colon here: }Node;
. Until that point, the type isn't known.
However, when using a struct tag such as done here struct Node
, you can refer to that tag from within the struct definition itself. Simply add the struct
keyword:
typedef struct Node
{
struct Node* last;
struct Node* next;
int val;
}Node;
But from there on you'll be able to use Node
without struct
, because it is now a known typedef.
CodePudding user response:
- type
Node
is not known at this point. It has to be:
typedef struct Node
{
struct Node* last;
struct Node* next;
int val;
}Node;
- You have duplicate
get_last
function definition. Delete one.
And it will compile OK