DSA C++ Project – To Do List

Program 1

// TO DO List Project Based on Linked List
#include <iostream>
#include <string>
using namespace std;

struct Task 
{
    int id;
    string description;
    bool completed;
    Task* next;

   // Task(int id, const string& desc) : id(id), description(desc), completed(false), next(nullptr) {}

   Task(int id, const string& desc) 
   {
     this->id = id;
     this->description = desc;
     this->completed = false;
     this->next = nullptr;
  }
};

class ToDoList 
{
private:
    Task* head;
    int taskCount;

public:
    ToDoList() 
    {
        head = nullptr;
        taskCount = 0;
    }

    void addTask() 
    {
        cin.ignore(); // clear buffer
        string desc;
        cout << "Enter task description: ";
        getline(cin, desc);

        Task* newTask = new Task(++taskCount, desc);

        if (!head) 
        {
            head = newTask;
        } 
        else 
        {
            Task* temp = head;
            while (temp->next)
                temp = temp->next;
            temp->next = newTask;
        }
        cout << "Task added successfully.\n";
    }

    void displayTasks() 
    {
        if (!head) 
        {
            cout << "To-Do List is empty.\n";
            return;
        }

        cout << "\nTo-Do List:\n";
        Task* temp = head;
        while (temp) 
        {
            cout << "ID: " << temp->id << " | ["<< (temp->completed ? "X" : " ") << "] "
                 << temp->description << "\n";
            temp = temp->next;
        }
    }

    void markCompleted() 
    {
        int id;
        cout << "Enter task ID to mark as completed: ";
        cin >> id;

        Task* temp = head;
        while (temp) {
            if (temp->id == id) {
                temp->completed = true;
                cout << "Task marked as completed.\n";
                return;
            }
            temp = temp->next;
        }
        cout << "Task with ID " << id << " not found.\n";
    }

    void deleteTask() 
    {
        int id;
        cout << "Enter task ID to delete: ";
        cin >> id;

        Task* temp = head;
        Task* prev = nullptr;

        while (temp && temp->id != id) 
        {
            prev = temp;
            temp = temp->next;
        }

        if (!temp) 
        {
            cout << "Task with ID " << id << " not found.\n";
            return;
        }

        if (!prev) 
        {
            head = temp->next;
        } else 
        {
            prev->next = temp->next;
        }

        delete temp;
        cout << "Task deleted successfully.\n";
    }
};

int main() 
{
    ToDoList list;
    int choice;

    do 
    {
        cout << "\n----- To-Do List Menu -----\n";
        cout << "1. Add Task\n";
        cout << "2. Display Tasks\n";
        cout << "3. Mark Task as Completed\n";
        cout << "4. Delete Task\n";
        cout << "5. Exit\n";
        cout << "---------------------------\n";
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) 
        {
            case 1: list.addTask(); break;
            case 2: list.displayTasks(); break;
            case 3: list.markCompleted(); break;
            case 4: list.deleteTask(); break;
            case 5: cout << "Exiting program.\n"; break;
            default: cout << "Invalid choice. Try again.\n";
        }

    } while (choice != 5);

    return 0;
}

 

courses

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.

Leave a Reply

Your email address will not be published. Required fields are marked *