DSA Python Project – To Do List
by DataFlair Team
Program 1
# To-Do List Using Singly Linked List
class Task:
def __init__(self, task_id, description):
self.id = task_id
self.description = description
self.completed = False
self.next = None
class ToDoList:
def __init__(self):
self.head = None
self.task_count = 0
def add_task(self):
desc = input("Enter task description: ").strip()
self.task_count += 1
new_task = Task(self.task_count, desc)
if self.head is None:
self.head = new_task
else:
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_task
print("Task added successfully.")
def display_tasks(self):
if not self.head:
print("To-Do List is empty.")
return
print("\nTo-Do List:")
temp = self.head
while temp:
status = "[X]" if temp.completed else "[ ]"
print(f"ID: {temp.id} | {status} {temp.description}")
temp = temp.next
def mark_completed(self):
try:
task_id = int(input("Enter task ID to mark as completed: "))
except ValueError:
print("Invalid input.")
return
temp = self.head
while temp:
if temp.id == task_id:
temp.completed = True
print("Task marked as completed.")
return
temp = temp.next
print(f"Task with ID {task_id} not found.")
def delete_task(self):
try:
task_id = int(input("Enter task ID to delete: "))
except ValueError:
print("Invalid input.")
return
temp = self.head
prev = None
while temp and temp.id != task_id:
prev = temp
temp = temp.next
if not temp:
print(f"Task with ID {task_id} not found.")
return
if not prev:
self.head = temp.next
else:
prev.next = temp.next
print("Task deleted successfully.")
def main():
todo_list = ToDoList()
while True:
print("\n----- To-Do List Menu -----")
print("1. Add Task")
print("2. Display Tasks")
print("3. Mark Task as Completed")
print("4. Delete Task")
print("5. Exit")
print("---------------------------")
choice = input("Enter your choice: ").strip()
if choice == '1':
todo_list.add_task()
elif choice == '2':
todo_list.display_tasks()
elif choice == '3':
todo_list.mark_completed()
elif choice == '4':
todo_list.delete_task()
elif choice == '5':
print("Exiting program.")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()
Tags: dsa pythondsa python programdsa python projectdsa using pythonto do listto do list in dsa pythonto do list projectto do list project in dsa pythonto do list using dsa python
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.