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() {// ... }
};
#include <iostream>
using namespace std;
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();
private:
int *data; // pointer to data
int size; // number of elements in data
};
// Default Constructor to create an empty list
List::List()
{
data = NULL; // set data to null pointer
size = 0; // set number of elements to 0
}
// Push integer n onto the end of the list
void List:: push_back(int n)
{
// create a new array of size+1
int *temp = new int[size+1];
// list is not empty
if(!empty())
{
// loop to copy existing elements from data to temp
for(int i=0;i<size;i++)
temp[i] = data[i];
delete data; // delete the existing array
}
// set data to temp
data = temp;
data[size] = n; // insert n at the end
size++; // increment size
}
// Push integer n onto the beginning of the list
void List:: push_front(int n)
{
// create a new array of size+1
int *temp = new int[size+1];
temp[0] = n; // set n to index 0
// list is not empty
if(!empty())
{
// loop to copy existing elements from data to temp
for(int i=0;i<size;i++)
temp[i+1] = data[i];
delete data; // delete the existing array
}
// set data to temp
data = temp;
size++; // increment size
}
// Pop element at the end of list
int List::pop_back()
{
int n = -9999; // default garbage value
// list is not empty
if(!empty())
{
n = data[size-1]; // get the last element
size--; // decrement the size
if(size == 0) // empty list, set data to null
{
delete data; // delete existing array
data = NULL;
}
else // non-empty list
{
// create a new array of size
int *temp = new int[size];
// loop to copy from data to temp
for(int i=0;i<size;i++)
temp[i] = data[i];
delete data; // delete data
data = temp; // set data to temp
}
}
return n; // return the removed data
}
// Pop element at the beginning of list
int List::pop_front()
{
int n = -9999; // default garbage value
// list is not empty
if(!empty())
{
n = data[0]; // get the first element
size--; // decrement size
// empty list, set data to null
if(size == 0)
{
delete data; // delete data
data = NULL;
}
else
{
// create a new array of size
int *temp = new int[size];
// loop to copy from data to temp
for(int i=1;i<=size;i++)
temp[i-1] = data[i];
delete data; // delete data
data = temp; // set data to temp
}
}
return n;
}
// Adds value at index pos. Indices start at 0
void List:: emplace(int pos, int value)
{
// validate index
if(pos >= 0 && pos <= size)
{
if(pos == 0) // insert at front
push_front(value);
else if(pos == size) // insert at back
push_back(value);
else
{
// create a new array of size+1
int *temp = new int[size+1];
// loop to copy elements from start to pos-1
for(int i=0;i<pos;i++)
temp[i] = data[i];
temp[pos] = value; // set pos to value
// loop to copy elements from pos to end
for(int i=pos+1;i<=size;i++)
{
temp[i] = data[i-1];
}
delete data; // delete data
data = temp; // set data to temp
size++; // increment size
}
}
}
// Return whether the list is empty or not
bool List:: empty()
{
return size == 0;
}