Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
data_structures::list_array::list Struct Reference

Structure of List with supporting methods. More...

Collaboration diagram for data_structures::list_array::list:
[legend]

Public Member Functions

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...
 

Public Attributes

std::array< uint64_t, 50 > data {}
 
uint64_t top = 0
 
bool isSorted = false
 

Detailed Description

Structure of List with supporting methods.

Member Function Documentation

◆ 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
dataArrlist
firstpointer to the first element in the remaining list
lastpointer to the last element in the remaining list
valelement that will be searched
Returns
index of element in the list if present else -1
45 {
46 // If both pointer cross each other means no element present in the list which is equal to the val
47 if (last < first) {
48 return -1;
49 }
50 uint64_t mid = (first + last) / 2;
51 // check whether current mid pointer value is equal to element or not
52 if (dataArr[mid] == val)
53 return mid;
54 // if current mid value is greater than element we have to search in first half
55 else if (val < dataArr[mid])
56 return (BinarySearch(dataArr, first, mid - 1, val));
57 // if current mid value is greater than element we have to search in second half
58 else if (val > dataArr[mid])
59 return (BinarySearch(dataArr, mid + 1, last, val));
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
Here is the call graph for this function:

◆ insert()

void data_structures::list_array::list::insert ( const uint64_t &  val)
inline

Insert the new element in the list.

Parameters
valelement that will be inserted
Returns
void
132 {
133 // overflow check
134 if (top == 49) {
135 std::cout << "\nOverflow";
136 return;
137 }
138 // if list is not sorted, insert at the last
139 // otherwise place it to correct position
140 if (!isSorted) {
141 data[top] = val;
142 top++;
143 } else {
144 uint64_t pos = 0; // Initialize the index variable
145 // Going through each element and find correct position for element
146 for (uint64_t i = 0; i < top - 1; i++) {
147 // check for the correct position
148 if (data[i] <= val && val <= data[i + 1]) {
149 pos = i + 1; // assign correct pos to the index var
150 break; // to get out from the loop
151 }
152 }
153 // if all elements are smaller than the element
154 if (pos == 0) {
155 pos = top - 1;
156 }
157 // shift all element to make a room for new element
158 for (uint64_t i = top; i > pos; i--) {
159 data[i] = data[i - 1];
160 }
161 top++; // Increment the value of top.
162 data[pos] = val; // Assign the value to the correct index in the array
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
dataArrlist
valelement that will be searched
Returns
index of element in the list if present else -1
71 {
72 // Going through each element in the list
73 for (uint64_t i = 0; i < top; i++) {
74 if (dataArr[i] == val) {
75 return i; // element found at ith index
76 }
77 }
78 // element is not present in the list
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
valelement that will be removed
Returns
void
171 {
172 uint64_t pos = search(val); // search the index of the value
173 // if search returns -1, element does not present in the list
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"; // print the appropriate message
179 // shift all the element 1 left to fill vacant space
180 for (uint64_t i = pos; i < top; i++) {
181 data[i] = data[i + 1];
182 }
183 top--; // decrement the top variable to maintain last index
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; // pos variable to store index value of element.
89 // if list is sorted, binary search works efficiently else linear search is the only option
90 if (isSorted) {
91 pos = BinarySearch(data, 0, top - 1, val);
92 } else {
93 pos = LinearSearch(data, val);
94 }
95 // if index is equal to -1 means element does not present
96 // else print the index of that element
97 if (pos != -1) {
98 std::cout << "\nElement found at position : " << pos;
99 } else {
100 std::cout << "\nElement not found";
101 }
102 // return the index of element or -1.
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 // Going through each element in the list
192 std::cout << '\n';
193 for (uint64_t i = 0; i < top; i++) {
194 std::cout << data[i] << " "; // print the element
195 }
196 }

◆ sort()

void data_structures::list_array::list::sort ( )
inline

Sort the list.

Returns
void
110 {
111 //Going through each element in the list
112 for (uint64_t i = 0; i < top; i++) {
113 uint64_t min_idx = i; // Initialize the min variable
114 for (uint64_t j = i + 1; j < top; j++) {
115 // check whether any element less than current min value
116 if (data[j] < data[min_idx]) {
117 min_idx = j; // update index accordingly
118 }
119 }
120 // swap min value and element at the ith index
121 std::swap(data[min_idx], data[i]);
122 }
123 // mark isSorted variable as true
124 isSorted = true;
125 }
T swap(T... args)
Here is the call graph for this function:

The documentation for this struct was generated from the following file: