Inserting Elements at the Beginning of a Linked list

This code demonstrates the implementation of a dynamic linked list in C, focusing on adding elements at the beginning of the list. The program initializes three nodes with data values, connects them to form a linked list, and then prompts the user if they want to add another element at the beginning. If the user chooses to do so, a new node with a data value of 50 is inserted at the beginning of the list. The process continues until the user decides to stop. Finally, the program traverses the linked list and prints out the data values of all nodes. This example serves as a simple illustration of dynamically managing memory and manipulating linked lists in C.

Here’s the code Shown in the video

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

// Define a structure for the linked list node
struct student {
    int data;
    struct student *link;
};

int main() {
    // Declare pointers for the nodes and traversal
    struct student *s1, *s2, *s3, *traverse, *new;

    // Dynamically allocate memory for the nodes
    s1 = malloc(sizeof(struct student));
    s2 = malloc(sizeof(struct student));
    s3 = malloc(sizeof(struct student));
    traverse = malloc(sizeof(struct student));
    new = malloc(sizeof(struct student));

    // Assign data values to the nodes
    s1->data = 10;
    s2->data = 20;
    s3->data = 30;

    // Connect the nodes to form a linked list
    s1->link = s2;
    s2->link = s3;
    s3->link = NULL;

    printf("Do you want to add another element at the beginning? (1 for yes, 0 for no): ");
    int input;
    scanf("%d", &input);

    // Add elements at the beginning based on user input
    while (input != 0) {
        // Dynamically allocate memory for the new node
        new = malloc(sizeof(struct student));
        
        // Assign data value to the new node
        new->data = 50;
        
        // Insert the new node at the beginning of the list
        new->link = s1;
        s1 = new; 

        // Prompt the user again
        printf("Do you want to add another element at the beginning? (1 for yes, 0 for no): ");
        scanf("%d", &input);
    }

    // Initialize traversal pointer to the head of the list
    traverse = s1;

    // Traverse the list and print the data values of nodes
    while (traverse != NULL) {
        printf("%d\n", traverse->data);
        traverse = traverse->link;
    }
    return 0;
}

Certainly! Let’s break down the code step by step, focusing on the logic and operations performed within the main function:

// Declare pointers for the nodes and traversal
struct student *s1, *s2, *s3, *traverse, *new;

// Dynamically allocate memory for the nodes
s1 = malloc(sizeof(struct student));
s2 = malloc(sizeof(struct student));
s3 = malloc(sizeof(struct student));
traverse = malloc(sizeof(struct student));
new = malloc(sizeof(struct student));
  • Here, pointers for nodes (s1, s2, s3, traverse, new) are declared to manage the linked list.
  • Memory is dynamically allocated for each node using malloc to store struct student objects.
// Assign data values to the nodes
s1->data = 10;
s2->data = 20;
s3->data = 30;
  • Data values are assigned to the data member of each node (s1, s2, s3).
// Connect the nodes to form a linked list
s1->link = s2;
s2->link = s3;
s3->link = NULL;
  • The nodes (s1, s2, s3) are linked together to form a linked list.
  • Each node’s link member is assigned the address of the next node.
// Prompt the user if they want to add another element at the beginning
printf("Do you want to add another element at the beginning? (1 for yes, 0 for no): ");
  • The user is prompted to indicate whether they want to add another element at the beginning of the linked list.
// Read user input
int input;
scanf("%d", &input);
  • User input is read to determine whether to proceed with adding elements to the beginning of the linked list.
// Add elements at the beginning based on user input
while (input != 0) {
    // Dynamically allocate memory for the new node
    new = malloc(sizeof(struct student));

    // Assign data value to the new node
    new->data = 50;

    // Insert the new node at the beginning of the list
    new->link = s1;
    s1 = new; 

    // Prompt the user again
    printf("Do you want to add another element at the beginning? (1 for yes, 0 for no): ");
    scanf("%d", &input);
}
  • Inside a loop, a new node is dynamically allocated and data (50 in this case) is assigned to it.
  • The new node is inserted at the beginning of the linked list by updating its link member to point to the current head of the list (s1), and then updating s1 to point to the new node.
  • The loop continues as long as the user indicates they want to add more elements.
// Initialize traversal pointer to the head of the list
traverse = s1;

