-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo2.cpp
More file actions
102 lines (84 loc) · 2.35 KB
/
todo2.cpp
File metadata and controls
102 lines (84 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<string>
using namespace std;
struct Task {
std::string description;
Task* next;
};
void addTask(Task*& head, const std::string& taskDescription) {
Task* newTask = new Task;
newTask->description = taskDescription;
newTask->next = nullptr;
if (head == nullptr) {
head = newTask;
} else {
Task* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newTask;
}
}
void viewTasks(Task* head) {
Task* temp = head;
while (temp != nullptr) {
std::cout << temp->description << std::endl;
temp = temp->next;
}
}
void deleteTask(Task*& head, const std::string& taskDescription) {
Task* temp = head;
Task* prev = nullptr;
if (temp != nullptr && temp->description == taskDescription) {
head = temp->next;
delete temp;
return;
}
while (temp != nullptr && temp->description != taskDescription) {
prev = temp;
temp = temp->next;
}
if (temp == nullptr) {
return;
}
prev->next = temp->next;
delete temp;
}
int main() {
Task* head = nullptr;
std::string taskDescription;
while (true) {
std::cout << "To-Do List Manager\n";
std::cout << "1. Add Task\n";
std::cout << "2. View Tasks\n";
std::cout << "3. Delete Task\n";
std::cout << "4. Exit\n";
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter task description: ";
std::cin.ignore();
std::getline(std::cin, taskDescription);
addTask(head, taskDescription);
break;
case 2:
std::cout << "Tasks:\n";
viewTasks(head);
break;
case 3:
std::cout << "Enter task description to delete: ";
std::cin.ignore();
std::getline(std::cin, taskDescription);
deleteTask(head, taskDescription);
break;
case 4:
std::cout << "Exiting..." << std::endl;
return 0;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
return 0;
}