From 725eb1fa147be27f511754a1006ce3e13eacef69 Mon Sep 17 00:00:00 2001 From: zagrichanskiy Date: Sat, 8 Feb 2014 11:24:57 +0200 Subject: [PATCH 1/5] Update 3.cpp --- 3.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/3.cpp b/3.cpp index 169e055..b682570 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; @@ -12,12 +14,30 @@ struct List // Add should create new List object, initialize it by value and add it to the end of the list. // It should return pointer to the added List object. -List* Add(List* l, int value) +List* Add(List* beg, int value) { - + List* new_item = new List; + new_item->value = value; + new_item->next = NULL; + + if (beg) { + List* temp = beg; + while (temp->next) + temp = temp->next; + temp->next = new_item; + } + + return new_item; } int main(int argc, char* argv[]) { -return 0; -} \ No newline at end of file + List* begin = NULL; + begin = Add(begin, 1); + Add(begin, 2); + Add(begin, 3); + Add(begin, 4); + Add(begin, 5); + + return 0; +} From 20fb8d83195f9e916bfdb4590692eb6ee7c49d13 Mon Sep 17 00:00:00 2001 From: zagrichanskiy Date: Sat, 8 Feb 2014 14:44:36 +0200 Subject: [PATCH 2/5] Update 3.cpp --- 3.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3.cpp b/3.cpp index b682570..295910a 100644 --- a/3.cpp +++ b/3.cpp @@ -14,13 +14,13 @@ struct List // Add should create new List object, initialize it by value and add it to the end of the list. // It should return pointer to the added List object. -List* Add(List* beg, int value) +List* Add(List* l, int value) { List* new_item = new List; new_item->value = value; new_item->next = NULL; - if (beg) { + if (l) { List* temp = beg; while (temp->next) temp = temp->next; From af5cc760e43a3f17c0cf59830030f30ff8e59c4b Mon Sep 17 00:00:00 2001 From: zagrichanskiy Date: Sat, 8 Feb 2014 15:10:01 +0200 Subject: [PATCH 3/5] destructor was added --- 3.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/3.cpp b/3.cpp index 295910a..2d6b19e 100644 --- a/3.cpp +++ b/3.cpp @@ -10,6 +10,7 @@ struct List { int value; List* next; + ~List(void); }; // Add should create new List object, initialize it by value and add it to the end of the list. @@ -30,6 +31,12 @@ List* Add(List* l, int value) return new_item; } +List::~List(void) { + if (this->next) + delete this->next; + +} + int main(int argc, char* argv[]) { List* begin = NULL; @@ -39,5 +46,9 @@ int main(int argc, char* argv[]) Add(begin, 4); Add(begin, 5); + // [...] + + delete begin; + return 0; } From 08eb45ffb2a05a45d0fdae35626f025ef84f08f4 Mon Sep 17 00:00:00 2001 From: zagrichanskiy Date: Sat, 8 Feb 2014 17:47:12 +0200 Subject: [PATCH 4/5] global function Free() instead of destructor --- 3.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/3.cpp b/3.cpp index 2d6b19e..9b3baa7 100644 --- a/3.cpp +++ b/3.cpp @@ -10,7 +10,6 @@ struct List { int value; List* next; - ~List(void); }; // Add should create new List object, initialize it by value and add it to the end of the list. @@ -22,7 +21,7 @@ List* Add(List* l, int value) new_item->next = NULL; if (l) { - List* temp = beg; + List* temp = l; while (temp->next) temp = temp->next; temp->next = new_item; @@ -31,16 +30,15 @@ List* Add(List* l, int value) return new_item; } -List::~List(void) { - if (this->next) - delete this->next; - +void Free(List* l) { + if (l->next) + Free(l->next); + delete l; } int main(int argc, char* argv[]) { - List* begin = NULL; - begin = Add(begin, 1); + List* begin = Add(NULL, 1); Add(begin, 2); Add(begin, 3); Add(begin, 4); @@ -48,7 +46,7 @@ int main(int argc, char* argv[]) // [...] - delete begin; + Free(begin); return 0; } From 9954616ca5024f011ffed0701bbd2e9608a1926b Mon Sep 17 00:00:00 2001 From: zagrichanskiy Date: Sat, 8 Feb 2014 18:26:48 +0200 Subject: [PATCH 5/5] useless variable removed --- 3.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/3.cpp b/3.cpp index 9b3baa7..e7ef56d 100644 --- a/3.cpp +++ b/3.cpp @@ -21,10 +21,9 @@ List* Add(List* l, int value) new_item->next = NULL; if (l) { - List* temp = l; - while (temp->next) - temp = temp->next; - temp->next = new_item; + while (l->next) + l = l->next; + l->next = new_item; } return new_item;