DSA Java Project – To Do List
by DataFlair Team
Program 1
// To-Do List Using Singly Linked List
import java.util.Scanner;
class Task
{
int id;
String description;
boolean completed;
Task next;
public Task(int id, String description)
{
this.id = id;
this.description = description;
this.completed = false;
this.next = null;
}
}
class ToDoList
{
private Task head;
private int taskCount;
public ToDoList()
{
head = null;
taskCount = 0;
}
public void addTask(Scanner scanner)
{
scanner.nextLine(); // clear buffer
System.out.print("Enter task description: ");
String desc = scanner.nextLine();
Task newTask = new Task(++taskCount, desc);
if (head == null)
{
head = newTask;
} else {
Task temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newTask;
}
System.out.println("Task added successfully.");
}
public void displayTasks()
{
if (head == null)
{
System.out.println("To-Do List is empty.");
return;
}
System.out.println("\nTo-Do List:");
Task temp = head;
while (temp != null)
{
String status = temp.completed ? "[X]" : "[ ]";
System.out.println("ID: " + temp.id + " | " + status + " " + temp.description);
temp = temp.next;
}
}
public void markCompleted(Scanner scanner) {
System.out.print("Enter task ID to mark as completed: ");
int id = scanner.nextInt();
Task temp = head;
while (temp != null)
{
if (temp.id == id)
{
temp.completed = true;
System.out.println("Task marked as completed.");
return;
}
temp = temp.next;
}
System.out.println("Task with ID " + id + " not found.");
}
public void deleteTask(Scanner scanner)
{
System.out.print("Enter task ID to delete: ");
int id = scanner.nextInt();
Task temp = head;
Task prev = null;
while (temp != null && temp.id != id) {
prev = temp;
temp = temp.next;
}
if (temp == null) {
System.out.println("Task with ID " + id + " not found.");
return;
}
if (prev == null) {
head = temp.next;
} else {
prev.next = temp.next;
}
System.out.println("Task deleted successfully.");
}
}
public class ToDoListApp
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ToDoList list = new ToDoList();
int choice;
do {
System.out.println("\n----- To-Do List Menu -----");
System.out.println("1. Add Task");
System.out.println("2. Display Tasks");
System.out.println("3. Mark Task as Completed");
System.out.println("4. Delete Task");
System.out.println("5. Exit");
System.out.println("---------------------------");
System.out.print("Enter your choice: ");
while (!scanner.hasNextInt()) {
System.out.println("Please enter a valid number.");
scanner.next(); // clear invalid input
}
choice = scanner.nextInt();
switch (choice) {
case 1:
list.addTask(scanner);
break;
case 2:
list.displayTasks();
break;
case 3:
list.markCompleted(scanner);
break;
case 4:
list.deleteTask(scanner);
break;
case 5:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (choice != 5);
scanner.close();
}
}
Tags: data structures & algorithmsdata structures using javadsa javadsa java practicaldsa java programdsa java projectdsa java to do listdsa java to do list projectto do listto do list in dsa javato do list project
DataFlair Team
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.