C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Dynamic Memory Allocation in C

Insert node dynamically in linked list

C Program: Insert node dynamically in linked list

Method 1:

C

#include <stdio.h>

#include <stdlib.h>

 

// Define structure for node

struct Node {

    int data;

    struct Node *next;

};

 

int main() {

    struct Node *head = NULL, *temp = NULL, *newNode = NULL;

    int choice = 1;

 

    while (choice) {

        // Dynamically allocate memory for new node

        newNode = (struct Node*) malloc(sizeof(struct Node));

        if (newNode == NULL) {

            printf("Memory allocation failed!\n");

            return 1;

        }

 

        printf("Enter data for node: ");

        scanf("%d", &newNode->data);

        newNode->next = NULL;

 

        // First node creation

        if (head == NULL)

            head = temp = newNode;

        else {

            temp->next = newNode;  // Link new node

            temp = newNode;        // Move temp to last node

        }

 

        printf("Do you want to add another node? (1-Yes / 0-No): ");

        scanf("%d", &choice);

    }

 

    // Display the linked list

    printf("\nLinked List Elements:\n");

    temp = head;

    while (temp != NULL) {

        printf("%d -> ", temp->data);

        temp = temp->next;

    }

    printf("NULL\n");

 

    // Free allocated memory

    temp = head;

    struct Node *nextNode;

    while (temp != NULL) {

        nextNode = temp->next;

        free(temp);

        temp = nextNode;

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter data for node: 10
Do you want to add another node? (1-Yes / 0-No): 1
Enter data for node: 20
Do you want to add another node? (1-Yes / 0-No): 1
Enter data for node: 30
Do you want to add another node? (1-Yes / 0-No): 0

Linked List Elements:
10 -> 20 -> 30 -> NULL

Explanation

Step

Description

1

Define a struct Node containing data and next pointer.

2

Use malloc() to dynamically allocate memory for each new node.

3

Link each new node to the end of the list.

4

Continue until the user enters 0.

5

Display the complete linked list.

6

Free memory at the end to prevent leaks.

 

C Program: Insert node in linked list

Method 2: Linked List Insertion (beginning, end, position)

C

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

// Function to display linked list

void display(struct Node *head) {

    struct Node *temp = head;

    printf("\nLinked List: ");

    while (temp != NULL) {

        printf("%d -> ", temp->data);

        temp = temp->next;

    }

    printf("NULL\n");

}

 

// Function to insert at beginning

struct Node* insertAtBeginning(struct Node *head, int value) {

    struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));

    newNode->data = value;

    newNode->next = head;

    return newNode;

}

 

// Function to insert at end

struct Node* insertAtEnd(struct Node *head, int value) {

    struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));

    newNode->data = value;

    newNode->next = NULL;

 

    if (head == NULL)

        return newNode;

 

    struct Node *temp = head;

    while (temp->next != NULL)

        temp = temp->next;

    temp->next = newNode;

    return head;

}

 

// Function to insert at specific position

struct Node* insertAtPosition(struct Node *head, int value, int pos) {

    struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));

    newNode->data = value;

 

    if (pos == 1) {  // Insert at beginning

        newNode->next = head;

        return newNode;

    }

 

    struct Node *temp = head;

    for (int i = 1; i < pos - 1 && temp != NULL; i++)

        temp = temp->next;

 

    if (temp == NULL) {

        printf("Position out of range!\n");

        free(newNode);

        return head;

    }

 

    newNode->next = temp->next;

    temp->next = newNode;

    return head;

}

 

int main() {

    struct Node *head = NULL;

    int choice, value, pos;

 

    while (1) {

        printf("\n=== Linked List Menu ===\n");

        printf("1. Insert at Beginning\n");

        printf("2. Insert at End\n");

        printf("3. Insert at Position\n");

        printf("4. Display List\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                printf("Enter value to insert: ");

                scanf("%d", &value);

                head = insertAtBeginning(head, value);

                break;

            case 2:

                printf("Enter value to insert: ");

                scanf("%d", &value);

                head = insertAtEnd(head, value);

                break;

            case 3:

                printf("Enter value to insert: ");

                scanf("%d", &value);

                printf("Enter position: ");

                scanf("%d", &pos);

                head = insertAtPosition(head, value, pos);

                break;

            case 4:

                display(head);

                break;

            case 5:

                printf("Exiting...\n");

                exit(0);

            default:

                printf("Invalid choice! Try again.\n");

        }

    }

 

    return 0;

}

Output

 
OUTPUT :
=== Linked List Menu ===
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Display List
5. Exit
Enter your choice: 1
Enter value to insert: 10

Enter your choice: 2
Enter value to insert: 30

Enter your choice: 3
Enter value to insert: 20
Enter position: 2

Enter your choice: 4
Linked List: 10 -> 20 -> 30 -> NULL


Explanation

Operation

Description

Insert at Beginning

New node’s next points to old head, then update head.

Insert at End

Traverse to last node and attach new node.

Insert at Position

Traverse to the given position, adjust links accordingly.

Display Function

Traverses and prints all nodes.