-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCode.cpp
151 lines (137 loc) · 3.78 KB
/
Code.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <bits/stdc++.h>
using namespace std;
class vector
{
private:
int size;
int capacity;
int *arr = nullptr;
public:
//if the user does not input the size for the vector
vector()
{
size = 0;
capacity = 10;
arr = new int[capacity];
}
//if the user input size for the vector
vector(int size) : size(size)
{
if (size < 0)
size = 1;
capacity = size + 10;
arr = new int[capacity];
}
// Returns the logic size (number items in vector)
int get_size() { return size; }
// Returns the real size
int get_capacity() { return capacity; }
// insert items to back
void push_back(int val)
{
//if the array is full
if (capacity == size)
{
//Double the capacity
capacity *= 2;
//create a new array with the new capacity
int *new_arr = new int[capacity]{};
//copy all items in the old_array to the new_array
for (int i = 0; i < size; i++)
new_arr[i] = arr[i];
//delete all copied items in the old array
delete[] arr;
//arr pointer points to the new array
arr = new_arr;
}
//add the new the item
arr[size] = val;
// increment the size
++size;
}
// delete items to back
void pop_back()
{
// actually we do not delete items just decrement the size
//If size = 0 can't decrement the sizeو because the size will be negative!!
if(size > 0){
// Decrement the size
--size;
// change value of the item need to delete .. you can don't do that
arr[size] = 0;
}
else{
// clean memory
delete[] arr;
// change the value of the capacity because if we need to push_back items again
capacity = 1;
}
}
// set value at the index (change value in the index)
void set_in(int val, int idx)
{
if(idx < size && idx >= 0){
arr[idx] = val;
}
}
// get value
int get_from(int idx)
{
if(idx >= 0 && idx < size)
return arr[idx];
}
//Insert an item at an index
void insert_at(int val, int idx)
{
if(idx >= 0 && idx < size){
//if the array is full
if (capacity == size)
{
//Double the capacity
capacity *= 2;
//create a new array with the new capacity
int *new_arr = new int[capacity]{};
//copy all items in the old_array to the new_array
for (int i = 0; i < size; i++)
new_arr[i] = arr[i];
//delete all copied items in the old array
delete[] arr;
//arr pointer points to the new array
arr = new_arr;
}
//change data
for (int i = size; i > idx; --i)
{
arr[i] = arr[i - 1];
}
//insert the new item
arr[idx] = val;
// increment the size
++size;
}
}
// delete an item at an index
void delete_at(int idx)
{
if(idx < size && idx >= 0){
// change data
for (int i = idx; i < size-1; ++i)
arr[i] = arr[i+1];
//Change the value of the last item to 0
arr[size-1] = 0;
// Decrement the size
--size;
}
}
//Print all vector
void print()
{
for (int i = 0; i < size; ++i)
cout << arr[i] << "\n";
}
//Check if the vector is empty
bool is_empty()
{
return size==0;
}
};