|
| 1 | +/* |
| 2 | +[FR] Implementation du concept de Liste chaînée en C++ vue en cours |
| 3 | +[ANG] Implementation of chain list concept in C++ |
| 4 | +@licence = GPL-3 |
| 5 | +@author = Dervaux Esteban |
| 6 | +*/ |
| 7 | +#include <iostream> // For std::cout & std::endl |
| 8 | +#include <stdexcept> // For std::runtime_error |
| 9 | +#include <string> // For std::string, std::to_string(), std::pop_back() |
| 10 | +//#include <cassert> // For check the code with some cases |
| 11 | +using std::string; // Shortcut for std::string to string |
| 12 | + |
| 13 | +struct node { |
| 14 | + int value; |
| 15 | + node* previous; |
| 16 | + // Model of a constructor of an struct |
| 17 | + node(int val, node* prev) : value(val), previous(prev) {} |
| 18 | +}; |
| 19 | + |
| 20 | +/* NodeChain is an node manager using none fixed memory bloc */ |
| 21 | +class NodeChain { |
| 22 | + private : |
| 23 | + unsigned int size; // Size of the chain |
| 24 | + node* sommet; // Pointer to an node at the first place of the chain |
| 25 | + public : |
| 26 | + /* ----- Main methods ----- */ |
| 27 | + void pushNode(int value) { |
| 28 | + /* Insert new node in memory */ |
| 29 | + sommet = new node(value, sommet); |
| 30 | + std::cout << "Added node with value: " << value << std::endl; |
| 31 | + size++; |
| 32 | + } |
| 33 | + |
| 34 | + void pullNode(const unsigned int index) { |
| 35 | + /* Pull a node from the chain using her index */ |
| 36 | + if(index >= size) { |
| 37 | + throw std::runtime_error("IndexError, index >= size of the Chain, index = "+std::to_string(index)+", index need to be < size"); |
| 38 | + } |
| 39 | + node* pulledNode = getByIndex(index); |
| 40 | + if (index == size-1) { |
| 41 | + // In cas we pulled the sommet |
| 42 | + sommet = sommet->previous; // Now the first element is the next one |
| 43 | + } else if (index == 0) { |
| 44 | + // In case we pulled the first element at index 0 |
| 45 | + getByIndex(1)->previous = nullptr; |
| 46 | + } else { |
| 47 | + // In common case |
| 48 | + node* nextNode = getByIndex(index+1); |
| 49 | + node* previousNode = getByIndex(index-1); |
| 50 | + nextNode->previous = previousNode; |
| 51 | + } |
| 52 | + delete pulledNode; // Pull out the node from RAM |
| 53 | + size--; // Decrement size because we pulled an element |
| 54 | + } |
| 55 | + |
| 56 | + node* getByIndex(unsigned int index) { |
| 57 | + /* Return an pointer of the node in the node chain at the index index*/ |
| 58 | + node* actualNode = sommet; |
| 59 | + if(index >= size) { |
| 60 | + throw std::runtime_error("IndexError, index > size of the Chain, index = "+std::to_string(index)); |
| 61 | + } |
| 62 | + |
| 63 | + for (size_t i = size-1; i > index; i--) { |
| 64 | + // We go back to the previous and set it the actual index time |
| 65 | + actualNode = actualNode->previous; |
| 66 | + }; |
| 67 | + return actualNode; // Return result |
| 68 | + } |
| 69 | + |
| 70 | + /* ----- Chain representation methods ----- */ |
| 71 | + string getChainToString() { |
| 72 | + /* Return the chain as a string */ |
| 73 | + string chain = "["; |
| 74 | + if (size != 0) { |
| 75 | + node* actualNode = sommet; |
| 76 | + for (size_t i = size-1; i > 0; i--) { |
| 77 | + chain += std::to_string(actualNode->value) + ", "; |
| 78 | + actualNode = actualNode->previous; |
| 79 | + }; |
| 80 | + chain.pop_back(), chain.pop_back(); |
| 81 | + } |
| 82 | + return chain+"]"; // Return a chain like "[a, b, c, d]" |
| 83 | + } |
| 84 | + |
| 85 | + /* ----- Constructor and destructor ----- */ |
| 86 | + NodeChain() { |
| 87 | + size = 0; // At the construction, the chain is empty |
| 88 | + sommet = nullptr; |
| 89 | + } |
| 90 | + ~NodeChain() { |
| 91 | + while (sommet != nullptr) { |
| 92 | + node* toDelete = sommet; |
| 93 | + std::cout << "Delete sommet : " << sommet << std::endl; |
| 94 | + sommet = sommet->previous; |
| 95 | + delete toDelete; |
| 96 | + }; |
| 97 | + delete sommet; // Enfin tue le sommet |
| 98 | + |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +/* Test function for NodeChain*/ |
| 103 | +class TestNodeChain { |
| 104 | +private: |
| 105 | + // Test getChainToString with empty chain |
| 106 | + int Test_emptyGetChainToString() { |
| 107 | + NodeChain lambdaChain; |
| 108 | + string result = lambdaChain.getChainToString(); |
| 109 | + return (result == "[]") ? 0 : 1; |
| 110 | + } |
| 111 | + // Test push with some value |
| 112 | + int Test_pushNode() { |
| 113 | + NodeChain lambdaChain; |
| 114 | + lambdaChain.pushNode(10); |
| 115 | + lambdaChain.pushNode(0.5); |
| 116 | + return (lambdaChain.getChainToString() == "[10, 0.5]") ? 0 : 1; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +public: |
| 121 | + TestNodeChain() { |
| 122 | + }; |
| 123 | + ~TestNodeChain(){}; |
| 124 | +}; |
| 125 | + |
| 126 | + |
| 127 | +int main(int argc, char const *argv[]) { |
| 128 | + NodeChain manage; |
| 129 | + std::cout << manage.getChainToString() << std::endl; |
| 130 | + manage.pushNode(10); // Create new node |
| 131 | + manage.pushNode(20); // Create new node |
| 132 | + manage.pushNode(30); // Create new node |
| 133 | + manage.pushNode(40); // Create new node |
| 134 | + std::cout << manage.getChainToString() << std::endl; |
| 135 | + manage.pullNode(1); // Deleted second node |
| 136 | + std::cout << manage.getChainToString() << std::endl; |
| 137 | + // "manage" stop being used so the deconstructor of NodeManager is call |
| 138 | + // All node's has been destroyed from the ram |
| 139 | + return 0; |
| 140 | +} |
0 commit comments