Huzo
Huzo

Reputation: 1692

Creating a linked list based on the number of elements requested by the user in C

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

Upvotes: 2

Views: 595

Answers (1)

vicky
vicky

Reputation: 189

you have to create new node n(input size) times and append to the tail of the linked list, you can use any looping construct to create new nodes, below is a sample code. Edited: condition for circular doubly linked list, earlier i'vent notice that you have asked for circular dll, i thought it was simply dll.

#include<stdio.h>
#include<stdlib.h>
typedef struct Node { //node structure
    int value;
    struct Node *next;
    struct Node *previous;
} node;

void createList(struct Node ** head, int size) {

    // temp variable to iterate list
    struct Node *temp = *head;
    int val;
    while (size--) {
        scanf("%d", &val);


        if (temp == NULL) {
            // create head node for empty linked list;
            struct Node *node = (struct Node*)malloc(sizeof( Node));
            node->previous = NULL;
            node->next = NULL;
            node->value = val;
            *head = node;
            temp = node;
        } else {
            // if head node exists append the numbers at the end of the linkedlist
            struct Node *node = (struct Node*)malloc(sizeof(struct Node));
            node->previous = temp;
            node->next = NULL;
            node->value = val;
            temp->next = node;
            temp = temp->next;
        }
    }

    // for circular linked list
    temp->next = *head;
    (*head)->previous = temp;
}

void printList (struct Node *head) {
    node *temp = head;
    while (head->next != temp) {
        printf("%d\n", head->value);
        head = head->next;
    }
    // print last node
    printf("%d\n", head->value);
}
int main () {

    struct Node *head = NULL;
    int size;
    scanf("%d", &size);
    createList(&head, size);
    printList(head);
    return 0;
}

Upvotes: 1

Related Questions