// Traverse the list and print the data values of nodes
while (traverse != NULL) {
    printf("%d\n", traverse->data);
    traverse = traverse->link;
}
  • The traversal pointer (traverse) is initialized to the head of the list (s1).
  • The list is traversed, printing the data values of each node as traversal progresses.
  • The loop continues until the end of the list is reached (traverse becomes NULL).

2 thoughts on “Inserting Elements at the Beginning of a Linked list”

  1. #include
    #include

    // Define the structure for a node
    struct Node {
    int data;
    struct Node* next;
    };

    int main() {
    // Manually creating the first three nodes
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    struct Node* second = (struct Node*)malloc(sizeof(struct Node));
    struct Node* third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 10;
    head->next = second;

    second->data = 20;
    second->next = third;

    third->data = 30;
    third->next = NULL;

    struct Node* new_node;
    struct Node* temp;

    // Insert at the end
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 40;
    new_node->next = NULL;

    temp = head;
    while (temp->next != NULL) {
    temp = temp->next;
    }
    temp->next = new_node;

    // Insert at the beginning
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 5;
    new_node->next = head;
    head = new_node;

    // Insert at a specific position (let’s say after the second node)
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 25;

    temp = head;
    int position = 3; // Insert after the second node (position 3 considering 1-based index)
    for (int i = 1; i next;
    }

    if (temp != NULL) {
    new_node->next = temp->next;
    temp->next = new_node;
    } else {
    printf(“The specified position is beyond the list length.\n”);
    }

    // Printing the linked list
    temp = head;
    printf(“Linked list: “);
    while (temp != NULL) {
    printf(“%d -> “, temp->data);
    temp = temp->next;
    }
    printf(“NULL\n”);

    return 0;
    }

  2. #include
    #include

    // Define the structure for a node
    struct Node {
    int data;
    struct Node* next;
    };

    int main() {
    // Manually creating the first three nodes
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    struct Node* second = (struct Node*)malloc(sizeof(struct Node));
    struct Node* third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 10;
    head->next = second;

    second->data = 20;
    second->next = third;

    third->data = 30;
    third->next = NULL;

    struct Node* new_node;
    struct Node* temp;

    // Insert at the end
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 40;
    new_node->next = NULL;

    temp = head;
    while (temp->next != NULL) {
    temp = temp->next;
    }
    temp->next = new_node;

    // Insert at the beginning
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 5;
    new_node->next = head;
    head = new_node;

    // Insert at a specific position (let’s say after the second node)
    new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = 25;

    temp = head;
    int position = 3; // Insert after the second node (position 3 considering 1-based index)
    for (int i = 1; i next;
    }

    if (temp != NULL) {
    new_node->next = temp->next;
    temp->next = new_node;
    } else {
    printf(“The specified position is beyond the list length.\n”);
    }

    // Printing the linked list before deletion
    temp = head;
    printf(“Linked list before deletion: “);
    while (temp != NULL) {
    printf(“%d -> “, temp->data);
    temp = temp->next;
    }
    printf(“NULL\n”);

    // Delete the first node
    if (head != NULL) {
    temp = head;
    head = head->next;
    free(temp);
    }

    // Delete the last node
    if (head != NULL) {
    temp = head;
    if (temp->next == NULL) { // If there’s only one node
    free(temp);
    head = NULL;
    } else {
    while (temp->next->next != NULL) {
    temp = temp->next;
    }
    free(temp->next);
    temp->next = NULL;
    }
    }

    // Delete the node at a specific position (let’s say position 2)
    position = 2;
    temp = head;
    if (temp != NULL && position == 1) {
    head = temp->next;
    free(temp);
    } else {
    for (int i = 1; temp != NULL && i next;
    }

    if (temp == NULL || temp->next == NULL) {
    printf(“The specified position is beyond the list length.\n”);
    } else {
    struct Node* next_node = temp->next->next;
    free(temp->next);
    temp->next = next_node;
    }
    }

    // Printing the linked list after deletion
    temp = head;
    printf(“Linked list after deletion: “);
    while (temp != NULL) {
    printf(“%d -> “, temp->data);
    temp = temp->next;
    }
    printf(“NULL\n”);

    return 0;
    }

Leave a Comment

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

Scroll to Top