Posts

Showing posts from March, 2024

Implementing linked list in C language

#include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct node {     int data; // The data stored in the node     struct node *next; // Pointer to the next node in the list }; struct node* head = NULL; // Initialize the head of the list as NULL // Function to insert nodes at the end of the list void insert(struct node **head) {     int num = 0, i, data;     printf("\nEnter the number of elements to be inserted: ");     scanf("%d", &num); // Get the number of nodes to be inserted     // Loop to insert the nodes     for(i = 0; i<num; i++) {         // Allocate memory for a new node and get the data for it         struct node* newNode = malloc(sizeof(struct node));         struct node *last = *head;         printf("\nEnter the element %d: ", i+1);         scanf("%d", &data); // Get the data for the node         // Initialize the new node with the given data and set its next pointer to