*******C++ *********** Create a new class orderedLList that will insert items in
ID: 657580 • Letter: #
Question
*******C++ ***********
Create a new class orderedLList that will insert items in the list in ascending order. Inherit the linked list you've already created. I have no idea where to even begin with this one, any help is appreciated!
Here is my linked list that I have already created:
#include "stdafx.h"
#include<iostream>
using namespace std;
template <class datatype>
class llNode{
public:
datatype datavalue;
llNode *Next;
};
template <class datatype>
class cLinkedList{
public:
cLinkedList();
bool listEmpty(void);
void addBegin(datatype datavalue);
datatype returnBegin(void);
void removeBegin(void);
void moveCurrentBegin(void);
void moveCurrentNext(void);
datatype returnCurrent(void);
bool currentNull(void);
void insertCurrent(datatype data);
void removeCurrent(void);
void insertEnd(datatype data);
datatype returnEnd(void);
void removeEnd(void);
int countnodes(void);
bool searchList(datatype x);
bool operator == (cLinkedList<datatype> obj);
cLinkedList<datatype> operator +(cLinkedList<datatype> obj);
//~cLinkedList();
private:
llNode<datatype> *head,
*current;
};
template <class datatype>
cLinkedList<datatype>::cLinkedList(){ //Constructor
head = NULL;
current = NULL;
}
//Contructor
template <class datatype>
bool cLinkedList<datatype>::listEmpty(){ //List Empty
bool tempvalue = true;
if(head != NULL){
tempvalue = false;
}
return tempvalue;
}
template <class datatype>
void cLinkedList<datatype>::addBegin(datatype data){ //Add Begin
llNode<datatype> *temp;
temp = new llNode<datatype>;
temp->datavalue = data;
temp->Next = head;
head = temp;
}
template <class datatype>
datatype cLinkedList<datatype>:: returnBegin(void){ //Return Begin - Must check list
return head->datavalue;
}
template <class datatype>
void cLinkedList<datatype>::removeBegin(void){ //Remove Begin
if(!listEmpty()){
llNode<datatype> *temp;
temp = head;
head = head->Next;
delete temp;
}
};
template <class datatype>
cLinkedList<datatype> cLinkedList<datatype>::operator+(cLinkedList<datatype> obj){
cLinkedList<datatype> newlist;
if(!listEmpty()){
moveCurrentBegin();
while(current != NULL){
newlist.insertEnd(current->datavalue);
moveCurrentNext();
}
}
if(!obj.listEmpty()){
obj.moveCurrentBegin();
while(current != NULL){
newlist.insertEnd(obj.current->datavalue);
obj.moveCurrentnext();
}
}
return newlist;
};
template <class datatype>
bool cLinkedList<datatype>::operator==(cLinkedList<datatype> obj){
bool llequal = false;
if(countnodes() == obj.countnodes()){
llequal = true;
}
return llequal;
};
template <class datatype>
bool cLinkedList<datatype>::searchList(datatype x){
bool finddata = false;+
if(!listEmpty()){
moveCurrentBegin();
while(current != NULL && !finddata){
if(current->datavalue == x){
finddata = true;
}
moveCurrentNext();
}
}
return finddata;+
};
template <class datatype>
int cLinkedList<datatype>::countnodes(void){
int count = 0;
if(!listEmpty()){
moveCurrentBegin();
count++;
while(current->Next != NULL){
current = current->Next;
count++;
}
}
return count;
};
template <class datatype>
void cLinkedList<datatype>::removeCurrent(void){
if(current != NULL){
if(current = head){
removeBegin();
current = head;
}else{
llNode<datatype> *temp;
temp = head;
while( temp->Next != current){
temp = temp->Next;
}
temp->Next = current->Next;
delete current;
current = temp;
}
}
};
template <class datatype>
void cLinkedList<datatype>::insertCurrent(datatype data){
if(current != NULL){
if(listEmpty()){
addBegin(data);
}else{
llNode<datatype> *temp;
temp = new llNode<datatype>;
temp->datavalue = data;
temp->Next = current->Next;
current = temp;
}
}
};
template <class datatype>
bool cLinkedList<datatype>::currentNull(void){
bool temp = true;
if (current != NULL){
temp = false;
}
return temp;
};
template <class datatype>
datatype cLinkedList<datatype>::returnCurrent(void){
datatype tempdata;
tempdata = current->datavalue;
return tempdata;
};
template <class datatype>
void cLinkedList<datatype>::moveCurrentNext(void){
if(!listEmpty() && current != NULL){
current = current->Next;
}
};
template <class datatype>
void cLinkedList<datatype>::removeEnd(void){
if(!listEmpty()){
moveCurrentBegin();
while(current->Next->Next != NULL){
moveCurrentNext();
}
delete current->Next;
current->Next = NULL;
}
};
template <class datatype>
datatype cLinkedList<datatype>::returnEnd(void){
if(!listEmpty()){
moveCurrentBegin();
while(current->Next != NULL){
moveCurrentNext();
}
datatype tempdata =returnCurrent();
}
return tempdata;
};
template <class datatype>
void cLinkedList<datatype>::insertEnd(datatype data){
if(listEmpty()){
addBegin(data);
}else{
llNode<datatype> *temp;
temp = new llNode<datatype>;
temp->datavalue = data;
temp->Next = NULL;
moveCurrentBegin();
while(current->Next != NULL){
moveCurrentNext();
}
current->Next = temp;
}
};
template <class datatype>
void cLinkedList<datatype>::moveCurrentBegin(void){
current = head;
}
//Remove Begin
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Explanation / Answer
template <class datatype>
class orderedLList : cLinkedList<datatype>{
public:
void insert(datatype data);
};
template <class datatype>
void orderedLList <datatype>::insert(datatype data){
if(listEmpty()){
addBegin(data);
}else{
llNode<datatype> *temp;
temp = new llNode<datatype>;
temp->datavalue = data;
temp->Next = NULL;
moveCurrentBegin();
while((current->Next->datavalue <data)|| (current->Next == NULL)) {
moveCurrentNext();
}
temp->Next=current->Next;
current->Next=temp;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.