Question: 1 3 . 8 LAB: Ordered lists NEED IN C + + PLEASE MAKE IT PASS UNIT TESTS AND EDIT THE SAMPLE CODE An OrderedList
LAB: Ordered lists NEED IN C PLEASE MAKE IT PASS UNIT TESTS AND EDIT THE SAMPLE CODE
An OrderedList is a vector that keeps elements in sorted order.
Complete template class OrderedList by defining the following functions:
int Size
Return the size of the list
TheType Atint index
Return the element of the list at parameter index.
int FindTheType value
Return the index of the first element in the list equal to parameter value.Return if parameter value is not found in the list.
bool RemoveTheType value
Search the list for parameter value. Hint: Use FindIf parameter value is found in the list, remove the element found by moving the subsequent elements towards the beginning of the list. Decrement list size and return true.Return false if parameter value is not found in the list.
Hint: Use any vector functions to simplify the implementations.
The template code provides the implementations of the following functions:
void InsertTheType value
Search the list for an element that is greater than parameter value.If an element is found, increment list size and move the element and all subsequent elements towards the end of the list to make room for parameter value. Copy parameter value at the location that was occupied by the first element greater than parameter value.If no such element is found, increment list size and add parameter value at the end of the list.
void Print
Output the list, separated by a space character.
template code:
#include
#include
#include
using namespace std;
Template class OrderedList declaration
template class OrderedList
public:
int Size; Number of elements in the list
TheType Atint index; Return the element at index
int FindTheType value; Return index of first occurrence
of value or if not found
void InsertTheType value; Insert value at its sorted index
bool RemoveTheType value; Find the first occurrence of value
and remove the value; true if success
void Print;
private:
vector list; Elements are stored in list
;
OrderedList function implementations
template
int OrderedList::Size
Type your code here.
template
TheType OrderedList::Atint index
Type your code here.
template
int OrderedList::FindTheType value
Type your code here.
template
void OrderedList::InsertTheType newItem
unsigned int j; Vector size is unsigned int
unsigned int k; Vector size is unsigned int
if listsize
list.pushbacknewItem;
return;
j ;
while j list.size && newItem list.atj
j;
listresizelistsize;
Now all items after index j are newItem
if j list.size
If newItem is last element, just add at end of list
list.pushbacknewItem;
else
Now move backwards from the end of the list copying elements to
the next higher position; stop at j where newItem will go
for k list.size; k j; k
listatk list.atk;
finally, insert newItem
list.atk newItem;
NOTE: Uses Find
template
bool OrderedList::RemoveTheType oldItem
unsigned int j;
int indx FindoldItem;
Type your code here.
template
void OrderedList::Print
for int j ; j Size; j
cout list.atj;
if j Size
cout ; No end line after last element
End OrderedList function implementations
Demonstration of functions
int main
OrderedList intList;
intListInsert;
intListInsert;
intListInsert;
if intListSize
cout "Size is correct" endl;
else
cout "Size should be endl;
if intListAt && intList.At && intList.At
cout "List is in correct order: ;
else
cout "List is out of order: ;
intListPrint;
cout endl;
int indx intList.Find;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
