Add a new function addorder to the Mlist class that adds a value to the Mlist so
ID: 3736177 • Letter: A
Question
Add a new function addorder to the Mlist class that adds a value to the Mlist so the list remains in non-decreasing order. Hand in your code for the entire Mlist class including the new function. Please help me add the function addorder to the following code. This is a Data Structures Question. Thank you.
#include <iostream>
#include <cassert>
#include <time.h>
using namespace std;
template <typename T>
class Lnode
{
public:
T data; // data type T within the List Node
Lnode *lptr; //left pointer
Lnode *rptr; //right pointer
};
template <typename T>
class Clist
{
public:
Clist(); //default constructor
void add(T x); //add element to the end of the list
void addindex(int i, T x); //adding an element to the list
void del(T x); //deleting a specific element
void delindex(int i);
T getfront(); //returns the front of the list
T getback(); //returns the back of the list
void print(); //print function
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); //gets that yung index
};
template <typename T>
Clist<T>::Clist()
{
first = 0;
last = 0;
lsize = 0;
}
template <typename T>
Lnode<T>* Clist<T>::getindex(int i) //function to scan through the list
{
Lnode<T>* tempptr = first;
for (int j = 0; j < i; j++)
{
tempptr = tempptr -> rptr;
}
return tempptr;
}
template <typename T>
void Clist<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 Clist<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 Clist<T>::del(T x)
{
for (int i = 0; i < lsize; i++)
{
if (x == getindex(i) -> data) //looking at data in node
{
delindex(i);
break;
}
}
}
template <typename T>
void Clist<T>::delindex(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 Clist<T>::getfront()
{
return first->data;
}
template <typename T>
T Clist<T>::getback()
{
return last->data;
}
template <typename T>
void Clist<T>::print()
{
for (int i = 0; i < lsize; i++)
{
cout << getindex(i) -> data << endl;
}
}
template <typename T>
T Clist<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()
{
Clist<int> *clist_ptr = new Clist<int>; //creates the list object
srand(time(NULL));
cout << "Testing add function: " << endl;
for (int i = 0; i < 10; i++) //populates the list
{
int z = (rand() % 101);
clist_ptr -> add(z); //add function in use to populate the list
cout << (*clist_ptr)[i] << endl;
}
cout << "Testing the add index function: " << endl;
clist_ptr -> addindex(5, 75);
clist_ptr -> print();
cout << "Testing the delete function: " << endl;
clist_ptr -> del(75);
clist_ptr -> print();
cout << "Testing the delete index function: " << endl;
clist_ptr -> delindex(8);
clist_ptr -> print();
cout << "Testing the getfront function: " << endl;
cout << clist_ptr -> getfront() << endl;
cout << "Testing the getback function: " << endl;
cout << clist_ptr -> getback() << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cassert>
#include <time.h>
using namespace std;
template <typename T>
class Lnode
{
public:
T data; // data type T within the List Node
Lnode *lptr; //left pointer
Lnode *rptr; //right pointer
};
template <typename T>
class Clist
{
public:
Clist(); //default constructor
void add(T x); //add element to the end of the list
void addindex(int i, T x); //adding an element to the list
void addorder(T x);
void del(T x); //deleting a specific element
void delindex(int i);
T getfront(); //returns the front of the list
T getback(); //returns the back of the list
void print(); //print function
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); //gets that yung index
};
template <typename T>
Clist<T>::Clist()
{
first = 0;
last = 0;
lsize = 0;
}
template <typename T>
Lnode<T>* Clist<T>::getindex(int i) //function to scan through the list
{
Lnode<T>* tempptr = first;
for (int j = 0; j < i; j++)
{
tempptr = tempptr -> rptr;
}
return tempptr;
}
template <typename T>
void Clist<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 Clist<T>::addorder(T x)
{
Lnode<T> *ptr = new Lnode<T>;
Lnode<T> *current = new Lnode<T>;
current=first;
ptr -> data = x;
if (lsize == 0)
{
ptr -> lptr = 0;
ptr -> rptr = 0;
first = ptr;
last = ptr;
}
// if the node is to be inserted at the beginning
// of the doubly linked list
else if (first->data <= ptr->data) {
ptr->rptr = first;
ptr->rptr->lptr = ptr;
first = ptr;
}
else {
current = first;
// locate the node after which the new node
// is to be inserted
while (current->rptr != NULL &&
current->rptr->data < ptr->data)
current = current->rptr;
/* Make the appropriate links */
ptr->rptr = current->rptr;
// if the new node is not inserted
// at the end of the list
if (current->rptr != NULL)
ptr->rptr->lptr = ptr;
current->rptr = ptr;
ptr->lptr = current;
last = current;
}
lsize++;
}
template <typename T>
void Clist<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 Clist<T>::del(T x)
{
for (int i = 0; i < lsize; i++)
{
if (x == getindex(i) -> data) //looking at data in node
{
delindex(i);
break;
}
}
}
template <typename T>
void Clist<T>::delindex(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 Clist<T>::getfront()
{
return first->data;
}
template <typename T>
T Clist<T>::getback()
{
return last->data;
}
template <typename T>
void Clist<T>::print()
{
for (int i = 0; i < lsize; i++)
{
cout << getindex(i) -> data << endl;
}
}
template <typename T>
T Clist<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()
{
Clist<int> *clist_ptr = new Clist<int>; //creates the list object
srand(time(NULL));
cout << "Testing add function: " << endl;
for (int i = 0; i < 10; i++) //populates the list
{
int z = (rand() % 101);
clist_ptr -> addorder(z); //add function in use to populate the list
cout << (*clist_ptr)[i] << endl;
}
cout << "Testing addorder function: " << endl;
for (int i = 0; i < 10; i++) //populates the list
{
int z = (rand() % 101);
clist_ptr -> add(z); //add function in use to populate the list
cout << (*clist_ptr)[i] << endl;
}
cout << "Testing the add index function: " << endl;
clist_ptr -> addindex(5, 75);
clist_ptr -> print();
cout << "Testing the delete function: " << endl;
clist_ptr -> del(75);
clist_ptr -> print();
cout << "Testing the delete index function: " << endl;
clist_ptr -> delindex(8);
clist_ptr -> print();
cout << "Testing the getfront function: " << endl;
cout << clist_ptr -> getfront() << endl;
cout << "Testing the getback function: " << endl;
cout << clist_ptr -> getback() << endl;
return 0;
}
output:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.