So I wanted to create a doubly linked circular list with the node structure:
typedef struct Node { //node structure
int value;
struct Node *next;
struct Node *previous;
} node;
At first, I had created my list as follows:
//allocate memory for the nodes
node *first = malloc(sizeof(*first));
node *second = malloc(sizeof(*second));
node *third = malloc(sizeof(*third));
node *fourth = malloc(sizeof(*fourth));
node *fifth = malloc(sizeof(*fifth));
{ //define the nodes and link them together.(circular linked list)
first->value = 1;
first->next = second;
first->previous = fifth;
second->value = 2;
second->next = third;
second->previous = first;
third->value = 3;
third->next = fourth;
third->previous = second;
fourth->value = 4;
fourth->next = fifth;
fourth->previous = third;
fifth->value = 5;
fifth->next = first;
fifth->previous = fourth;
}
But then I realized that I do not know if the list should consist of 5 elements and wanted to ask the user about the size of the list.
The problem is that when I tried to implement a function called createList()
and pass the list size as a parameter, I have no clue of how I should create a such list. I thought of creating a temporary pointer first with ->next
and ->previous
values as NULL
but then how would I iterate through the input n
and create new nodes if the next
node is NULL
?
Thanks