Question

In: Computer Science

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.

Solutions

Expert Solution

#######################################
            main.cpp
#######################################
#include <iostream>
#include"sortedList.h"

using namespace std;

void showMenu();

int main() {
        char choice;

        showMenu();
        cin >> choice;

        SortedList myList;

        while(choice != 'q') {

                if(choice == 'a') {
                        int num;

                        cout << "Enter a number: ";
                        cin >> num;

                        myList.insertItem(num);
                }

                else if(choice == 'q') {
                        cout << "Goodbye ..." << endl;
                } else {
                        cout << "Invalid selection! Please try again." << endl;
                }

                cout << endl;
                myList.show();
                showMenu();
                cin >> choice;
        }
}

//Function to print the menu to the user
void showMenu() {
        cout << "\nChoose from the options below." << endl
        << "------------------------------" << endl
        << "a. Insert a number in the list." << endl
        << "q. Quit the program" << endl
        << "------------------------------" << endl
        << "Enter your choice: ";
}



#######################################
      sortedList.cpp
#######################################
#include<iostream>
#include "sortedList.h"

using namespace std;

SortedList::SortedList() {
        numbers.reserve(2);
        length = 0;
}

void SortedList::insertItem(int item) { 
        if(length == numbers.size()) {
                numbers.reserve(length * 2);
        }

        int i = length-1;
        while(i >= 0 && numbers[i] > item) {
                numbers[i+1] = numbers[i];
                i--;
        }
        numbers[i + 1] = item;
        length++;
}

void SortedList::show() {
        cout<<"The list elements are: ";
        for(int i=0; i < length; i++)
                cout << numbers[i] << " ";
        cout<< endl;
}



#######################################
        sortedList.h
#######################################
#ifndef SORTEDLIST_H
#define SORTEDLIST_H

#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

class SortedList {

        private:
                vector<int> numbers;
                int length;
                
        public:
                SortedList();
                void insertItem(int item);
                void deleteItem(int item);
                void show();// To see the list
};

#endif


**************************************************

please ask if any issues.


Related Solutions

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.
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
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?
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...
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...
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...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement: 1)Object get(int), which takes an int index and returns the Object at that index 2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference Both your...
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 :)
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT