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.
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.
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
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?
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:...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT