#include<iostream> #include<cassert> using namespace std; template<class Type> s
ID: 3566385 • Letter: #
Question
#include<iostream>
#include<cassert>
using namespace std;
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmpty();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
//Tutorial #2 problem1
bool retrieveAt(int position, Type& item);
//Tutorial #2 problem6
void printCommon(linkedListType<Type>& list1, linkedListType<Type>& list2);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
//protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template<class Type>
class orderedLinkedListType: public linkedListType<Type>
{
public:
bool searcho(const Type& searchItem);
void insertNode(const Type& newItem);
void deleteNode(const Type& newItem);
void mergeLists(orderedLinkedListType<Type> &list1, orderedLinkedListType<Type> &list2);
};
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
nodeType<Type> *current; // pointer to traverse the list
current = list.first;
while(current != NULL)
{
osObject<<current->info<<" ";
current = current->link;
}
return osObject;
}
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
if(this != &otherList) //avoid self-copy
copyList(otherList);
return *this;
}
template<class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template<class Type>
bool linkedListType<Type>::isEmpty()
{
return(first == NULL);
}
template<class Type>
int linkedListType<Type>::length()
{
return count;
}
template<class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
Type linkedListType<Type>::front()
{
assert(last != NULL);
return first->info;
}
template<class Type>
Type linkedListType<Type>::back()
{
assert(last != NULL);
return last->info;
}
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
bool found;
current = first;
found = false;
while(current != NULL && !found)
if(current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if(last == NULL)
last = newNode;
}
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
count++;
}
template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL)
cout<<"Cannot delete from an empty list."<<endl;
else
{
if(first->info == deleteItem)
{
current = first;
first = first->link;
count--;
if(first == NULL) // list has only one node
last = NULL;
delete current;
}
else // search the list for the node with the given info
{
found = false;
trailCurrent = first;
current = first->link;
while(current != NULL && !found)
{
if(current->info != deleteItem)
{
trailCurrent = current;
current = current->link;
}
else
found = true;
}
if(found)
{
trailCurrent->link = current->link;
count--;
if(last == current) //node to be deleted is the last node
last = trailCurrent;
delete current;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
}
template<class Type>
linkedListType<Type>::linkedListType()
{
first = NULL;
last = NULL;
count = 0;
}
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
first = NULL;
copyList(otherList);
}
template<class Type>
linkedListType<Type>::~linkedListType()
{
destroyList();
}
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if(first != NULL)
destroyList();
if(otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the lis to be copied
count = otherList.count;
first = new nodeType<Type>; //create the node
assert(first != NULL);
first->info = current->info; //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node
current = current->link; //make current point to the next node
while(current != NULL) //copy the remaining list
{
newNode = new nodeType<Type>; //create a node
assert(newNode != NULL);
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last node
current = current->link; //make current point to the next node
}
}
}
//Tutorial #2 problem1
template<class Type>
bool linkedListType<Type>::retrieveAt(int position, Type& item)
{
nodeType<Type> *current;
if(first == NULL)
{
cout<<"Cannot retrieve in an empty list."<<endl<<endl;
return false;
}
else if(position < 0 || position >= count)
{
cout<<"The position "<<position<<" is out of range."<<endl<<endl;
return false;
}
else
{
current = first;
for(int i=0; i<position; i++)
current = current->link;
item = current->info;
return true;
}
}
//Tutorial #2 problem6
template<class Type>
void linkedListType<Type>::printCommon(linkedListType<Type>& list1, linkedListType<Type>& list2)
{
nodeType<Type> *current;
if(list1.length()==0 || list2.length()==0)
cout<<"One of the lists is empty";
else
{
current = list1.first;
while(current != NULL)
{
if(list2.search(current->info))
cout<<current->info<<" ";
current = current->link;
}
}
}
/*template<class Type>
bool orderedLinkedListType<Type>::searcho(const Type& searchItem)
{
bool found;
nodeType<Type> *current;
found = false;
current = first;
while(current != NULL && !found)
if(current->info >=searchItem)
found = true;
else
current = current->link;
if(found)
found = (current->info == searchItem);
return found;
}
*/
/*template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newItem; //store newitem in the node
newNode->link = NULL; //set the link field of the node to NULL
if(first == NULL) //case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == first) //case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}
}
*/
/*template<class Type>
void orderedLinkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL) //case 1
cout<<"Cannot delete from an empty list."<<endl;
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == NULL) //case 4
cout<<"Item to be deleted is not in the list."<<endl;
else
if(current->info == deleteItem) //item to be deleted is in the list
{
if(first == current) //case 2
{
first = first->link;
delete current;
}
else //case 3
{
trailCurrent->link = current->link;
delete current;
}
count--;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
*/
template<class Type>
void orderedLinkedListType<Type>::mergeLists(orderedLinkedListType<Type> &list1, orderedLinkedListType<Type> &list2)
{
orderedLinkedListType<Type> &list3;
orderedLinkedListType<Type> *current, *current2, *newNode;
current=list1->first;
current2=list2->first;
newNode = new nodeType<Type>;
newNode->info=orderedLinkedListType<Type>::first;
newNode->link=NULL;
while(current != NULL || current2 != NULL)
{
if(current->info > current-> info)
{
newNode ->info = current->info;
current= current->link;
}
else{
newNode->info=current2->info;
current2= current2->link;
}
while(list3 != NULL)
{
list3->info=newNode->info;
newNode = newNode->link;
}
}
int main()
{
orderedLinkedListType<double> list1,list2,list3;
list1.insertNode(1.9);
list1.insertNode(2.3);
list1.insertNode(2.7);
list1.insertNode(3.1);
list1.insertNode(3.6);
list2.insertNode(2.1);
list2.insertNode(2.4);
list2.insertNode(2.9);
list2.insertNode(3.2);
cout<<list1<<endl;
cout<<list2<<endl;
list3.mergeLists(list1,list2);
cout<<list3<<endl;
system ("pause");
return 0;
}
Please fix my code to get it running, it merges list1 and list2 which are linked lists.
Explanation / Answer
Here is the modified code which is working fine , i just edited the mergeLists function . check it and still if you have any problem comment.
#include<iostream>
#include<cassert>
using namespace std;
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmpty();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
//Tutorial #2 problem1
bool retrieveAt(int position, Type& item);
//Tutorial #2 problem6
void printCommon(linkedListType<Type>& list1, linkedListType<Type>& list2);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
//protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template<class Type>
class orderedLinkedListType: public linkedListType<Type>
{
public:
bool searcho(const Type& searchItem);
void insertNode(const Type& newItem);
void deleteNode(const Type& newItem);
void mergeLists(orderedLinkedListType<Type> &list1, orderedLinkedListType<Type> &list2);
};
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
nodeType<Type> *current; // pointer to traverse the list
current = list.first;
while(current != NULL)
{
osObject<<current->info<<" ";
current = current->link;
}
return osObject;
}
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
if(this != &otherList) //avoid self-copy
copyList(otherList);
return *this;
}
template<class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template<class Type>
bool linkedListType<Type>::isEmpty()
{
return(first == NULL);
}
template<class Type>
int linkedListType<Type>::length()
{
return count;
}
template<class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
Type linkedListType<Type>::front()
{
assert(last != NULL);
return first->info;
}
template<class Type>
Type linkedListType<Type>::back()
{
assert(last != NULL);
return last->info;
}
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
bool found;
current = first;
found = false;
while(current != NULL && !found)
if(current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if(last == NULL)
last = newNode;
}
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
count++;
}
template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL)
cout<<"Cannot delete from an empty list."<<endl;
else
{
if(first->info == deleteItem)
{
current = first;
first = first->link;
count--;
if(first == NULL) // list has only one node
last = NULL;
delete current;
}
else // search the list for the node with the given info
{
found = false;
trailCurrent = first;
current = first->link;
while(current != NULL && !found)
{
if(current->info != deleteItem)
{
trailCurrent = current;
current = current->link;
}
else
found = true;
}
if(found)
{
trailCurrent->link = current->link;
count--;
if(last == current) //node to be deleted is the last node
last = trailCurrent;
delete current;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
}
template<class Type>
linkedListType<Type>::linkedListType()
{
first = NULL;
last = NULL;
count = 0;
}
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
first = NULL;
copyList(otherList);
}
template<class Type>
linkedListType<Type>::~linkedListType()
{
destroyList();
}
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if(first != NULL)
destroyList();
if(otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the lis to be copied
count = otherList.count;
first = new nodeType<Type>; //create the node
assert(first != NULL);
first->info = current->info; //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node
current = current->link; //make current point to the next node
while(current != NULL) //copy the remaining list
{
newNode = new nodeType<Type>; //create a node
assert(newNode != NULL);
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last node
current = current->link; //make current point to the next node
}
}
}
//Tutorial #2 problem1
template<class Type>
bool linkedListType<Type>::retrieveAt(int position, Type& item)
{
nodeType<Type> *current;
if(first == NULL)
{
cout<<"Cannot retrieve in an empty list."<<endl<<endl;
return false;
}
else if(position < 0 || position >= count)
{
cout<<"The position "<<position<<" is out of range."<<endl<<endl;
return false;
}
else
{
current = first;
for(int i=0; i<position; i++)
current = current->link;
item = current->info;
return true;
}
}
//Tutorial #2 problem6
template<class Type>
void linkedListType<Type>::printCommon(linkedListType<Type>& list1, linkedListType<Type>& list2)
{
nodeType<Type> *current;
if(list1.length()==0 || list2.length()==0)
cout<<"One of the lists is empty";
else
{
current = list1.first;
while(current != NULL)
{
if(list2.search(current->info))
cout<<current->info<<" ";
current = current->link;
}
}
}
template<class Type>
bool orderedLinkedListType<Type>::searcho(const Type& searchItem)
{
bool found;
nodeType<Type> *current;
found = false;
current = first;
while(current != NULL && !found)
if(current->info >=searchItem)
found = true;
else
current = current->link;
if(found)
found = (current->info == searchItem);
return found;
}
template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newItem; //store newitem in the node
newNode->link = NULL; //set the link field of the node to NULL
if(first == NULL) //case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == first) //case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}
}
template<class Type>
void orderedLinkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL) //case 1
cout<<"Cannot delete from an empty list."<<endl;
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == NULL) //case 4
cout<<"Item to be deleted is not in the list."<<endl;
else
if(current->info == deleteItem) //item to be deleted is in the list
{
if(first == current) //case 2
{
first = first->link;
delete current;
}
else //case 3
{
trailCurrent->link = current->link;
delete current;
}
count--;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
template<class Type>
void orderedLinkedListType<Type>::mergeLists(orderedLinkedListType<Type> &list1, orderedLinkedListType<Type> &list2)
{
nodeType<Type> *current, *current2, *newNode;
current=list1.first;
current2=list2.first;
while(current != NULL && current2 != NULL)
{
if(current->info > current2->info)
{
insertLast(current2->info);
current2 = current2->link;
}
else
{
insertLast(current->info);
current = current->link;
}
}
while(current != NULL)
{
insertLast(current->info);
current = current->link;
}
while(current2 != NULL)
{
insertLast(current2->info);
current2 = current2->link;
}
}
int main()
{
orderedLinkedListType<double> list1,list2,list3;
list1.insertNode(1.9);
list1.insertNode(2.3);
list1.insertNode(2.7);
list1.insertNode(3.1);
list1.insertNode(3.6);
list2.insertNode(2.1);
list2.insertNode(2.4);
list2.insertNode(2.9);
list2.insertNode(3.2);
cout<<list1<<endl;
cout<<list2<<endl;
list3.mergeLists(list1,list2);
cout<<list3<<endl;
system ("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.