diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-12-05 22:21:11 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-12-05 22:21:11 +0100 |
commit | a60e8a8eabde116cc3da920b637bc4f6f5b8b17c (patch) | |
tree | bd62368e27aa21884de036df150c139469f0fde6 /tests/exec/list.cpp | |
parent | a01d09ca4730de4987d67e73a8ee895f77f57f9c (diff) | |
download | LPC-Projet-a60e8a8eabde116cc3da920b637bc4f6f5b8b17c.tar.gz LPC-Projet-a60e8a8eabde116cc3da920b637bc4f6f5b8b17c.zip |
Added tests.
Diffstat (limited to 'tests/exec/list.cpp')
-rw-r--r-- | tests/exec/list.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
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 <iostream> + +// 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"; + } +} |