Complete the implementation of the class LinkedSortedList, and write a driver program to fully test it. (Ch. 12, Programming Problems 1, pg. 392 of Data Abstraction and Problem Solving with C++ (7th Edition) ) Name the program sortedlist.cpp. Make sure the following requirements are met. Program must compile and run. Must use the Sorted List ADT  SortedListInterface.h    For LinkSortedList pg. 376 has the code for insertSorted. You must complete the implementation. Image shown below: Therefore the LinkedSortedList is not based on a LinkedList but uses its own link-based implementation. Do not use throw on function declarations as it is obsolete. Driver program should: create a sorted list insert 21 random numbers (1-100) using the STL random library Display the numbers as they are inserted. Then remove the first number inserted. Last of all display the sorted list of 20 numbers. No user input for driver program. SortedListInterface.h: //Â Created by Frank M. Carrano and Timothy M. Henry. //Â Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. // Listing 12-1 /** Interface for the ADT sorted list @file SortedListInterface.h */ #ifndef SORTED_LIST_INTERFACE_ #define SORTED_LIST_INTERFACE_ template class SortedListInterface { public: /** Inserts an entry into this sorted list in its proper order so that the list remains sorted. @pre None. @post newEntry is in the list, and the list is sorted. @param newEntry The entry to insert into the sorted list. */ virtual bool insertSorted(const ItemType& newEntry) = 0; /** Removes the first or only occurrence of the given entry from this sorted list. @pre None. @post If the removal is successful, the first occurrence of the given entry is no longer in the sorted list, and the returned value is true. Otherwise, the sorted list is unchanged and the returned value is false. @param anEntry The entry to remove. @return True if removal is successful, or false if not. */ virtual bool removeSorted(const ItemType& anEntry) = 0; /** Gets the position of the first or only occurrence of the given entry in this sorted list. In case the entry is not in the list, determines where it should be if it were added to the list. @pre None. @post The position where the given entry is or belongs is returned. The sorted list is unchanged. @param anEntry The entry to locate. @return Either the position of the given entry, if it occurs in the sorted list, or the position where the entry would occur, but as a negative integer. */ virtual int getPosition(const ItemType& anEntry) const = 0; // The following methods are the same as those given in ListInterface // in Listing 8-1 of Chapter 8 and are completely specified there. /** Sees whether this list is empty. */ virtual bool isEmpty() const = 0; /** Gets the current number of entries in this list. */ virtual int getLength() const = 0; /** Removes the entry at a given position from this list. */ virtual bool remove(int position) = 0; /** Removes all entries from this list. */ virtual void clear() = 0; /** Gets the entry at the given position in this list. */ virtual ItemType getEntry(int position) const = 0; /** Destroys object and frees memory allocated by object. */ virtual ~SortedListInterface() { } }; // end SortedListInterface #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
icon
Related questions
Question
100%

Complete the implementation of the class LinkedSortedList, and write a driver program to fully test it.

(Ch. 12, Programming Problems 1, pg. 392 of Data Abstraction and Problem Solving with C++ (7th Edition) )

Name the program sortedlist.cpp. Make sure the following requirements are met.

  • Program must compile and run.
  • Must use the Sorted List ADT  SortedListInterface.h   
  • For LinkSortedList pg. 376 has the code for insertSorted. You must complete the implementation.

Image shown below:

  • Therefore the LinkedSortedList is not based on a LinkedList but uses its own link-based implementation.
  • Do not use throw on function declarations as it is obsolete.
  • Driver program should:
    • create a sorted list
    • insert 21 random numbers (1-100) using the STL random library
    • Display the numbers as they are inserted.
    • Then remove the first number inserted.
    • Last of all display the sorted list of 20 numbers.
  • No user input for driver program.

SortedListInterface.h:

//Â Created by Frank M. Carrano and Timothy M. Henry. //Â Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. // Listing 12-1 /** Interface for the ADT sorted list @file SortedListInterface.h */ #ifndef SORTED_LIST_INTERFACE_ #define SORTED_LIST_INTERFACE_ template class SortedListInterface { public: /** Inserts an entry into this sorted list in its proper order so that the list remains sorted. @pre None. @post newEntry is in the list, and the list is sorted. @param newEntry The entry to insert into the sorted list. */ virtual bool insertSorted(const ItemType& newEntry) = 0; /** Removes the first or only occurrence of the given entry from this sorted list. @pre None. @post If the removal is successful, the first occurrence of the given entry is no longer in the sorted list, and the returned value is true. Otherwise, the sorted list is unchanged and the returned value is false. @param anEntry The entry to remove. @return True if removal is successful, or false if not. */ virtual bool removeSorted(const ItemType& anEntry) = 0; /** Gets the position of the first or only occurrence of the given entry in this sorted list. In case the entry is not in the list, determines where it should be if it were added to the list. @pre None. @post The position where the given entry is or belongs is returned. The sorted list is unchanged. @param anEntry The entry to locate. @return Either the position of the given entry, if it occurs in the sorted list, or the position where the entry would occur, but as a negative integer. */ virtual int getPosition(const ItemType& anEntry) const = 0; // The following methods are the same as those given in ListInterface // in Listing 8-1 of Chapter 8 and are completely specified there. /** Sees whether this list is empty. */ virtual bool isEmpty() const = 0; /** Gets the current number of entries in this list. */ virtual int getLength() const = 0; /** Removes the entry at a given position from this list. */ virtual bool remove(int position) = 0; /** Removes all entries from this list. */ virtual void clear() = 0; /** Gets the entry at the given position in this list. */ virtual ItemType getEntry(int position) const = 0; /** Destroys object and frees memory allocated by object. */ virtual ~SortedListInterface() { } }; // end SortedListInterface #endif

template<class ItemType>
void LinkedSortedList<ItemType>:insertSorted(const ItemType&
newEntry)
auto newNodePtr(std::make_shared<Node<ItemType>>(newEntry));
auto prevPtr = getNodeBefore(newEntry);
if (isEmpty() || (prevPtr == nullptr)) // Add at beginning
{
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
else
// Add after node before
auto aftPtr = prevPtr->getNext();
newNodePtr->setNext(aftPtr);
prevPtr->setNext(newNodePtr);
} // end if
itemCount++;
return true;
} // end insertSorted
Transcribed Image Text:template<class ItemType> void LinkedSortedList<ItemType>:insertSorted(const ItemType& newEntry) auto newNodePtr(std::make_shared<Node<ItemType>>(newEntry)); auto prevPtr = getNodeBefore(newEntry); if (isEmpty() || (prevPtr == nullptr)) // Add at beginning { newNodePtr->setNext(headPtr); headPtr = newNodePtr; else // Add after node before auto aftPtr = prevPtr->getNext(); newNodePtr->setNext(aftPtr); prevPtr->setNext(newNodePtr); } // end if itemCount++; return true; } // end insertSorted
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps

Blurred answer
Knowledge Booster
Linked List Representation
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education