TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
list_array.cpp
Go to the documentation of this file.
1
16
17
#include <array>
18
#include <cassert>
19
#include <cstdint>
20
#include <iostream>
21
26
namespace
data_structures
{
32
namespace
list_array
{
36
template
<u
int
64_t N>
37
struct
list
{
38
std::array<uint64_t, N> data{};
// Array that implement list
39
uint64_t top = 0;
// Pointer to the last element
40
bool
isSorted =
false
;
// indicator whether list is sorted or not
49
uint64_t
BinarySearch
(
const
std::array<uint64_t, N> &dataArr,
50
const
uint64_t &first,
const
uint64_t &last,
51
const
uint64_t &val) {
52
// If both pointer cross each other means no element present in the list
53
// which is equal to the val
54
if
(last < first) {
55
return
-1;
56
}
57
uint64_t mid = (first + last) / 2;
58
// check whether current mid pointer value is equal to element or not
59
if
(dataArr[mid] == val)
60
return
mid;
61
// if current mid value is greater than element we have to search in
62
// first half
63
else
if
(val < dataArr[mid])
64
return
(
BinarySearch
(dataArr, first, mid - 1, val));
65
// if current mid value is greater than element we have to search in
66
// second half
67
else
if
(val > dataArr[mid])
68
return
(
BinarySearch
(dataArr, mid + 1, last, val));
69
70
std::cerr << __func__ <<
":"
<< __LINE__ <<
": Undefined condition\n"
;
71
return
-1;
72
}
73
80
uint64_t
LinearSearch
(
const
std::array<uint64_t, N> &dataArr,
81
const
uint64_t &val)
const
{
82
// Going through each element in the list
83
for
(uint64_t i = 0; i < top; i++) {
84
if
(dataArr[i] == val) {
85
return
i;
// element found at ith index
86
}
87
}
88
// element is not present in the list
89
return
-1;
90
}
91
92
/*
93
* @brief Parent function of binarySearch and linearSearch methods
94
* @param val element that will be searched
95
* @return index of element in the list if present else -1
96
*/
97
uint64_t
search
(
const
uint64_t &val) {
98
uint64_t pos;
// pos variable to store index value of element.
99
// if list is sorted, binary search works efficiently else linear search
100
// is the only option
101
if
(isSorted) {
102
pos =
BinarySearch
(
data
, 0, top - 1, val);
103
}
else
{
104
pos =
LinearSearch
(
data
, val);
105
}
106
// if index is equal to -1 means element does not present
107
// else print the index of that element
108
if
(pos != -1) {
109
std::cout <<
"\nElement found at position : "
<< pos;
110
}
else
{
111
std::cout <<
"\nElement not found"
;
112
}
113
// return the index of element or -1.
114
return
pos;
115
}
116
121
void
sort
() {
122
// Going through each element in the list
123
for
(uint64_t i = 0; i < top; i++) {
124
uint64_t min_idx = i;
// Initialize the min variable
125
for
(uint64_t j = i + 1; j < top; j++) {
126
// check whether any element less than current min value
127
if
(data[j] < data[min_idx]) {
128
min_idx = j;
// update index accordingly
129
}
130
}
131
// swap min value and element at the ith index
132
std::swap(data[min_idx], data[i]);
133
}
134
// mark isSorted variable as true
135
isSorted =
true
;
136
}
137
143
void
insert
(
const
uint64_t &val) {
144
// overflow check
145
if
(top == N) {
146
std::cout <<
"\nOverflow"
;
147
return
;
148
}
149
// if list is not sorted, insert at the last
150
// otherwise place it to correct position
151
if
(!isSorted) {
152
data[top] = val;
153
top++;
154
}
else
{
155
uint64_t pos = 0;
// Initialize the index variable
156
// Going through each element and find correct position for element
157
for
(uint64_t i = 0; i < top - 1; i++) {
158
// check for the correct position
159
if
(data[i] <= val && val <= data[i + 1]) {
160
pos = i + 1;
// assign correct pos to the index var
161
break
;
// to get out from the loop
162
}
163
}
164
// if all elements are smaller than the element
165
if
(pos == 0) {
166
pos = top - 1;
167
}
168
// shift all element to make a room for new element
169
for
(uint64_t i = top; i > pos; i--) {
170
data[i] = data[i - 1];
171
}
172
top++;
// Increment the value of top.
173
data[pos] =
174
val;
// Assign the value to the correct index in the array
175
}
176
}
177
183
void
remove
(
const
uint64_t &val) {
184
uint64_t pos =
search
(val);
// search the index of the value
185
// if search returns -1, element does not present in the list
186
if
(pos == -1) {
187
std::cout <<
"\n Element does not present in the list "
;
188
return
;
189
}
190
std::cout <<
"\n"
191
<< data[pos] <<
" deleted"
;
// print the appropriate message
192
// shift all the element 1 left to fill vacant space
193
for
(uint64_t i = pos; i < top; i++) {
194
data[i] = data[i + 1];
195
}
196
top--;
// decrement the top variable to maintain last index
197
}
198
203
void
show
() {
204
// Going through each element in the list
205
std::cout <<
'\n'
;
206
for
(uint64_t i = 0; i < top; i++) {
207
std::cout << data[i] <<
" "
;
// print the element
208
}
209
}
210
};
// structure list
211
}
// namespace list_array
212
}
// namespace data_structures
213
218
static
void
test
() {
219
data_structures::list_array::list<50>
L;
220
221
// Insert testing
222
L.
insert
(11);
223
L.
insert
(12);
224
assert(L.top == 2);
225
L.
insert
(15);
226
L.
insert
(10);
227
L.
insert
(12);
228
L.
insert
(20);
229
L.
insert
(18);
230
assert(L.top == 7);
231
L.
show
();
// To print the array
232
233
// Remove testing
234
L.
remove
(12);
// Remove Duplicate value in the list
235
L.
remove
(15);
// Remove the existing value in the list
236
assert(L.top == 5);
237
L.
remove
(50);
// Try to remove the non-existing value in the list
238
assert(L.top == 5);
239
240
// LinearSearch testing
241
assert(L.search(11) == 0);
// search for the existing element
242
assert(L.search(12) == 2);
243
assert(L.search(50) == -1);
// search for the non-existing element
244
245
// Sort testing
246
L.
sort
();
247
assert(L.isSorted ==
true
);
248
L.
show
();
249
250
// BinarySearch testing
251
assert(L.search(11) == 1);
// search for the existing element
252
assert(L.search(12) == 2);
253
assert(L.search(50) == -1);
// search for the non-existing element
254
}
255
260
int
main
() {
261
test
();
// Execute the tests
262
return
0;
263
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data
int data[MAX]
test data
Definition
hash_search.cpp:24
LinearSearch
int LinearSearch(int *array, int size, int key)
for IO operations
Definition
linear_search.cpp:21
data_structures
for IO operations
list_array
Functions for Dynamic Array algorithm.
search
for std::assert
Definition
binary_search.cpp:47
data_structures::list_array::list
Structure of List with supporting methods.
Definition
list_array.cpp:37
data_structures::list_array::list::show
void show()
Utility function to print array.
Definition
list_array.cpp:203
data_structures::list_array::list::sort
void sort()
Sort the list.
Definition
list_array.cpp:121
data_structures::list_array::list::BinarySearch
uint64_t BinarySearch(const std::array< uint64_t, N > &dataArr, const uint64_t &first, const uint64_t &last, const uint64_t &val)
Search an element in the list using binarySearch.
Definition
list_array.cpp:49
data_structures::list_array::list::remove
void remove(const uint64_t &val)
To remove the element from the list.
Definition
list_array.cpp:183
data_structures::list_array::list::insert
void insert(const uint64_t &val)
Insert the new element in the list.
Definition
list_array.cpp:143
data_structures::list_array::list::LinearSearch
uint64_t LinearSearch(const std::array< uint64_t, N > &dataArr, const uint64_t &val) const
Search an element using linear search.
Definition
list_array.cpp:80
data_structures
list_array.cpp
Generated by
1.18.0