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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| #include <iostream>
#include <cstdlib>
class c_linked_list {
public:
// c_linked_list() : head(NULL), last(NULL), nb_links(0) {}
c_linked_list() { init(); }
~c_linked_list() { reset(); }
public:
friend std::ostream& operator<<(std::ostream&, const c_linked_list&);
public:
void add_value(int /*new_value*/);
void reset();
bool split_half(c_linked_list& /*result_list*/) const;
private:
inline void init() { head = NULL; last = NULL; nb_links = 0; }
private:
class c_one_link {
public:
c_one_link(int input_value=0) : value(input_value), next(NULL) {}
public:
inline c_one_link* get_next() const { return next; }
inline int get_value() const { return value; }
inline void set_next(c_one_link* next_link) { next = next_link; }
inline void set_value(int input_value) { value = input_value; }
private:
int value;
c_one_link* next;
};
private:
c_one_link* head;
c_one_link* last;
size_t nb_links;
};
void c_linked_list::add_value(int new_value) {
c_one_link* one_link = new c_one_link(new_value);
if (last != NULL) {
last->set_next(one_link);
}
if (head == NULL) {
head = one_link;
}
last = one_link;
++nb_links;
}
void c_linked_list::reset() {
c_one_link* one_link = head;
c_one_link* next_link;
while (one_link != NULL) {
next_link = one_link->get_next();
delete one_link;
one_link = next_link;
}
init();
}
bool c_linked_list::split_half(c_linked_list& result_list) const {
bool result;
if ((nb_links % 2) == 0) {
c_one_link* one_link = head;
size_t count = (nb_links / 2);
result_list.reset();
while ((count != 0) && (one_link != NULL)) {
result_list.add_value( one_link->get_value() );
one_link = one_link->get_next();
--count;
}
result = true;
} else {
result = false;
}
return result;
}
std::ostream& operator<<(std::ostream& os, const c_linked_list& linked_list) {
if (linked_list.nb_links > 0) {
c_linked_list::c_one_link* one_link = linked_list.head;
os << "[";
while (one_link != NULL) {
if (one_link != linked_list.last) {
os << one_link->get_value() << ", ";
} else {
os << one_link->get_value() << "]";
}
one_link = one_link->get_next();
}
} else {
os << "[]";
}
return os;
}
int main (int argc, char** argv)
{
c_linked_list list;
c_linked_list half_list;
list.add_value(5);
list.add_value(4);
list.add_value(6);
list.add_value(9);
std::cout << list << std::endl;
list.split_half(half_list);
std::cout << std::endl << "Half List:" << std::endl << half_list << std::endl;
list.add_value(26);
list.add_value(48);
std::cout << std::endl << std::endl << list << std::endl;
list.split_half(half_list);
std::cout << std::endl << "Half List:" << std::endl << half_list << std::endl;
list.reset();
std::cout << std::endl << std::endl << "Reset:" << std::endl << list << std::endl;
return EXIT_SUCCESS;
} |
Partager