Question

In: Computer Science

C++ Please Fill in for the functions for the code below. The functions will implement an...

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() {// ... }

};

Solutions

Expert Solution

#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;
}


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. 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: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2 files (table2.h, short story text file) are all fully coded and commented for convenience. *****I have given references that I've completed previously for the table2.cpp file, I just need help applying them. Will provide good rating, thanks****** -------------------------------------------------------------------------------------------------------------------------- table2.h (given file, do not edit): // FILE: table2.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is...
Fill in the blanks in the MATLAB code below.
Fill in the blanks in the MATLAB code below. (Do not type unnecessary words or blank spaces in your response. The correct answers are case-sensitive.) % Consider a row vector v. % Complete the lines of code below to find the average and standard deviation of the elements of vector v such that these two values are assigned to variables M and S, respectively. E = G =
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT