I thought I had made another reply, but I think I accidentally deleted it.
I don't know what an array-based unordered list is, but after googling around and reading some Java documentation, my best guess is that it's a resizable array with a sequence interface.
In C++, the canonical class for that is , for some element type T, e.g. int. You can use std::vector after including the standard vector header: You can read about the interface of vector and the algorithmic complexity of each operation here: std::vector - cppreference.com
It's likely that your professor has another type in mind (you mentioned some AUList). In that case, use the corresponding operations on that type.
Based on the problem described, a function having the following signature ought to do the trick (assuming you are working with integers):
Code:
std::vector<int> repeatPrefix(const std::vector<int>& original, int prefixLength);
That takes a reference providing non-modifiable access to a vector of ints, and returns a new vector of ints, where the idea is that the returned vector has the first prefixLength elements of the original repeated at the front.
Since you probably won't be using vector, I don't feel too bad writing this for you as an example:
Code:
#include <cassert>
#include <initializer_list>
#include <iostream>
#include <vector>
// Return a copy of the specified `original` but that has additionally had
// the `prefixLength`-length prefix repeated once at the beginning. For example,
// `repeatPrefix({1, 2, 3, 4, 5}, 2) == std::vector<int>({1, 2, 1, 2, 3, 4, 5})`. The
// behavior is undefined unless `prefixLength` is non-negative and less than
// or equal to the length of `original`.
std::vector<int> repeatPrefix(const std::vector<int>& original, int prefixLength) {
assert(prefixLength >= 0);
assert(prefixLength <= int(original.size()));
// This is the object we will return.
std::vector<int> result;
// Make room for the elements. This is unnecessary and a premature
// optimization, but since we know the output length, I can't resist.
result.reserve(original.size() + prefixLength);
// Add the first `prefixLength` elements.
result.insert(result.end(), original.begin(), original.begin() + prefixLength);
// Add the entire input.
result.insert(result.end(), original.begin(), original.end());
return result;
}
int main() {
// See what the result is when we use the example from the contract.
for (int x : repeatPrefix({1, 2, 3, 4, 5}, 2))
std::cout << x << '\n';
}