Question

In: Computer Science

In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...

In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: for the ease of submission, everything is written inside a single file, which can be separated into multiple files easily.

#include <iostream>

#include <vector>

using namespace std;

//template class representing a sorted list

template <class T>

class SortedList {

                //using a vector of type T as underlying data structure

    vector<T> vec;

public:

    SortedList() {}

    //method to add a value in proper position so that elements will be in sorted order

    void add(T value);

    //method to get an element at given position

    T get(int index) const;

    //method to remove an item from given position

    T remove(int index);

    //returns the current size of the list

    int size() const;

    //overloaded friend << operator function to display elements of the list

    template <class T1>

    friend ostream& operator<<(ostream& out, const SortedList<T1>& sortedVec);

};

//implementation of all methods declared inside the class

template <class T>

void SortedList<T>::add(T value)

{

    //if vector is empty, simply adding

    if (vec.empty()) {

        vec.push_back(value);

        //exiting function

        return;

    }

    //if value is less than first element, adding as first element

    if (value < vec[0]) {

        vec.insert(vec.begin(), value);

        return;

    }

    //otherwise, looping through each pair and checking if value can be added

    //to the middle of any pair

    for (int i = 0; i < vec.size() - 1; i++) {

        if (value >= vec[i] && value <= vec[i + 1]) {

            //adding number between current vec[i] and vec[i+1]

            vec.insert(vec.begin() + i + 1, value);

            return;

        }

    }

    //otherwise, adding as last value

    vec.push_back(value);

}

template <class T>

T SortedList<T>::get(int index) const

{

                //assuming index is valid, returning element at given index

    return vec[index];

}

template <class T>

T SortedList<T>::remove(int index)

{              //assuming index is valid, fetching element at given index

    T element = vec[index];

    //removing element at index position

    vec.erase(vec.begin() + index);

    //returning removed value

    return element;

}

template <class T>

int SortedList<T>::size() const

{

                //returning size of vector

    return vec.size();

}

template <typename E>

ostream& operator<<(ostream& out, const SortedList<E>& list)

{

    //looping and printing all values in list space separated

    for (int i = 0; i < list.size(); i++) {

        out << list.vec[i] << " ";

    }

    return out;

}

int main()

{

                //creating a SortedList of integers, adding some values, displaying list

    SortedList<int> list1;

    list1.add(15);

    list1.add(7);

    list1.add(-2);

    list1.add(99);

    list1.add(76);

    cout << "Sorted integer list: " << list1 << endl;

                //creating a SortedList of characters, adding some values, displaying list

    SortedList<char> list2;

    list2.add('e');

    list2.add('a');

    list2.add('d');

    list2.add('b');

    list2.add('c');

    cout << "Sorted char list: " << list2 << endl;

   

  return 0;

}

/*OUTPUT*/

Sorted integer list: -2 7 15 76 99

Sorted char list: a b c d e


Related Solutions

Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
What is an array-based list? What is a resizable list? What is the difference between a...
What is an array-based list? What is a resizable list? What is the difference between a list’s capacity and its size? When a list is expanded, is the size changed or is its capacity changed?
please do all parts a.For resizable array based implementation, what is the time complexity in the...
please do all parts a.For resizable array based implementation, what is the time complexity in the worst and base case scenario? Explain. b. For linked implementation of a list, what is the time complexity in the wort and best case scenario? Explain. remove(givenPosition: integer)
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT