## DEV Community

Maniruzzaman Akash

Posted on

When a program needs to smoothly handle dynamic operations like adding, updating, and removing data-linked lists with a good solution at that time. It provides a flexible way to manage information, making it easier to insert, update, or delete data elements in our program.

## Structure of a Linked list

``````struct Node {
int data;
struct Node* next;
};
``````

``````// Create a new node
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = newData;
// Update pointers
``````

## Insert in the middle

``````// Create a new node
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = newData;
// Update pointers
newNode->next = temp->next;
temp->next = newNode;
``````

## Insert in the end

``````struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = NULL;
// Update pointers
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
``````

``````struct Node* current = head;
while (current != NULL) {
// Access data at the current node
printf("%d -> ", current->data);
// Move to the next node
current = current->next;
}
``````

## Real life example of Linked list

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

// Node structure for the linked list
char description[100];
};

// Allocate memory for the new task

// If the list is empty, make the new task the head
} else {
// Otherwise, add the new task to the end of the list
while (current->next != NULL) {
current = current->next;
}
}

}

// Function to traverse and display the to-do list
printf("To-Do List is empty.\n");
return;
}

// Initialize a pointer to the head of the list

while (current != NULL) {

// Move to the next task
current = current->next;
}
}

// Function to remove a task from the to-do list
// If the list is empty, nothing to delete
printf("To-Do List is empty. Cannot delete.\n");
}

}

// Search for the task to be deleted

while (current != NULL && current->taskID != id) {
previous = current;
current = current->next;
}

if (current == NULL) {
}

// Remove the task from the list
previous->next = current->next;
free(current);

}

int main() {
// Initialize an empty to-do list

toDoList = insertTask(toDoList, 1, "Complete assignment");
toDoList = insertTask(toDoList, 3, "Attend meeting");

// Display the to-do list
printf("Initial To-Do List:\n");
traverseToDoList(toDoList);

// Display the updated to-do list
printf("\nUpdated To-Do List after deletion:\n");
traverseToDoList(toDoList);

return 0;
}
``````

This is just the beginning of the Linked list.

If you need more examples and detailed learning, deleting, and many more Please go through this article with many practical examples and learning - https://devsenv.com/tutorials/linked-list