Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago