forked from vitalWord/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
62 lines (47 loc) · 1.27 KB
/
Copy path3.cpp
File metadata and controls
62 lines (47 loc) · 1.27 KB
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
// Напишите функцию Add(), которая создает новый объект List, инициализирует
// его входным значением value и добавляет его в конец списка l, полученного на вход.
// В функции main() создайте проинициализированный список, со значениями value равными: 1, 2, 3, 4 и 5.
#include <iostream>
struct List
{
int value;
List* next;
~List();
void printList() const;
};
// 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 *newElem = new List;
newElem->value = value;
newElem->next = NULL;
if (l) {
while (l->next)
l = l->next;
l->next = newElem;
}
return newElem;
}
int main(int argc, char* argv[])
{
List *l, *tmpList;
size_t i;
l = Add(NULL, 1);
for (i = 2, tmpList = l; i < 6; i++)
tmpList = Add(tmpList, i);
l->printList();
delete l;
return 0;
}
List::~List()
{
delete next;
}
void List::printList() const
{
std::cout << "The list is:";
for (const List *iter = this; iter; iter = iter->next)
std::cout << " " << iter->value;
std::cout << std::endl;
}