-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector Pattern.cpp
More file actions
106 lines (105 loc) · 2.01 KB
/
Copy pathVector Pattern.cpp
File metadata and controls
106 lines (105 loc) · 2.01 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//List
struct Node
{
//List consists of to variables: first data as int and pointer to next container of data
int data;
Node* next;
//Constructor if there is not first container then data is equal to 0 and our pointer points to NULL
Node(int data = 0, Node* next = nullptr)
{
//in other case after adding data we connect two container by next pointer
this->data = data;
this->next = next;
}
};
struct vector
{
Node* head;
int Size;
vector()
{
Size = 0;
head = nullptr;
}
//Constructor creates vector with n size, in eahc container data = 0
vector(int n)
{
//Here are cases when size = 0 and our head pointer points to nullptr
if (Size != 0)
Size = 0;
if (head != nullptr)
head = nullptr;
while (n != 0)
{
head = new Node(0, head);
//Increase vector size after adding new container
Size++;
n--;
}
}
//Constructor creates vector with n size, in eahc container data = val
vector(int n, int val)
{
if (Size != 0)
Size = 0;
if (head != nullptr)
head = nullptr;
while (n != 0)
{
head = new Node(val, head);
Size++;
n--;
}
}
//Adds data to vector's end
void push_back(int data)
{
if (head == nullptr)
{
head = new Node(data);
}
else
{
Node* current = this->head;
//Works until reaching end of vector
while (current->next != nullptr)
{
current = current->next;
}
current->next = new Node(data);
}
Size++;
}
//Deletes last container of vector
void pop_back()
{
Node* current = this->head;
while (current->next != nullptr)
{
current = current->next;
}
delete current->next;
current->next = current;
Size--;
}
//Gets i number container under
int get(int i)
{
int counter = 0;
Node* current = this->head;
while (current != nullptr)
{
if (counter == i)
{
return current->data;
}
current = current->next;
counter++;
}
}
//Return vector's Size
int size()
{
return this->Size;
}
};