Question: Need help with this code, I am receiving a variable type is an abstract class and need help resolving. Below is my code for all
Need help with this code, I am receiving a variable type is an abstract class and need help resolving. Below is my code for all my headers and main.cpp //main.cpp #include
using namespace std;
int main() {
cout
cout
CountSelfOrderedList
c.find('F'); c.find('D'); c.find('F'); c.find('G'); c.find('E'); c.find('G'); c.find('F'); c.find('A'); c.find('D'); c.find('F'); c.find('G'); c.find('E'); c.find('H'); c.find('I');
cout
MTFSelfOrderedList
mtf.find('F'); mtf.find('D'); mtf.find('F'); mtf.find('G'); mtf.find('E'); mtf.find('G'); mtf.find('F'); mtf.find('A'); mtf.find('D'); mtf.find('F'); mtf.find('G'); mtf.find('E'); mtf.find('H'); mtf.find('I');
cout
TransposeSelfOrderedList
t.find('F'); t.find('D'); t.find('F'); t.find('G'); t.find('E'); t.find('G'); t.find('F'); t.find('A'); t.find('D'); t.find('F'); t.find('G'); t.find('E'); t.find('H'); t.find('I');
cout
cout
cout
// open file fstream input; input.open("test.txt");
CountSelfOrderedList
string word;
// gets file and places into lists while (input >> word) { cs.find(word); mtfs.find(word); ts.find(word); }
// count print cout
// move-to-front print cout
// transpose print cout
input.close(); system("pause");
return 0; }
--------------------------------------------------------------------------------------- //CountSelfOrderedList.h #include "SelfOrderedListADT.h" #include "llist.h"
using namespace std;
#ifndef COUNTSELFORDEREDLIST_H #define COUNTSELFORDEREDLIST_H
template
//Count list constructor, set compares to zero CountSelfOrderedList() {numOfCompares = 0;}
~CountSelfOrderedList() {}
//Function for finding a user given value bool find(const E& it) {
//Variable for triggering parts of code int found = 0;
//Move the list to the start list.moveToStart();
//Loop through the list checking for duplicate values for (int i = 0; i
//If there is a duplicate found break out of the loop if (list.getValue() == it) {
found = 1; break; }
//Advance down the list list.next();
}
//Increments the number of times accessed if a duplicate value is found if (found == 1) { //Grab current amount of accesses int temp = list.getTimesAccessed();
//Set the new number of accesses list.setTimesAccessed(temp + 1);
//Call the reorder function reorder(); return true; }
else { list.moveToEnd(); list.insert(it); list.setTimesAccessed(0); return true; }
return false; }
//Function to add a value to the end of the list void add(const E& it) { list.append(it); }
//Function to print the list void printlist() const { cout
}
//Same as above, but will only print a given number of values void printlist(int n) const { cout
//Function to return the number of compares int getCompares() const { return numOfCompares; }
int size() const { return list.length(); }
//Function to reorder the items based on their number of accesses void reorder() {
//Grab the number of accesses for the item you wish to swap int toSwap = list.getTimesAccessed();
//Go backwards in the list list.prev();
//Grab the number of accesses for the previous neighbor int prev = list.getTimesAccessed();
//Loop to swap elements until the number of accesses of the current item is less than the previous element while (toSwap > prev) { list.next();
//Remove the item E temp = list.remove();
//Set the number of accesses for the item that was shifted down list.setTimesAccessed(prev); list.prev();
//Insert the removed item list.insert(temp);
//Set the correct number of accesses for the newly inserted item list.setTimesAccessed(toSwap);
//Go forward and set the correct number of accesses list.next(); list.setTimesAccessed(prev);
//Go back two in the list and grab the number of accesses list.prev(); list.prev(); prev = list.getTimesAccessed(); }
}
private: CountSelfOrderedList(const CountSelfOrderedList&) {} //CountSelfOrderedList operator=(const CountSelfOrderedList&) {} LList
#endif ----------------------------------------------------------------------------------------- //book.h #ifndef BOOK_H #define BOOK_H
#include
using namespace std;
// Now all the standard names that we use using std::cout; using std::endl; using std::string; using std::ostream;
const int defaultSize = 10;
// Return true iff "x" is even inline bool EVEN(int x) { return (x % 2) == 0; }
// Return true iff "x" is odd inline bool ODD(int x) { return (x % 2) != 0; }
void Assert(bool val, string s) { if (!val) { cout
// Swap two elements in a generic array template
inline void Randomize() { srand(1); }
// Return a random value in range 0 to n-1 inline int Random(int n) { return rand() % (n); }
// Swap two integers inline void swap(int& i, int& j) { int temp = i; i = j; j = temp; }
// Swap two char*'s inline void swap(char* i, char* j) { char* temp = i; i = j; j = temp; }
// Big enough for simple testing #define INFINITY 9999
// Timing variables and functions unsigned tstart = 0;
// Initialize the program timer void Settime() { tstart = (unsigned) clock(); }
// Return the elapsed time since the last call to Settime double Gettime() { unsigned tcurr = (unsigned) clock(); return (double)(tcurr - tstart)/(double)CLOCKS_PER_SEC; }
class Int { private: int val; public: Int(int input=0) { val = input; }
int key() const { return val; } Int operator= (int input) { val = input; return val; } };
// Let us print out Ints easily ostream& operatorkey(); }
#endif
-------------------------------------------------------------------------- //link.h #ifndef LINK_H #define LINK_H
template
//Function to set the number of accesses void setAccessed(int n) { timesAccessed = n; }
//Function to return the number of accesses int getAccessed() { return timesAccessed; } };
#endif ------------------------------------------------------------------- //list.h #ifndef LIST_H #define LIST_H
template
virtual void clear() = 0;
virtual void insert(const E& item) = 0;
virtual void append(const E& item) = 0; virtual E remove() = 0;
virtual void moveToStart() = 0;
virtual void moveToEnd() = 0;
virtual void prev() = 0;
virtual void next() = 0;
virtual int length() const = 0;
virtual int currPos() const = 0;
virtual void moveToPos(int pos) = 0;
virtual const E& getValue() const = 0;
virtual int getTimesAccessed() const = 0; };
#endif ---------------------------------------------------------------- //llist.h #include "list.h" #include "link.h" #include "book.h" using namespace std;
#ifndef LLIST_H #define LLIST_H
template
void init() { curr = tail = head = new Link
void removeall() { while(head != NULL) { curr = head; head = head->next; delete curr; } }
public: LList() { init(); } ~LList() { removeall(); } //void print() const; void clear() { removeall(); init(); }
// Insert "it" at current position void insert(const E& it) { curr->next = new Link void append(const E& it) { // Append "it" to list tail = tail->next = new Link // Remove and return current element E remove() { //Assert(curr->next != NULL, "No element"); E it = curr->next->element; Link void moveToStart() { curr = head; } void moveToEnd() { curr = tail; } // Move curr one step left; no change if already at front void prev() { if (curr == head) return; Link // Move curr one step right; no change if already at end void next() { if (curr != tail) curr = curr->next; } int length() const { return cnt; } // Return the position of the current element int currPos() const { Link // Move down list to "pos" position void moveToPos(int pos) { Assert ((pos>=0)&&(posnext; } const E& getValue() const { Assert(curr->next != NULL, "No value"); return curr->next->element; } //Function to set the number of accesses for an item void setTimesAccessed(int n) { curr->setAccessed(n); } //Return the current number of accesses for a link int getTimesAccessed() const { return curr->getAccessed(); } //Return the tail value from the list E tailValue() { return tail->element; } //Return the front value from the list E frontValue() { return head->next->element; } //Function to print the list void print() const { //Temp pointer to head next Link //Temp pointer to head Link //Loop to go through and print all items in the list for (int i = 0; i element getAccessed(); temp = temp->next; temp2 = temp2->next; } } //Same as above, but the loop is for a specific number as specified by n void print(int n) const { Link #endif ---------------------------------------------------------------------------- //MTFSelfOrderedList.h #include "SelfOrderedListADT.h" #include "llist.h" #include using namespace std; #ifndef MTFSELFORDEREDLIST_H #define MTFSELFORDEREDLIST_H template //MTF Constructor, set the number of compares to zero MTFSelfOrderedList() {numOfCompares=0;}; ~MTFSelfOrderedList() {}; //Function to find a value in the list bool find(const E& it) { //Variable to determine whether code proceeds or not int found = 0; //Move the list to the start list.moveToStart(); //Loop to determine if there are duplicate values in the list or not for (int i = 0; i //If a duplicate is found set the found variable to one and break out of the loop if (list.getValue() == it) { found = 1; break; } //Advance the list list.next(); } //If found equals one then increment the number of accesses for the current item if (found == 1) { //Get the current number of accesses int temp = list.getTimesAccessed(); //Set the new number of accesses list.setTimesAccessed(temp + 1); //Call the reorder function reorder(); return true; } //Otherwise add the item to the end of the list else { list.moveToEnd(); list.insert(it); return true; } return false; } //Function to call the list append function and pass it the element void add(const E& it) { list.append(it); } //Function to reorder the elements by moving them to the front when they are accessed void reorder() { //Get the number of accesses for the item to be swapped int toSwap = list.getTimesAccessed(); //Advance the list list.next(); //Get the number of accesses for the next item int next = list.getTimesAccessed(); //Go back to the item to be swapped list.prev(); //Remove the item E temp = list.remove(); //Correct the number of accesses list.setTimesAccessed(next); //Move the list to the start list.moveToStart(); //Get the number of accesses for the front item int front = list.getTimesAccessed(); //Insert the removed item list.insert(temp); //Set the correct number of accesses for the new item list.setTimesAccessed(toSwap); //Advance the list and set the correct number of accesses for the item list.next(); list.setTimesAccessed(front); } //Function to print the list void printlist() const { cout //Call the list print function list.print(); } //Function to print a specified number of items in the list void printlist(int n) const { cout //Call the list print function, but specify a number of items to print list.print(n); } //Function to return the number of compares int getCompares() const { return numOfCompares; } //Function to return the list size int size() const { return list.length(); } private: MTFSelfOrderedList(const MTFSelfOrderedList&) {} //MTFSelfOrderedList operator=(const MTFSelfOrderedList&) {} LList #endif ---------------------------------------------------------------------------------- //SelfOrderedList.h #ifndef SELFORDEREDLISTADT_H #define SELFORDEREDLISTADT_H template virtual bool find(const E& it) = 0; virtual void add(const E& it) = 0; virtual int getCompares() const = 0; virtual int size() const = 0; //Print the list virtual void printlist() const = 0; virtual void printlist(int n) const = 0; }; #endif /* SELFORDEREDLISTADT_H */ ----------------------------------------------------------------------- // TransposeSelfOrderedList.h #include "SelfOrderedListADT.h" #include "llist.h" using namespace std; #ifndef TRANSPOSESELFORDEREDLIST_H #define TRANSPOSESELFORDEREDLIST_H template //Transpose constructor, set the number of comapres to zero TransposeSelfOrderedList() {numOfCompares = 0;} ~TransposeSelfOrderedList() {} //Function to find a specified element in the list bool find(const E& it) { //Variable to determine whether to continue in the code or not int found = 0; //Move the list to the start list.moveToStart(); //Loop to determine whether there are duplicates in the list for (int i = 0; i //If there is a duplicate found, set the found flag to one and break out of the loop if (list.getValue() == it) { found = 1; break; } //Advance the list list.next(); } //If there is a duplicate increment the number of times accessed for that element and call the reorder function if (found == 1) { //Get the current number of item accesses int temp = list.getTimesAccessed(); //Set the new number of item accesses list.setTimesAccessed(temp + 1); //Call the reorder function reorder(); return true; } //Otherwise, add the element to the end of the list and set the number of accesses to zero else { list.moveToEnd(); list.insert(it); list.setTimesAccessed(0); return true; } return false; } //Function to call the list append function void add(const E& it) { list.append(it); } //Function to print the list void printlist() const { cout } //Function to reorder the elements by swapping them with the element previous void reorder() { //Grab the first value in the list E frontVal = list.frontValue(); //Grab the last value in the list E endValue = list.tailValue(); //If the current value matches the front value then stop the function if (list.getValue() == frontVal) { return; } //If the current value matches the value at the end of the list if (list.getValue() == endValue) { //Grab the access number for the end of the list int endValue = list.getTimesAccessed(); //Go back one in the list list.prev(); //Grab the access number for the previous element int prevValue = list.getTimesAccessed(); //Remove the previous element E temp = list.remove(); //Set the correct access value list.setTimesAccessed(prevValue); //Move the list to the end list.moveToEnd(); //Insert the removed value list.insert(temp); //Set the correct number of accesses list.setTimesAccessed(prevValue); //Go back one in the list list.prev(); //Set the correct number of accesses list.setTimesAccessed(endValue); return; } //Grab the number accesses of the item to be swapped int toSwap = list.getTimesAccessed(); //Step back one in the list list.prev(); //Get the number of accesses for the previous item int prev = list.getTimesAccessed(); //Go back to the element to be swapped list.next(); //Grab the number of accesses for that value int curr = list.getTimesAccessed(); //Advance down the list list.next(); //Grab the number of accesses for the element after the one to be swapped int next = list.getTimesAccessed(); //Go back to the element to be removed list.prev(); //Remove the element E temp = list.remove(); //Set the correct number of accesses for the next value list.setTimesAccessed(next); //Go backwards in the list list.prev(); //Grab the number of accesses prev = list.getTimesAccessed(); //Insert the removed value list.insert(temp); //Set the correct number of accesses for removed value list.setTimesAccessed(toSwap); //Advance down the list and set the correct number of accesses list.next(); list.setTimesAccessed(prev); } //Function to print a specified number of items from the list void printlist(int n) const { cout //Function to return the number of compares int getCompares() const { return numOfCompares; } //Function to return the size of the list int size() const { return list.length(); } private: TransposeSelfOrderedList(const TransposeSelfOrderedList&) {} //TransposeSelfOrderedList operator=(const TransposeSelfOrderedList&) {} LList }; #endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
