Linked list learning in C programming with Practical examples

Maniruzzaman-Akash
3 min readFeb 23, 2024

--

Why Linked list

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

Photo by Kelly Sikkema on Unsplash

Components of Linked List

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

Insert in Linked List

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

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.
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}

Linked List Traverse

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 a Linked list

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

// Node structure for the linked list
struct Task {
int taskID;
char description[100];
struct Task* next;
};

// Function to add a new task to the to-do list
struct Task* insertTask(struct Task* head, int id, const char* desc) {
// Allocate memory for the new task
struct Task* newTask = (struct Task*)malloc(sizeof(struct Task));
// Set the task details
newTask->taskID = id;
strncpy(newTask->description, desc, sizeof(newTask->description));
newTask->next = NULL;
// If the list is empty, make the new task the head
if (head == NULL) {
head = newTask;
} else {
// Otherwise, add the new task to the end of the list
struct Task* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newTask;
}
return head;
}


// Function to traverse and display the to-do list
void traverseToDoList(struct Task* head) {
if (head == NULL) {
printf("To-Do List is empty.\n");
return;
}
// Initialize a pointer to the head of the list
struct Task* current = head;
// Iterate through the tasks
while (current != NULL) {
// Display task details
printf("Task %d: %s\n", current->taskID, current->description);
// Move to the next task
current = current->next;
}
}


// Function to remove a task from the to-do list
struct Task* deleteTask(struct Task* head, int id) {
// If the list is empty, nothing to delete
if (head == NULL) {
printf("To-Do List is empty. Cannot delete.\n");
return head;
}

// If the task to be deleted is the head
if (head->taskID == id) {
struct Task* newHead = head->next;
free(head);
return newHead;
}

// Search for the task to be deleted
struct Task* current = head;
struct Task* previous = NULL;
while (current != NULL && current->taskID != id) {
previous = current;
current = current->next;
}

// If the task is not found
if (current == NULL) {
printf("Task %d not found. Cannot delete.\n", id);
return head;
}

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

int main() {
// Initialize an empty to-do list
struct Task* toDoList = NULL;

// Insert tasks
toDoList = insertTask(toDoList, 1, "Complete assignment");
toDoList = insertTask(toDoList, 2, "Buy groceries");
toDoList = insertTask(toDoList, 3, "Attend meeting");

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

// Delete a task
toDoList = deleteTask(toDoList, 2);

// 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

--

--

Maniruzzaman-Akash
Maniruzzaman-Akash

No responses yet