Please 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 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
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Please 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 ItemType>
class SortedList
{
private:
Node<ItemType> *head;
int itemCount;//Current count of list items
Node<ItemType> *getNodeBefore(const ItemType &anEntry)const;
Node<ItemType> *getNodeAt(int position) const; //Return a pointer that hold the address of the node at 'position'
public:
SortedList(); //Default constructor
SortedList(const LinkedSortedList<ItemType> &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<iostream>
#include"Node.h"
#include"SortedList.h"
#include<ctime>
using namespace std;
int main()
{
SortedList<int> aList;
//Test inserSorted() funciton
srand(static_cast<unsigned int>(time(NULL)));
for(int i=0;i<8;i++){
aList.insertSorted(rand()%100);
}
cout<<"================\n";
aList.displayList();
cout<<"================\n";
//Test clear() function
aList.clear();
aList.insertSorted(17);
aList.insertSorted(5);
aList.insertSorted(21);
aList.insertSorted(15);
aList.insertSorted(4);
cout<<"================\n";
aList.displayList();
cout<<"================\n";
//Test removeSorted() function
if(!aList.removeSorted(20)){
cout<<"Removal failed!\n";
}
if(!aList.removeSorted(15)){
cout<<"Removal failed!\n";
}
cout<<"================\n";
aList.displayList();
cout<<"================\n";
if(!aList.removeSorted(4)){
cout<<"Removal failed!\n";
}
if(!aList.removeSorted(21)){
cout<<"Removal failed!\n";
}
cout<<"================\n";
aList.displayList();
cout<<"================\n";
//Test getPosition() function
cout<<"The postion of 25 is "<<aList.getPosition(25)<<endl;
cout<<"The postion of 2 is "<<aList.getPosition(2)<<endl;
cout<<"The postion of 11 is "<<aList.getPosition(11)<<endl;
return 0;
}
//header file for the template class Node
#ifndef _NODE
#define _NODE
template<class ItemType>
class Node
{
private:
ItemType item;
Node<ItemType> *next;
public:
Node(); //default constructor
Node(const ItemType &anItem);//none default constructor
Node(const ItemType &anItem, Node<ItemType> *nextPtr); //none default constructor
void setItem(const ItemType &anItem);
void setNext(Node<ItemType> *nextPtr);
ItemType getItem() const;
Node<ItemType> *getNext() const;
};
template<class ItemType>
Node<ItemType>::Node()
{
next = NULL;
}
template<class ItemType>
Node<ItemType>::Node(const ItemType &anItem)
{
item = anItem;
next = NULL;
}
template<class ItemType>
Node<ItemType>::Node(const ItemType &anItem, Node<ItemType> *nextPtr)
{
item = anItem;
next = nextPtr;
}
template<class ItemType>
void Node<ItemType>::setItem(const ItemType &anItem)
{
item = anItem;
}
template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType> *nextPtr)
{
next = nextPtr;
}
template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
return item;
}
template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
return next;
}
#endif
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education