In: Computer Science
C++ Please
Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below:
class List {
public:
// Default Constructor
List() {// ... }
// Push integer n onto the end of the list
void push_back(int n) {// ... }
// Push integer n onto the beginning of the list
void push_front(int n) {// ... }
// Pop element at the end of list
int pop_back() {// ... }
// Pop element at the beginning of list
int pop_front() {// ... }
// Adds value at index pos. Indices start at 0
void emplace(int pos, int value) {// ... }
// Return whether the list is empty or not
bool empty() {// ... }
};
class List {
private: char* c; int lnth, capacity;
public:
// Default Constructor
//
We can't really resize arrays in C++, but we can
do the next best thing: create a new array of a different length,
//then copy the data from the old array to the new one, and finally
throw the old one away. To do this, we need to //use a new C++
concept, that of a pointer.
List();
~List();
bool append(char x);
List::List() { capacity = INITIAL_LENGTH; c = new char[capacity]; lnth = 0; }
bool List::append(char x) { char* temp; if (lnth >= capacity) { temp = new char[2*capacity]; for (int i=0; i<capacity; i++) { temp[i] = c[i]; } capacity *= 2; delete [] c; c = temp; } c[lnth] = x; lnth++; return true; }
List::~List() { if (c) delete [] c; }
void push_back(int n)
{
list.push_back(n);
// Sorting
function
list.sort();
for
(
auto
it = list.begin(); it !=
list.end(); ++it)
cout
<<
' '
<< *it;
}
// Push integer n onto the beginning of the list
void push_front(int n) {
list.push_front (n);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=list.begin(); it!=list.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// Pop element at the end of list
int pop_back() {
List.pop_back();
// List after
removing element from end
cout <<
"\n\nList after removing an element from end:
"
;
for
(
auto
itr = List.begin(); itr !=
List.end(); itr++)
cout
<< *itr <<
" "
;
}
// Pop element at the beginning of list
int pop_front() {
List.pop_front();
// List after
removing element from front
cout <<
"\n\nList after removing an element from end:
"
;
for
(
auto
itr = List.begin(); itr !=
List.end(); itr++)
cout
<< *itr <<
" "
;
}
// Adds value at index pos. Indices start at 0
void emplace(int pos, int value) {
//Converting list to vector
std::vector<int>
vec{std::make_move_iterator(list.begin()),
std::make_move_iterator(list.end())};
for(char const &c vec)
{
std::cout <<c<<' ' ;
}
// Create Iterator pointing to posth Position
auto itPos = vecOfNums.begin() + pos;
// Insert element with value at posth Position in vector
auto newIt = vecOfNums.insert(itPos, value);
}
// Return whether the list is empty or not
bool empty() {
// check if list is empty
if
(List.empty())
return true
else
return false;
}
};