-
Notifications
You must be signed in to change notification settings - Fork 11
/
list.cpp
91 lines (79 loc) · 1.49 KB
/
list.cpp
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
#include <iostream>
using namespace std;
class Node
{
public:
Node(const int v) :
next(nullptr),
value(v)
{}
Node* next;
int value;
};
class List
{
public:
List();
void add(Node* node); // dodaje element na koniec listy
Node* get(const int value); // zwraca element o wskazanej wartości
private:
Node* first;
};
List::List() :
first(nullptr)
{}
void List::add(Node* node)
{
if(!first)
{
first = node;
}
else
{
Node* current = first;
while(current->next)
{
current = current->next;
}
current->next = node;
}
}
Node* List::get(const int value)
{
if(!first)
{
cout << "List is empty!" << endl;
return nullptr;
}
else
{
Node* current = first;
do
{
if(current->value == value)
{
cout << "Found value " << current->value << endl;
return current;
}
else
{
cout << "Going through " << current->value << endl;
current = current->next;
}
} while(current);
cout << "Not found: value " << value << endl;
return nullptr;
}
}
int main()
{
List lista;
Node* node4 = new Node(4);
Node* node7 = new Node(7);
lista.add(node4);
lista.add(new Node(2));
lista.add(node7);
lista.add(new Node(9));
auto node = lista.get(1);
return 0;
}