C++

Tenzin N.

asked • 05/07/22

MyLinkedList: MyLinkedList should have all of the functions from MyVector v1, but must store data in a linked list rather than an array.

For this assignment, you will re-implement MyVector as a class called MyLinkedList. MyLinkedList should have all of the functions from MyVector v1, but must store data in a linked list rather than an array.

You also must implement a copy constructor -- a constructor that takes a reference to another MyLinkedList as a parameter, and produces a new MyLinkedList with the same contents as the original. 

You must also implement the following overloaded operators:

operator<< 

operator[]

Note that implementing a full linked list can be tricky! Insertion and deletion from the middle of a linked list can require thinking about a number of edge cases -- you will have to write functions that work when there's 0 items in the list, when there's 1 item in the list, and when there's many items in the list. I very, very, very strongly recommend drawing the list out on paper and thinking through the steps required to insert items, instead of just diving straight in to writing code. 


MyVector v1:


In this assignment you will create a class that partially reimplements the vector class from the C++ standard library. You must do the following;

 

Create a class called MyVector which stores ints in a dynamic array. MyVector should have three constructors:

  1. a default constructor that creates a MyVector that can contain 10 ints, 
  2. a one-parameter constructor that takes one int and creates a MyVector that can contain that number of ints
  3. a two-parameter constructor that takes two ints. The first is used to determine the number of elements the MyVector can initially store, the second is used to initialize those values. (so MyVector(2, 3) creates a two-element array with both elements initialized to 3

MyVector should have the following member functions:

  1. A function called grow which doubles the number of elements your MyVector can store. Call this function when your array needs to grow. This function should be private. 
  2. A function called shrink which halves the number of elements your MyVector can store. Call this function if space for three times as many items are allocated as there are actual items in the MyVector. 
  3. A function called pushBack which adds a new element to the end of the array
  4. A function called popBack which returns the last element of the array and also removes it from the array.
  5. A function called insert which takes two parameters -- an int to insert, and the index to insert it at. If the insertion is past the current end of the array, place it at the end of the array. Shift all elements past the insertion point to accommodate the new item.
  6. A function called remove which takes one parameter (an index) and removes the element at that index. Shift all elements past the removal point down to avoid leaving gaps in your array
  7. A function called at which takes an int as parameter and returns a reference to that index of the array. Print an error message and use exit(0) to end the program if the index is not in the array
  8. Note: since it returns a reference, .at() can be used to both print values in the array and assign values to the array.
  9. A destructor to delete [] the dynamic array when your MyVector goes out of scope.

You will almost certainly have to declare a number of private instance variables to implement this class.




1 Expert Answer

By:

Matthew K. answered • 06/29/22

Tutor
5 (1)

B.A. in Computer Science, Swarthmore College

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.