Please use this header file to build the sorted list. #ifndef SORTEDLIST_H #defi
ID: 3674275 • Letter: P
Question
Please use this header file to build the sorted list.
#ifndef SORTEDLIST_H
#define SORTEDLIST_H
#define MAX_ITEMS 100
class SortedList{
public: // 8 public member functions
SortedList(); // constructor
~SortedList(); // destructor
// transformers
void MakeEmpty();
int InsertItem(int item);
void DeleteItem(int item);
// observers
bool IsFull() const;
int GetLength() const; // returns length of list
int RetrieveItem(int index); // return the item on the index-th item, starting from 0
int findItem(int item); // return the index of the first item if found; return -1 if not found
// iterators
void ResetList();
void GetNextItem(int& item);
private: // 3 private data members
int length; // the current number of items in the list
//int info[MAX_ITEMS];
int *info;
int currentPos;
int capacity; // the capacity of the list
};
#endif
Sorted List is a basic data structure that holds a sorted list of items in the ascending order, you need to implement all functions in the defined . read the comments on the header file and complete them using C++. You need to implement all of the SortedList ADT functions in SortedList.cpp file, and test them in main.cpp.
Explanation / Answer
SortedList.h
#ifndef SORTEDLIST_H
#define SORTEDLIST_H
#define MAX_ITEMS 100
class SortedList{
public: // 8 public member functions
SortedList(); // constructor
~SortedList(); // destructor
// transformers
void MakeEmpty();
int InsertItem(int item);
void DeleteItem(int item);
// observers
bool IsFull();
int GetLength(); // returns length of list
int RetrieveItem(int index); // return the item on the index-th item, starting from 0
int findItem(int item); // return the index of the first item if found; return -1 if not found
// iterators
void ResetList();
void GetNextItem(int& item);
private: // 3 private data members
int length; // the current number of items in the list
//int info[MAX_ITEMS];
int *info;
int currentPos;
int capacity; // the capacity of the list
};
#endif
SortedList.cpp
#include "SortedList.h"
#include <iostream>
#include <climits>
using namespace std;
SortedList::SortedList() {
length = 0;
currentPos = 0;
// set default capacity
capacity = MAX_ITEMS;
info = new int[capacity];
}
SortedList::~SortedList() {
delete[] info;
}
void SortedList::MakeEmpty() {
length = 0;
}
int SortedList::InsertItem(int item) {
if (length == capacity) {
cout << "List is already full!" << endl;
return -1;
}
int i = 0;
while (i < length && info[i] <= item) {
++i;
}
for (int j = length - 1; j >= i; --j) {
info[j + 1] = info[j];
}
info[i] = item;
length += 1;
return i;
}
void SortedList::DeleteItem(int item) {
int i = 0;
while (i < length && info[i] != item) ++i;
while (i + 1 < length) info[i] = info[i + 1];
}
bool SortedList::IsFull() {
return length == capacity;
}
int SortedList::GetLength() {
return length;
}
int SortedList::RetrieveItem(int index) {
if (index < 0 || index >= length) return -1;
else return info[index];
}
int SortedList::findItem(int item) {
for (int i = 0; i < length; ++i) {
if (info[i] == item) return i;
}
return -1;
}
void SortedList::ResetList() {
length = 0;
}
void SortedList::GetNextItem(int &item) {
if (currentPos >= length) item = INT_MAX;
else item = info[currentPos++];
}