Using C++ In this lab we are going to finish the Mlist class, and begin to devel
ID: 3919625 • Letter: U
Question
Using C++
In this lab we are going to finish the Mlist class, and begin to develop and implement the template class Mtree.
The interface is here:
#include
using namespace std;
template
class Lnode
{
public :
T data;
Lnode *lptr;
Lnode *rptr;
};
template
class Mlist
{ public :
Mlist();
void add(T x);
void add(,int i,T x);
void del(T x);
void delete(int i);
T getfront();
T getback();
T operator[] (unsigned int i);
private :
Lnode *first;
Lnode *last;
int lsize;
};
Add the following functions to the Mlist class.
void add(,int i,T x);
void delete(int i);
T getback();
T operator[] (unsigned int i);
Write the functions and test them with a main program.
Next create the interface for the class Mtree along with function stubs. The interface is here:
#include
using namespace std;
template
class Tnode
{
public :
T data;
Tnode *lptr;
Tnode *rptr;
};
template
class Mtree
{ public :
Mtree();
void add(T x);
bool find(T x);
private :
Tnode *root;
int tsize;
};
Add stubs for each member function and test the compilation (c++ -c).
Write the functions and test the compilation.
Write a main program that tests the class functions. (Use the pseudo-random number generator to create a list and add and remove elements from the list
Explanation / Answer
//Answer for Mlist question
#include <iostream>
#include <cassert>
#include <time.h>
using namespace std;
template <typename T>
class Lnode
{
public:
T data;
Lnode *lptr;
Lnode *rptr;
};
template <typename T>
class Mlist
{
public:
Mlist();
void add(T x);
void addIndex(int i, T x); //Have renamed from add() to addIndex to differentiate with the above add
void del(T x);
void deleteIndex(int i); // was delete in the question, delete is a reserved keyword. Renaming this interface to deleteIndex
T getfront();
T getback();
void print(); // Adding Extra print function to print the Mlist
T operator[](unsigned int i);
typedef Lnode<T>* iterator;
iterator front() {return first;}
iterator back() {return last;}
private:
Lnode<T> *first;
Lnode<T> *last;
int lsize;
Lnode<T>* getIndex(int i); //This doesn't exist in the question, adding it to make it easier to search node by given index
};
template <typename T>
Mlist<T>::Mlist()
{
first = 0;
last = 0;
lsize = 0;
}
template <typename T>
Lnode<T>* Mlist<T>::getIndex(int i)
{
Lnode<T>* tempptr = first;
for (int j = 0; j < i; j++)
{
tempptr = tempptr -> rptr;
}
return tempptr;
}
template <typename T>
void Mlist<T>::add(T x)
{
Lnode<T> *ptr = new Lnode<T>;
ptr -> data = x;
if (lsize == 0)
{
ptr -> lptr = 0;
ptr -> rptr = 0;
first = ptr;
last = ptr;
}
else
{
ptr -> lptr = last;
ptr -> rptr = 0;
last -> rptr = ptr;
last = ptr;
}
lsize++;
}
template <typename T>
void Mlist<T>::addIndex(int index, T x)
{
Lnode<T> *ptr = new Lnode<T>;
if (index != lsize)
{
Lnode<T>* tempptr = getIndex(index);
tempptr -> lptr = ptr;
ptr -> rptr = tempptr;
}
else
{
last = ptr;
last -> rptr = 0;
}
if (index != 0)
{
Lnode<T>* tempptr2 = getIndex(index - 1);
tempptr2 -> rptr = ptr;
ptr -> lptr = tempptr2;
}
else
{
first = ptr;
first -> lptr = 0;
}
ptr -> data = x;
lsize++;
}
template <typename T>
void Mlist<T>::del(T x)
{
for (int i = 0; i < lsize; i++)
{
if (x == getIndex(i) -> data)
{
deleteIndex(i);
break;
}
}
}
template <typename T>
void Mlist<T>::deleteIndex(int index)
{
Lnode<T> *tempafter;
Lnode<T> *tempbefore;
Lnode<T> *tempcurrent = getIndex(index);
if (index != lsize - 1)
{
tempafter = getIndex(index + 1);
}
else
{
last = getIndex(index - 1);
last -> rptr = 0;
}
if (index != 0)
{
tempbefore = getIndex(index - 1);
}
else
{
first = getIndex(index + 1);
first -> lptr = 0;
}
delete tempcurrent;
if (index > 0 and index < lsize - 1)
{
tempbefore -> rptr = tempafter;
tempafter -> lptr = tempbefore;
}
lsize--;
}
template <typename T>
T Mlist<T>::getfront()
{
return first->data;
}
template <typename T>
T Mlist<T>::getback()
{
return last->data;
}
template <typename T>
void Mlist<T>::print()
{
for (int i = 0; i < lsize; i++)
{
cout << getIndex(i) -> data << endl;
}
}
template <typename T>
T Mlist<T>::operator[](unsigned int i)
{
assert (lsize > 0);
Lnode<T> *ptr;
ptr = first;
for(int j = 0; j < i; j++)
{
ptr = ptr -> rptr;
}
return ptr -> data;
}
int main()
{
//creating the list object
Mlist<int> *mlist_ptr = new Mlist<int>;
srand(time(NULL));
cout << "Testing the add() method: " << endl;
//populating the list with random values
for (int i = 0; i < 12; i++) {
int z = (rand() % 50);
mlist_ptr -> add(z);
cout << (*mlist_ptr)[i] << endl;
}
cout << "Testing the addIndex() method: " << endl;
mlist_ptr -> addIndex(5, 75);
mlist_ptr -> print();
cout << "Testing the del() method: " << endl;
mlist_ptr -> del(75);
mlist_ptr -> print();
cout << "Testing the deleteIndex() method: " << endl;
mlist_ptr -> deleteIndex(8);
mlist_ptr -> print();
cout << "Testing the getfront() method: " << endl;
cout << mlist_ptr -> getfront() << endl;
cout << "Testing the getback() method: " << endl;
cout << mlist_ptr -> getback() << endl;
return 0;
}
//Output
$g++ -std=c++11 -o main *.cpp
$main
Testing the add() method:
18
46
21
34
8
31
10
2
15
32
32
45
Testing the addIndex() method:
18
46
21
34
8
75
31
10
2
15
32
32
45
Testing the del() method:
18
46
21
34
8
31
10
2
15
32
32
45
Testing the deleteIndex() method:
18
46
21
34
8
31
10
2
32
32
45
Testing the getfront() method:
18
Testing the getback() method:
45
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.