Complete the ADT SortedList by finishing all the constructors and methods in the class template SortedList.h (C++)
Question:
Complete the ADT SortedList by finishing all the constructors and methods in the class template SortedList.h (C++)
SortedList.h:
#ifndef _SORTED_LIST
#define _SORTED_LIST
#include"Node.h"
template
class SortedList
{
private:
Node *head;
int itemCount;//Current count of list items
Node *getNodeBefore(const ItemType &anEntry)const;
Node *getNodeAt(int position) const; //Return a pointer that hold the address of the node at 'position'
public:
SortedList(); //Default constructor
SortedList(const LinkedSortedList &aList); //Copy constructor
bool isEmpty() const; //Check if the list empty (no item stored in the array)
int getLength() const; //Returns the number of items in the list
bool remove(int position); //Remove an entry at given position from the list
void clear(); //remove all the items from the list
ItemType getEntry(int position) const; //Retrieves an item on the list at the given position
void displayList();
//Following are three new methods:
void insertSorted(const ItemType &newEntry);
bool removeSorted(const ItemType &anEntry);
int getPosition(const ItemType &newEntry) const;
};
//Complete all the constructors and methods here:
#endif
Please download the following source file and use it as a testing program to make sure all the member functions in your class template are working correctly before submitting your header file.
source file for testing the class template:
#include
#include"Node.h"
#include"SortedList.h"
#include
using namespace std;
int main()
{
SortedList aList;
//Test inserSorted() funciton
srand(static_cast(time(NULL)));
for(int i=0;i<8;i++){
aList.insertSorted(rand()%100);
}
cout<<"================";
aList.displayList();
cout<<"================";
//Test clear() function
aList.clear();
aList.insertSorted(17);
aList.insertSorted(5);
aList.insertSorted(21);
aList.insertSorted(15);
aList.insertSorted(4);
cout<<"================";
aList.displayList();
cout<<"================";
//Test removeSorted() function
if(!aList.removeSorted(20)){
cout<<"Removal failed!";
}
if(!aList.removeSorted(15)){
cout<<"Removal failed!";
}
cout<<"================";
aList.displayList();
cout<<"================";
if(!aList.removeSorted(4)){
cout<<"Removal failed!";
}
if(!aList.removeSorted(21)){
cout<<"Removal failed!";
}
cout<<"================";
aList.displayList();
cout<<"================";
//Test getPosition() function
cout<<"The postion of 25 is "<
cout<<"The postion of 2 is "<
cout<<"The postion of 11 is "<
return 0;
}
//header file for the template class Node
#ifndef _NODE
#define _NODE
template
class Node
{
private:
ItemType item;
Node *next;
public:
Node(); //default constructor
Node(const ItemType &anItem);//none default constructor
Node(const ItemType &anItem, Node *nextPtr); //none default constructor
void setItem(const ItemType &anItem);
void setNext(Node *nextPtr);
ItemType getItem() const;
Node *getNext() const;
};
template
Node::Node()
{
next = NULL;
}
template
Node::Node(const ItemType &anItem)
{
item = anItem;
next = NULL;
}
template
Node::Node(const ItemType &anItem, Node *nextPtr)
{
item = anItem;
next = nextPtr;
}
template
void Node::setItem(const ItemType &anItem)
{
item = anItem;
}
template
void Node::setNext(Node *nextPtr)
{
next = nextPtr;
}
template
ItemType Node::getItem() const
{
return item;
}
template
Node* Node::getNext() const
{
return next;
}
#endif