Aufgabe 15
Schwierigkeitsgrad: Schwer
Themen: Kontrollstrukturen Programmausgabe Klassen Zeiger
- Aufgabenstellung
- Lösung
Erstellen Sie mithilfe von Klassen eine Todo-Liste mit verschiedenen Aufgaben. Zu der Liste soll man Aufgaben mit add(...) hinzufügen und mit remove(...) wieder entfernen können. Jede Aufabe hat eine Textbeschreibung und eine Priorität, nach der diese in der Liste geordnet wird. Nutzen Sie Header-Dateien für die Deklaration und Source-Dateien für die Definition der Klassen.
Implementieren und Testen Sie die folgenden Klassen:
class Task
{
public:
string content;
int priority;
Task* prev;
Task* next;
Task(string content, int priority, Task* prev, Task* next);
~Task();
friend ostream &operator<<(ostream &out, const Task &t);
};
class List
{
public:
Task *first;
Task *last;
List();
~List();
void add(string content, int priority);
void remove(string content);
friend ostream &operator<<(ostream &out, const List &l);
};
list.hpp
#pragma once
#include <string>
using std::string;
#include <iostream>
using std::ostream, std::cout, std::endl;
class Task
{
public:
string content;
int priority;
Task *prev;
Task *next;
Task(string content, int priority, Task *prev, Task *next);
~Task();
friend ostream &operator<<(ostream &out, const Task &t);
};
class List
{
public:
Task *first;
Task *last;
List();
~List();
void add(string content, int priority);
void remove(string content);
friend ostream &operator<<(ostream &out, const List &l);
};
list.cpp
#include "list.hpp"
Task::Task(string content, int priority, Task *prev, Task *next) : content(content), priority(priority), prev(prev), next(next) {}
Task::~Task() {}
ostream &operator<<(ostream &out, const Task &t)
{
out << t.content << " - " << t.priority << endl;
return out;
}
List::List() : first(nullptr), last(nullptr) {}
List::~List()
{
Task *t = this->first;
while (t != nullptr)
{
Task *next_task = t->next;
delete t;
t = next_task;
}
}
ostream &operator<<(ostream &out, const List &l)
{
Task *t = l.first;
while (t != nullptr)
{
out << *t;
t = t->next;
}
return out;
}
void List::add(string content, int priority)
{
if (this->first == nullptr)
{
this->first = new Task(content, priority, nullptr, nullptr);
this->last = this->first;
return;
}
Task *next_task = this->first;
while (next_task != nullptr && next_task->priority <= priority)
{
next_task = next_task->next;
}
if (next_task == nullptr)
{
this->last->next = new Task(content, priority, this->last, nullptr);
this->last = this->last->next;
return;
}
if (next_task == this->first)
{
this->first->prev = new Task(content, priority, nullptr, this->first);
this->first = this->first->prev;
return;
}
next_task->prev->next = new Task(content, priority, next_task->prev, next_task);
next_task->prev = next_task->prev->next;
}
void List::remove(string content)
{
Task *t = this->first;
while (t != nullptr)
{
if (t->content == content)
{
if (t == this->first)
{
this->first = t->next;
if (this->first != nullptr)
this->first->prev = nullptr;
}
else if (t == this->last)
{
this->last = t->prev;
if (this->last != nullptr)
this->last->next = nullptr;
}
else
{
if (t->prev != nullptr)
t->prev->next = t->next;
if (t->next != nullptr)
t->next->prev = t->prev;
}
Task *to_delete = t;
t = t->next;
delete to_delete;
}
else
{
t = t->next;
}
}
}
main.cpp
#include "list.hpp"
int main()
{
List l = List();
//Ein paar Funktionen zum Testen:
l.add("clean the room", 1);
l.add("do homework", 5);
//l.remove("clean the room");
l.add("take out trash", 4);
l.add("do homework", 4);
//l.remove("do homework");
//l.remove("take out trash");
l.add("visit friends", 0);
//l.remove("nothing on the list");
cout << l << endl;
}