In: Computer Science
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.
#######################################
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.