#include <xtd/xtd>
using namespace text;
class example {
public:
static auto main() -> void {
auto sentence = linked_list<string> { "the", "fox", "jumps", "over", "the", "dog" };
display(sentence, "The linked list values:");
sentence.add_first("today");
display(sentence, "Test 1: Add 'today' to beginning of the list:");
auto node = *sentence.first();
sentence.remove(node);
sentence.add_last(node);
display(sentence, "Test 2: Move first node to be last node:");
sentence.remove_last();
sentence.add_last("yesterday");
display(sentence, "Test 3: Change the last node to 'yesterday':");
node = *sentence.last();
sentence.remove(node);
sentence.add_first(node);
display(sentence, "Test 4: Move last node to be first node:");
sentence.remove_first();
node = *sentence.find_last("the");
indicate_node(node, "Test 5: Indicate last occurence of 'the':");
sentence.add_after(node, "old");
sentence.add_after(*sentence.find_last("the"), "lazy");
indicate_node(*sentence.find_last("the"), "Test 6: Add 'lazy' and 'old' after 'the':");
node = *sentence.find_last("fox");
indicate_node(node, "Test 7: Indicate the 'fox' node:");
sentence.add_before(*sentence.find("fox"), "quick");
sentence.add_before(*sentence.find("fox"), "brown");
indicate_node(*sentence.find("fox"), "Test 8: Add 'quick' and 'brown' before 'fox':");
node = *sentence.find_last("dog")->previous();
indicate_node(*sentence.find("dog"), "Test 9: Indicate the 'dog' node:");
console::write_line("Test 10: Throw exception by adding node (fox) already in the list:");
try {
sentence.add_before(*sentence.find("fox"), node);
} catch (const invalid_operation_exception& ex) {
console::write_line("Exception message: {}", ex.message());
}
console::write_line();
node = *sentence.find("fox");
sentence.remove(node);
sentence.add_before(*sentence.find("dog"), node);
node = *sentence.find("dog");
indicate_node(node, "Test 11: Move a referenced node (fox) before the current node (dog):");
sentence.remove(node);
node = sentence.find("dog") ? *sentence.find("dog") : linked_list_node<string>("dog");
indicate_node(node, "Test 12: Remove current node (dog) and attempt to indicate it:");
sentence.add_after(*sentence.find("brown"), node);
indicate_node(node, "Test 13: Add node removed in test 12 after a referenced node (brown):");
sentence.remove("old");
display(sentence, "Test 14: Remove node that has the value 'old':");
sentence.remove_last();
auto& icoll = as<icollection<string>>(sentence);
icoll.add("rhinoceros");
display(sentence, "Test 15: Remove last node, cast to icollection, and add 'rhinoceros':");
console::write_line("Test 16: Copy the list to an array:");
auto sarray = array<string>(sentence.count());
sentence.copy_to(sarray, 0);
for (auto s : sarray)
console::write_line(s);
console::write_line();
console::write_line("Test 17: linked list Contains 'jumps' = {0}", sentence.contains("jumps"));
sentence.clear();
console::write_line();
console::write_line("Test 18: Cleared linked list Contains 'jumps' = {0}", sentence.contains("jumps"));
}
static void display(const linked_list<string>& words, const string& test) {
console::write_line(test);
for (auto word : words)
console::write(word + " ");
console::write_line();
console::write_line();
}
static void indicate_node(const linked_list_node<string>& node, const string& test) {
console::write_line(test);
auto l = node.list();
if (!l) {
console::write_line("Node '{0}' is not in the list.\n", node.value());
return;
}
auto result = string_builder {"(" + node.value() + ")"};
auto node_previous = node.previous();
while (node_previous) {
result.insert(0, node_previous->value() + " ");
node_previous = node_previous->previous();
}
auto node_next = node.next();
while (node_next) {
result.append(" " + node_next->value());
node_next = node_next->next();
}
console::write_line(result);
console::write_line();
}
};
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168