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

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;

}

};


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 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...
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...
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. 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: ---the code can be general (can be tested with any int main() test function)--- Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: //...
Please fill in the code where it says to implement Methods needed to implement include: insert,...
Please fill in the code where it says to implement Methods needed to implement include: insert, find, remove, and toIndex // // STRINGTABLE.JAVA // A hash table mapping Strings to their positions in the the pattern sequence // You get to fill in the methods for this part. // public class StringTable {    private LinkedList<Record>[] buckets; private int nBuckets; // // number of records currently stored in table -- // must be maintained by all operations // public int...
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...
Fill in the code for the following C functions. Function srl performs a logical right shifting...
Fill in the code for the following C functions. Function srl performs a logical right shifting using arithmetic right shift (given by value xsra), followed by other operations not including right shifts or division. Function sra performs an arithmetic right shift using a logical right shift (given by value xsrl), followed by other operations not including right shift or division. you may use the computation 8*size of (int) to determine w, the number of bits in data type int. The...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT