Question: Solve the problem using C++. Make necessary changes in the following codes to complete the task. SortedList.h #ifndef SORTEDLIST_H #define SORTEDLIST_H template class SortedList{ struct

Solve the problem using C++. Make necessary changes in the following codes to complete the task.

SortedList.h

#ifndef SORTEDLIST_H #define SORTEDLIST_H

template class SortedList{ struct Node{ ItemType info; Node* next; };

private: Node* listData; Node* currentPos; int length;

public: SortedList(); ~SortedList();

bool InsertItem(ItemType); bool DeleteItem(ItemType); void MakeEmpty();

bool RetrieveItem(ItemType&); bool GetNextItem(ItemType&); void ResetList(); int LengthIs();

bool IsFull(); bool IsEmpty();

};

#include "SortedList.tpp" #endif

SortedList.tpp

template SortedList::SortedList(){ listData = nullptr; // NULL currentPos = nullptr; // NULL length = 0; }

template SortedList::~SortedList() { MakeEmpty(); }

template bool SortedList::InsertItem(ItemType item) { if(IsFull()) return false; Node* newNode = new Node; newNode->info = item;

Node* predLoc = nullptr; Node* loc = listData;

while(loc != nullptr){ if(loc->info next; } else break; } if(predLoc == nullptr){ newNode->next = listData; listData = newNode; } else{ newNode->next = loc; predLoc->next = newNode; } length++; return true; }

template bool SortedList::DeleteItem(ItemType item) {

if(IsEmpty()) return false;

if(listData->info == item) { Node *temp = listData; listData = listData->next; delete temp; } else{ Node* location = listData; while(item!=(location->next)->info){ location = location -> next; if(location->next == nullptr) return false; }

Node* temp = location->next; location->next = temp->next; delete temp; } length--; return true;

}

template void SortedList::MakeEmpty() { Node* temp; while(listData != nullptr){ temp = listData; listData = listData->next; delete temp; } length = 0; currentPos = nullptr; }

template bool SortedList::RetrieveItem(ItemType& item) {

Node* location = listData; bool found = false;

while(location != nullptr){ if(location->info == item){ found = true; item = location->info; break; } else{ location = location->next; } } return found;

}

template bool SortedList::GetNextItem(ItemType & item) { if(IsEmpty()) return false;

if(currentPos == nullptr) currentPos = listData; else if(currentPos->next == nullptr) return false; else currentPos = currentPos->next;

item = currentPos->info; return true;

}

template void SortedList::ResetList() { currentPos = nullptr; }

template int SortedList::LengthIs() { return length; }

template bool SortedList::IsFull() { try { Node* temp = new Node; delete temp; return false; } catch (std::bad_alloc& e) { return true; } }

template bool SortedList::IsEmpty() { return (length == 0); }

main.cpp

#include #include "SortedList.h"

int main() { std::cout

Solve the problem using C++. Make necessary changes in the following codes

1. Create a public function with function prototype "void Reverse();" in the Sorted List class. When called, this function should reverse the linked list. In the driver file (main.cpp): a. Create a float SortedList object. b. Insert a few items c. Call Reverse function. d. Print the list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!