Extend the class linkedListType by adding the following operations: Provide the
ID: 3532108 • Letter: E
Question
Extend the class linkedListType by adding the following operations:
Provide the definitions of these functions in the class linkedListType. Also, write a program to test these functions. (Use either the class unorderedLinkedList or the class orderedLinkedList to test your function.)
Ok, so my code does compile successfully and there's nothing wrong with it. I cant understand if its actually doing what the question is asking me to do. I mean i should know since its my code but i'm confused as to if its actually finding the kth element or do i need to restart. Some comments would be great alongside my code please. If its not can you please fix it. Thanks
heres the code:
[code]
#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
template <typename T>
class linkedListType : public list<T>{
private:
T data;
public:
bool operator<(const linkedListType<T>& llt)
{
return data < llt.data;
}
bool operator==(const linkedListType<T>& llt)
{
return data == llt.data;
}
void operator=(const linkedListType<T>& llt)
{
data = llt.data;
}
T get_min()
{
return *min_element(this->begin(), this->end());
}
void del_firstmin()
{
T d = get_min();
linkedListType<T>::iterator i;
for (i = begin(); i != end(); i++) {
if (*i == d) {
this->erase(i);
break;
}
}
}
void del_element(int iPos)
{
linkedListType<T>::iterator i;
int iCount = 0;
for (i = begin(); i != end(); i++, iCount++) {
if (iPos == iCount) {
this->erase(i);
break;
}
}
if (iPos != iCount) {
cout << "Element " << iPos << " not in list!" << endl;
}
}
T& get_info(int iPos)
{
linkedListType<T>::iterator i;
int iCount = 0;
for (i = begin(); i != end(); i++, iCount++) {
if (iCount == iPos) {
break;
}
}
if (iCount != iPos) {
system("pause");
exit(0); // terminate program
}
return *i;
}
void splitMid(linkedListType<T>& l)
{
linkedListType<T>::iterator i = begin();
advance(i, size() / 2 + size() %2);
l.splice(l.begin(), *this, i, end() );
}
};
int main()
{
linkedListType<int> llt;
llt.push_back(101);
llt.push_back(200);
llt.push_back(227);
llt.push_back(189);
llt.push_back(52);
copy(llt.begin(), llt.end(), ostream_iterator<int>(cout, " "));
cout << " Deleted the first occurrence of the smallest element: " << llt.get_min() << endl;
llt.del_firstmin();
copy(llt.begin(), llt.end(), ostream_iterator<int>(cout, " "));
cout << " info about element 3: " << llt.get_info(2) << endl;
// if you uncomment the following the program terminates!
// cout << "info about element 10: " << llt.get_info(10);
linkedListType<int> sublist;
llt.splitMid(sublist);
cout << " First List: ";
copy(llt.begin(), llt.end(), ostream_iterator<int>(cout, " "));
cout << " Splitted List: ";
copy(sublist.begin(), sublist.end(), ostream_iterator<int>(cout, " "));
system("pause");
return 0;
}
[code]
Explanation / Answer
LinkedList.h
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.