I am trying to create doubly linked list with a function to add a node at the end of the list. I have two typedef structs: one for Node and one for the doubly linked list.
In insertion_last() I am getting an error when trying to set list->tail->next to new_node. The error is "pointer to incomplete class type is not allowed" and is referring to DoublyLinkedList. I suppose I have done something wrong in making the structs, but cannot quite figure out why it is not working.
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeStruct
{
int data;
struct Node *next;
struct Node *prev;
} Node;
typedef struct DoublyLinkedListStruct
{
int size;
struct Node *head;
struct Node *tail;
} DoublyLinkedList;
Node* newNode(int data, Node* next, Node* prev)
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = next;;
new_node->prev = prev;
return new_node;
};
void insertion_beginning(DoublyLinkedList* list, int new_data)
{
Node* new_node = newNode(new_data, list->head, NULL);
list->head = new_node;
if (!list->tail)
{
list->tail = new_node;
}
list->size++;
}
void insertion_last(DoublyLinkedList* list, int new_data)
{
Node *new_node = newNode(new_data, NULL, list->tail);
if(list->tail)
{
list->tail->next = new_node;
}
else
{
list->head = new_node;
}
list->tail = new_node;
list->size++;
}
You define tail
and several others fields to have type struct Node *
. You don't have any such struct defined.
You want to change those to use struct NodeStruct *
instead.