TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 37 of file list_array.cpp.

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

Definition at line 49 of file list_array.cpp.

51 {
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 }
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.

◆ 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

Definition at line 143 of file list_array.cpp.

143 {
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 }

◆ 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

Definition at line 80 of file list_array.cpp.

81 {
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 }

◆ 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

Definition at line 183 of file list_array.cpp.

183 {
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 }
int data[MAX]
test data
for std::assert

◆ search()

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

Definition at line 97 of file list_array.cpp.

97 {
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 }
uint64_t LinearSearch(const std::array< uint64_t, N > &dataArr, const uint64_t &val) const
Search an element using linear search.

◆ show()

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

Utility function to print array.

Returns
void

Definition at line 203 of file list_array.cpp.

203 {
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 }

◆ sort()

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

Sort the list.

Returns
void

Definition at line 121 of file list_array.cpp.

121 {
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 }

Member Data Documentation

◆ data

template<uint64_t N>
std::array<uint64_t, N> data_structures::list_array::list< N >::data {}

Definition at line 38 of file list_array.cpp.

38{}; // Array that implement list

◆ isSorted

template<uint64_t N>
bool data_structures::list_array::list< N >::isSorted = false

Definition at line 40 of file list_array.cpp.

◆ top

template<uint64_t N>
uint64_t data_structures::list_array::list< N >::top = 0

Definition at line 39 of file list_array.cpp.


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