Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ DATA STRUCTURES I can\'t complete part B of the project (I just need part b)

ID: 3875322 • Letter: C

Question

C++ DATA STRUCTURES

I can't complete part B of the project (I just need part b). I get 6 errors related to the same line (I would highlight and comment in the line that I get the error).

The error I get are:

(14): error C2143: syntax error: missing ';' before '<'
(24): note: see reference to class template instantiation 'UnsortedType<T>' being compiled
(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
h(14): error C2238: unexpected token(s) preceding ';'
1>Source.cpp
(14): error C2143: syntax error: missing ';' before '<'
(24): note: see reference to class template instantiation 'UnsortedType<T>' being compiled
(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(14): error C2238: unexpected token(s) preceding ';'

Program instructions:

An Unsorted Type ADT is to be extended by the addition of function SplitLists, which as the following specifications:

SplitLists(UnsortedType list, ItemType item, UnsortedType& list1. UnsortedType& list2)

Function: Divides
list into two lists according to the key of item.

Preconditions:
list has been initialized and is not empty.

Post conditions: list1 contains all the items of list whose keys are less than or equal to items key;
list2 contains all of the items of
list whose keys are greater than items key.

a. Implement SplitLists as an array-based member function of the Unsorted List ADT.

b. Implement SplitLists as a linked member function of the Unsorted List ADT.

// header file

#pragma once

#include <vector>

template <class T>

class UnsortedType {

private:

       int length;

vector <T> list; // THE ERRORS I GET COME FROM THIS DECLARATION

public:

       UnsortedType(); // constructor

       int getLength(); //get length of list

       T getListElement(int i);

       void putItem(T item);

void show();

};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// SplitList.cpp

#include <iostream>

#include "SplitList.h"

using namespace std;

template <class T>

UnsortedType<T>::UnsortedType()

{

       length = 0;

}

template <class T>

int UnsortedType<T>::getLength()

{

       return this->length;

}

template <class T>

T UnsortedType<T>::getListElement(int i) {

       return list.at(i);

}

template <class T>

void UnsortedType<T>::putItem(T item) {

       list.push_back(item);

       length++;

}

template <class T>

void UnsortedType<T>::show() {

       for (int i = 0; i < length; i++)

              cout << "List is: " << list.at(i) << endl;

      

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include "SplitList.h"

using namespace std;

template <class T>

void splitLists(UnsortedType<T>list, T item,

       UnsortedType<T>&list1, UnsortedType<T>&list2)

{

       for (int i = 0; i < list.getLength(); i++) {

              if (list.getListElement(i) <= item) {

                     list1.putItem(list.getListElement(i));

              }

              else

              {

                     list2.putItem(list.getListElement(i));

              }

       }

}

int main() {

       UnsortedType<double> list;

       cout << "orignal item list:" << endl;

       list.putItem(8);

       list.putItem(6.5);

       list.putItem(9.6);

       list.putItem(4);

       list.putItem(3);

       list.putItem(2);

       list.putItem(10);

       list.show();

       UnsortedType<double> list1;

       UnsortedType < double > list2;

       cout << "Split list in two (list1& list2)" << endl;

       cout << "list 1: " << endl;

       list1.show();

       cout << "list 2:" << endl;

       list2.show();

}

Explanation / Answer

///// File : SplitList.h //////

#pragma once
#include <vector>

using namespace std; // Should be added

template <class T> class UnsortedType
{
private:
int length;
vector <T> list; // THE ERRORS I GET COME FROM THIS DECLARATION
public:
UnsortedType(); // constructor
int getLength(); //get length of list
T getListElement(int i);
void putItem(T item);
void show();
};

////// File : SplitList.cpp ///////

#include <iostream>

#include "SplitList.h"

using namespace std;

template <class T> UnsortedType<T>::UnsortedType()

{

length = 0;

}

template <class T> int UnsortedType<T>::getLength()

{

return this->length;

}

template <class T> T UnsortedType<T>::getListElement(int i)

{

return list.at(i);

}

template <class T> void UnsortedType<T>::putItem(T item)

{

list.push_back(item);

length++;

}

template <class T> void UnsortedType<T>::show() {

for (int i = 0; i < length; i++)

cout << "List is: " << list.at(i) << endl;

  

}

template <class T> void splitLists(UnsortedType<T> list, T item, UnsortedType<T> &list1, UnsortedType<T> &list2)

{

for (int i = 0; i < list.getLength(); i++) {

if (list.getListElement(i) <= item) {

list1.putItem(list.getListElement(i));

}

else

{

list2.putItem(list.getListElement(i));

}

}

}

int main() {

UnsortedType<double> list;

cout << "orignal item list:" << endl;

list.putItem(8);

list.putItem(6.5);

list.putItem(9.6);

list.putItem(4);

list.putItem(3);

list.putItem(2);

list.putItem(10);

list.show();

UnsortedType<double> list1;

UnsortedType<double> list2;

cout << "Split list in two (list1& list2)" << endl;

cout << "list 1: " << endl;

list1.show();

cout << "list 2:" << endl;

list2.show();

}