Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::list_array::list< N > Struct Template Reference

Structure of List with supporting methods. More...

Collaboration diagram for data_structures::list_array::list< N >:
[legend]

Public Member Functions

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.
 
uint64_t LinearSearch (const std::array< uint64_t, N > &dataArr, const uint64_t &val) const
 Search an element using linear search.
 
uint64_t search (const uint64_t &val)
 
void sort ()
 Sort the list.
 
void insert (const uint64_t &val)
 Insert the new element in the list.
 
void remove (const uint64_t &val)
 To remove the element from the list.
 
void show ()
 Utility function to print array.
 

Public Attributes

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

Detailed Description

template<uint64_t N>
struct data_structures::list_array::list< N >

Structure of List with supporting methods.

Member Function Documentation

◆ BinarySearch()

template<uint64_t N>
uint64_t data_structures::list_array::list< N >::BinarySearch ( const std::array< uint64_t, N > & 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
46 {
47 // If both pointer cross each other means no element present in the list which is equal to the val
48 if (last < first) {
49 return -1;
50 }
51 uint64_t mid = (first + last) / 2;
52 // check whether current mid pointer value is equal to element or not
53 if (dataArr[mid] == val)
54 return mid;
55 // if current mid value is greater than element we have to search in first half
56 else if (val < dataArr[mid])
57 return (BinarySearch(dataArr, first, mid - 1, val));
58 // if current mid value is greater than element we have to search in second half
59 else if (val > dataArr[mid])
60 return (BinarySearch(dataArr, mid + 1, last, val));
61
62 std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n";
63 return -1;
64 }
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:45
Here is the call graph for this function:

◆ insert()

template<uint64_t N>
void data_structures::list_array::list< N >::insert ( const uint64_t & val)
inline

Insert the new element in the list.

Parameters
valelement that will be inserted
Returns
void
133 {
134 // overflow check
135 if (top == N) {
136 std::cout << "\nOverflow";
137 return;
138 }
139 // if list is not sorted, insert at the last
140 // otherwise place it to correct position
141 if (!isSorted) {
142 data[top] = val;
143 top++;
144 } else {
145 uint64_t pos = 0; // Initialize the index variable
146 // Going through each element and find correct position for element
147 for (uint64_t i = 0; i < top - 1; i++) {
148 // check for the correct position
149 if (data[i] <= val && val <= data[i + 1]) {
150 pos = i + 1; // assign correct pos to the index var
151 break; // to get out from the loop
152 }
153 }
154 // if all elements are smaller than the element
155 if (pos == 0) {
156 pos = top - 1;
157 }
158 // shift all element to make a room for new element
159 for (uint64_t i = top; i > pos; i--) {
160 data[i] = data[i - 1];
161 }
162 top++; // Increment the value of top.
163 data[pos] = val; // Assign the value to the correct index in the array
164 }
165 }

◆ LinearSearch()

template<uint64_t N>
uint64_t data_structures::list_array::list< N >::LinearSearch ( const std::array< uint64_t, N > & 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
72 {
73 // Going through each element in the list
74 for (uint64_t i = 0; i < top; i++) {
75 if (dataArr[i] == val) {
76 return i; // element found at ith index
77 }
78 }
79 // element is not present in the list
80 return -1;
81 }

◆ remove()

template<uint64_t N>
void data_structures::list_array::list< N >::remove ( const uint64_t & val)
inline

To remove the element from the list.

Parameters
valelement that will be removed
Returns
void
172 {
173 uint64_t pos = search(val); // search the index of the value
174 // if search returns -1, element does not present in the list
175 if (pos == -1) {
176 std::cout << "\n Element does not present in the list ";
177 return;
178 }
179 std::cout << "\n" << data[pos] << " deleted"; // print the appropriate message
180 // shift all the element 1 left to fill vacant space
181 for (uint64_t i = pos; i < top; i++) {
182 data[i] = data[i + 1];
183 }
184 top--; // decrement the top variable to maintain last index
185 }
int data[MAX]
test data
Definition hash_search.cpp:24
for std::vector
Definition binary_search.cpp:45

◆ search()

template<uint64_t N>
uint64_t data_structures::list_array::list< N >::search ( const uint64_t & val)
inline
88 {
89 uint64_t pos; // pos variable to store index value of element.
90 // if list is sorted, binary search works efficiently else linear search is the only option
91 if (isSorted) {
92 pos = BinarySearch(data, 0, top - 1, val);
93 } else {
94 pos = LinearSearch(data, val);
95 }
96 // if index is equal to -1 means element does not present
97 // else print the index of that element
98 if (pos != -1) {
99 std::cout << "\nElement found at position : " << pos;
100 } else {
101 std::cout << "\nElement not found";
102 }
103 // return the index of element or -1.
104 return pos;
105 }
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:72

◆ show()

template<uint64_t N>
void data_structures::list_array::list< N >::show ( )
inline

Utility function to print array.

Returns
void
191 {
192 // Going through each element in the list
193 std::cout << '\n';
194 for (uint64_t i = 0; i < top; i++) {
195 std::cout << data[i] << " "; // print the element
196 }
197 }

◆ sort()

template<uint64_t N>
void data_structures::list_array::list< N >::sort ( )
inline

Sort the list.

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

Member Data Documentation

◆ data

template<uint64_t N>
std::array<uint64_t, N> data_structures::list_array::list< N >::data {}
34{}; // Array that implement list

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