DSA Project – To Do List
Learn DSA & Get Ready for MAANG Companies Start Now!!
Program 1
// TO DO List Project Based on Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Task
{
int id;
char description[100];
int completed;
struct Task* next;
} Task;
Task* head = NULL; // First Node
int taskCount = 0;
// Function to create a new task
Task* createTask(char desc[])
{
Task* newTask = (Task*)malloc(sizeof(Task));
newTask->id = ++taskCount;
strcpy(newTask->description, desc);
newTask->completed = 0;
newTask->next = NULL;
return newTask;
}
// Function to add a task
void addTask()
{
char desc[100];
getchar(); // clear buffer
printf("Enter task description: ");
fgets(desc, sizeof(desc), stdin);
desc[strcspn(desc, "\n")] = '\0'; // remove newline
Task* newTask = createTask(desc);
if (head == NULL)
{
head = newTask;
} else
{
Task* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newTask;
}
printf("Task added successfully.\n");
}
// Function to display all tasks
void displayTasks()
{
if (head == NULL)
{
printf("To-Do List is empty.\n");
return;
}
Task* temp = head;
printf("\nTo-Do List:\n");
while (temp != NULL)
{
printf("ID: %d | [%s] %s\n", temp->id,temp->completed ? "X" : "*", temp->description);
temp = temp->next;
}
}
// Function to mark a task as completed
void markCompleted()
{
if(head==NULL)
printf("To do list is empty");
else
{
int id;
printf("Enter task ID to mark as completed: ");
scanf("%d", &id);
Task* temp = head;
while (temp != NULL)
{
if (temp->id == id)
{
temp->completed = 1;
printf("Task marked as completed.\n");
return;
}
temp = temp->next;
}
printf("Task with ID %d not found.\n", id);
}
}
// Function to delete a task
void deleteTask()
{
int id;
printf("Enter task ID to delete: ");
scanf("%d", &id);
Task *temp = head, *prev = NULL;
while (temp != NULL && temp->id != id)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
{
printf("Task with ID %d not found.\n", id);
return;
}
if (prev == NULL)
{
head = temp->next;
} else
{
prev->next = temp->next;
}
free(temp);
printf("Task deleted successfully.\n");
}
// Main menu
int main()
{
int choice;
do
{
printf("\n----- To-Do List Menu -----\n");
printf("1. Add Task\n");
printf("2. Display Tasks\n");
printf("3. Mark Task as Completed\n");
printf("4. Delete Task\n");
printf("5. Exit\n");
printf("---------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: addTask(); break;
case 2: displayTasks(); break;
case 3: markCompleted(); break;
case 4: deleteTask(); break;
case 5: printf("Exiting program.\n"); break;
default: printf("Invalid choice. Try again.\n");
}
} while (choice != 5);
return 0;
}
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

