Creating a Dynamic Linked List in C: User Input for Node Creation.

Introduction:

Welcome to our tutorial on creating a dynamic linked list of bank accounts in C. In this tutorial, we’ll guide you through a step-by-step process to create a linked list that can hold an unlimited number of bank accounts. We’ll ask for user input to determine how many nodes (bank accounts) they want to create and then prompt them to input the bank account number and balance for each account.

Linked lists are fundamental data structures used in computer science for managing collections of data. They are particularly useful when the number of elements is not known in advance or when frequent insertions and deletions are expected.

By the end of this tutorial, you’ll have a solid understanding of how linked lists work in C and how to implement them in real-world scenarios. Let’s get started!

Here’s The Code I have written in the video

#include <stdio.h>
#include <stdlib.h>

// Node structure for the linked list
struct node {
    int account_number;
    float balance;
    struct node *link;
};

int main() {
    int num_nodes, i;
    printf("Enter the number of nodes you want to create: ");
    scanf("%d", &num_nodes);

    struct node *head = NULL;
    struct node *temp = NULL;

    // Create nodes using a for loop
    for (i = 0; i < num_nodes; i++) {
        // Allocate memory for a new node
        struct node *new_node = (struct node *)malloc(sizeof(struct node));

        if (new_node == NULL) {
            printf("Memory allocation failed. Exiting...");
            exit(1);
        }

        // Input bank account number
        printf("Enter bank account number for node %d: ", i+1);
        scanf("%d", &new_node->account_number);

        // Input balance
        printf("Enter balance for account number %d: ", new_node->account_number);
        scanf("%f", &new_node->balance);

        // Link the new node to the list
        new_node->link = NULL;

        if (head == NULL) {
            // If list is empty, make new node as head
            head = new_node;
        } else {
            // Traverse to the end of the list
            temp = head;
            while (temp->link != NULL) {
                temp = temp->link;
            }
            // Link the new node to the end of the list
            temp->link = new_node;
        }
    }

    // Print the linked list
    printf("\nBank Accounts:\n");
    temp = head;
    while (temp != NULL) {
        printf("Account Number: %d, Balance: %.2f\n", temp->account_number, temp->balance);
        temp = temp->link;
    }

    // Free allocated memory
    temp = head;
    while (temp != NULL) {
        struct node *next_node = temp->link;
        free(temp);
        temp = next_node;
    }

    return 0;
}

Let’s break down the code step by step:

#include <stdio.h>
#include <stdlib.h>

These lines include necessary header files. <stdio.h> is for input and output operations like printf, and <stdlib.h> is for functions like malloc, free, and exit.

struct node {
    int account_number;
    float balance;
    struct node *link;
};

This defines a structure named node which represents a single node in the linked list. Each node contains an integer account_number representing the bank account number, a floating-point balance, and a pointer link which points to the next node in the linked list.

int main() {
    int num_nodes, i;
    printf("Enter the number of nodes you want to create: ");
    scanf("%d", &num_nodes);

This is the main() function where the program execution begins. It prompts the user to enter the number of nodes they want to create and stores the value in the variable num_nodes.

    struct node *head = NULL;
    struct node *temp = NULL;

These lines declare pointers to struct node. head will point to the first node of the linked list, and temp will be used for temporary node operations.

    for (i = 0; i < num_nodes; i++) {

This for loop iterates num_nodes times, creating nodes according to the user’s input.

        struct node *new_node = (struct node *)malloc(sizeof(struct node));

This line dynamically allocates memory for a new node using malloc. The size of memory allocated is determined by sizeof(struct node).

        if (new_node == NULL) {
            printf("Memory allocation failed. Exiting...");
            exit(1);
        }

This checks if memory allocation was successful. If malloc returns NULL, it indicates failure, and the program prints an error message and exits.

        printf("Enter bank account number for node %d: ", i+1);
        scanf("%d", &new_node->account_number);

This prompts the user to input the bank account number for the current node and stores it in new_node->account_number.

        printf("Enter balance for account number %d: ", new_node->account_number);
        scanf("%f", &new_node->balance);

Similarly, this prompts the user to input the balance for the current bank account and stores it in new_node->balance.

        new_node->link = NULL;

This line initializes the link pointer of the new node to NULL, indicating that it’s currently the last node in the list.

        if (head == NULL) {
            head = new_node;
        } else {
            temp = head;
            while (temp->link != NULL) {
                temp = temp->link;
            }
            temp->link = new_node;
        }

This section adds the newly created node to the linked list. If head is NULL, it means the list is empty, so head is set to point to the new node. Otherwise, it traverses the list to find the last node (temp->link == NULL) and links the new node to it.

    printf("\nBank Accounts:\n");
    temp = head;
    while (temp != NULL) {
        printf("Account Number: %d, Balance: %.2f\n", temp->account_number, temp->balance);
        temp = temp->link;
    }

This part prints the bank accounts stored in the linked list. It traverses the list from head to the last node (temp->link == NULL) and prints the account number and balance of each node.

    temp = head;
    while (temp != NULL) {
        struct node *next_node = temp->link;
        free(temp);
        temp = next_node;
    }

Finally, this section frees the allocated memory for each node in the linked list to prevent memory leaks. It iterates through the list, freeing each node’s memory, and moves to the next node until all nodes are freed.

    return 0;
}

This statement indicates successful termination of the program. It returns 0 to the operating system to signify that the program executed without any errors.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top