From a60e8a8eabde116cc3da920b637bc4f6f5b8b17c Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Thu, 5 Dec 2013 22:21:11 +0100 Subject: Added tests. --- tests/exec/list.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/exec/list.cpp (limited to 'tests/exec/list.cpp') diff --git a/tests/exec/list.cpp b/tests/exec/list.cpp new file mode 100644 index 0000000..f1aadcb --- /dev/null +++ b/tests/exec/list.cpp @@ -0,0 +1,48 @@ +#include + +// liste simplement chaînée + +class cell { +public: + int v; + cell *next; + cell(int v, cell* next); +}; + +cell::cell(int v, cell* next) { + this->v = v; + this->next = next; +} + +// liste chaînée de size éléments, avec accès à l'élément i (en temps O(i)) + +class list { +public: + cell *head; + list(int size); + int& get(int i); +}; + +list::list(int size) { + this->head = NULL; + while (size-- > 0) this->head = new cell(0, this->head); +} + +int& list::get(int i) { + cell *c = this->head; + while (i-- > 0) c = c->next; + return c->v; +} + +// on s'en sert comme un tableau pour calculer les premiers nombres +// de Fibonacci (en temps quadratique, donc) + +int main() { + list l = list(11); + l.get(1) = 1; + int i; + for (i = 2; i < 11; i++) { + l.get(i) = l.get(i-2) + l.get(i-1); + std::cout << "F(" << i << ") = " << l.get(i) << "\n"; + } +} -- cgit v1.2.3