diff --git a/3.cpp b/3.cpp index 169e055..e7ef56d 100644 --- a/3.cpp +++ b/3.cpp @@ -4,6 +4,8 @@ // на вход. В функции main() создайте проинициализированный // список, со значениями value равными: 1, 2, 3, 4 и 5. +#define NULL 0 + struct List { int value; @@ -14,10 +16,36 @@ struct List // It should return pointer to the added List object. List* Add(List* l, int value) { + List* new_item = new List; + new_item->value = value; + new_item->next = NULL; + + if (l) { + while (l->next) + l = l->next; + l->next = new_item; + } + + return new_item; +} +void Free(List* l) { + if (l->next) + Free(l->next); + delete l; } int main(int argc, char* argv[]) { -return 0; -} \ No newline at end of file + List* begin = Add(NULL, 1); + Add(begin, 2); + Add(begin, 3); + Add(begin, 4); + Add(begin, 5); + + // [...] + + Free(begin); + + return 0; +}