Structure of List with supporting methods.
More...
|
uint64_t | BinarySearch (const std::array< uint64_t, 50 > &dataArr, const uint64_t &first, const uint64_t &last, const uint64_t &val) |
| Search an element in the list using binarySearch. More...
|
|
uint64_t | LinearSearch (const std::array< uint64_t, 50 > &dataArr, const uint64_t &val) const |
| Search an element using linear search. More...
|
|
uint64_t | search (const uint64_t &val) |
|
void | sort () |
| Sort the list. More...
|
|
void | insert (const uint64_t &val) |
| Insert the new element in the list. More...
|
|
void | remove (const uint64_t &val) |
| To remove the element from the list. More...
|
|
void | show () |
| Utility function to print array. More...
|
|
|
std::array< uint64_t, 50 > | data {} |
|
uint64_t | top = 0 |
|
bool | isSorted = false |
|
Structure of List with supporting methods.
◆ BinarySearch()
uint64_t data_structures::list_array::list::BinarySearch |
( |
const std::array< uint64_t, 50 > & |
dataArr, |
|
|
const uint64_t & |
first, |
|
|
const uint64_t & |
last, |
|
|
const uint64_t & |
val |
|
) |
| |
|
inline |
Search an element in the list using binarySearch.
- Parameters
-
dataArr | list |
first | pointer to the first element in the remaining list |
last | pointer to the last element in the remaining list |
val | element that will be searched |
- Returns
- index of element in the list if present else -1
45 {
46
47 if (last < first) {
48 return -1;
49 }
50 uint64_t mid = (first + last) / 2;
51
52 if (dataArr[mid] == val)
53 return mid;
54
55 else if (val < dataArr[mid])
57
58 else if (val > dataArr[mid])
60
61 std::cerr << __func__ <<
":" << __LINE__ <<
": Undefined condition\n";
62 return -1;
63 }
uint64_t BinarySearch(const std::array< uint64_t, 50 > &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:44
◆ insert()
void data_structures::list_array::list::insert |
( |
const uint64_t & |
val | ) |
|
|
inline |
Insert the new element in the list.
- Parameters
-
val | element that will be inserted |
- Returns
- void
132 {
133
134 if (top == 49) {
136 return;
137 }
138
139
140 if (!isSorted) {
141 data[top] = val;
142 top++;
143 } else {
144 uint64_t pos = 0;
145
146 for (uint64_t i = 0; i < top - 1; i++) {
147
148 if (data[i] <= val && val <= data[i + 1]) {
149 pos = i + 1;
150 break;
151 }
152 }
153
154 if (pos == 0) {
155 pos = top - 1;
156 }
157
158 for (uint64_t i = top; i > pos; i--) {
159 data[i] = data[i - 1];
160 }
161 top++;
162 data[pos] = val;
163 }
164 }
◆ LinearSearch()
uint64_t data_structures::list_array::list::LinearSearch |
( |
const std::array< uint64_t, 50 > & |
dataArr, |
|
|
const uint64_t & |
val |
|
) |
| const |
|
inline |
Search an element using linear search.
- Parameters
-
dataArr | list |
val | element that will be searched |
- Returns
- index of element in the list if present else -1
71 {
72
73 for (uint64_t i = 0; i < top; i++) {
74 if (dataArr[i] == val) {
75 return i;
76 }
77 }
78
79 return -1;
80 }
◆ remove()
void data_structures::list_array::list::remove |
( |
const uint64_t & |
val | ) |
|
|
inline |
To remove the element from the list.
- Parameters
-
val | element that will be removed |
- Returns
- void
171 {
172 uint64_t pos =
search(val);
173
174 if (pos == -1) {
175 std::cout <<
"\n Element does not present in the list ";
176 return;
177 }
178 std::cout <<
"\n" << data[pos] <<
" deleted";
179
180 for (uint64_t i = pos; i < top; i++) {
182 }
183 top--;
184 }
int data[MAX]
test data
Definition: hash_search.cpp:24
for std::vector
Definition: binary_search.cpp:45
◆ search()
uint64_t data_structures::list_array::list::search |
( |
const uint64_t & |
val | ) |
|
|
inline |
87 {
88 uint64_t pos;
89
90 if (isSorted) {
92 } else {
94 }
95
96
97 if (pos != -1) {
98 std::cout <<
"\nElement found at position : " << pos;
99 } else {
101 }
102
103 return pos;
104 }
uint64_t LinearSearch(const std::array< uint64_t, 50 > &dataArr, const uint64_t &val) const
Search an element using linear search.
Definition: list_array.cpp:71
◆ show()
void data_structures::list_array::list::show |
( |
| ) |
|
|
inline |
Utility function to print array.
- Returns
- void
190 {
191
193 for (uint64_t i = 0; i < top; i++) {
195 }
196 }
◆ sort()
void data_structures::list_array::list::sort |
( |
| ) |
|
|
inline |
Sort the list.
- Returns
- void
110 {
111
112 for (uint64_t i = 0; i < top; i++) {
113 uint64_t min_idx = i;
114 for (uint64_t j = i + 1; j < top; j++) {
115
116 if (data[j] < data[min_idx]) {
117 min_idx = j;
118 }
119 }
120
122 }
123
124 isSorted = true;
125 }
The documentation for this struct was generated from the following file: