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.
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?
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...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT