Question: Write a merge function based on the following conditions and sample Code: #include //exception is thrown if wrong input class BadInput { public: BadInput (){};
Write a merge function based on the following conditions and sample Code:

#include //exception is thrown if wrong input class BadInput { public: BadInput (){}; }; template struct Node { E data; Node *next; }; template class SortedChain { public: SortedChain() {first=0;} ~SortedChain(); bool IsEmpty() {return (first==0);} //int Length() const; //bool Search (const K& k, const E& e); SortedChain& Delete(const K& k, E& e); SortedChain& Insert (const K& k,const E& e); void Output() const; private: Node *first; };
template SortedChain::~SortedChain() { Node *current=first; while (first) { current=first->next; delete first; first=current; } } template SortedChain& SortedChain::Delete(const K& k, E& e) { Node *prior=first, *current=first; while (current && current->data.Key() k prior=current; current=current->next; } if (current && current->data.Key() == k) { // e=current->data; if (first == current) //delete first node first=current->next; else prior->next=current->next; delete current; return *this; } else throw BadInput(); }
SortedChain& SortedChain:: Merge (SortedChain & S2 const) {
------- write your function }
template SortedChain& SortedChain::Insert(const K& k, const E& e) { Node *prior=first, *current=first; while (current && (current->data).Key() next; } if (current && (current->data).Key() == k) throw BadInput(); //another node with same key else { Node *newp; newp=new Node; if (first == current) { //we like to insert on first position first=newp; } else prior->next=newp; newp->next = current; newp->data=e; return *this; } } template void SortedChain::Output() const { Node *current=first; cout data.Key() data.Value() "; current=current->next; } } class TypeE { public: long key; long value; Type(){}; long Key(){return key;} long Value(){return value;} }; main() { try {
SortedChain Chain; TypeE e; e.key=2; e.value = 1000; Chain.Insert(e.Key(),e); e.key=4; e.value =500; Chain.Insert(e.Key(),e); e.key=6; e.value =1500; Chain.Insert(e.Key(),e); e.key=1; e.value=3000; Chain.Insert(e.Key(),e); Chain.Output(); Chain.Delete(2,e); Chain.Output(); } catch (BadInput) { cerr
You must write a function called Merge SortedChain EK&SortedChain E,K>: Merge (SortedChain E,K> & S2 const) { write your function that does the following: Suppose that we declare the following two dictionary objects: main SortedChain TypeE, long> Chain SortedChain TypeE, long> Chain2; Suppose Chain I is 9 where 5,8,and 9 are the keys, and suppose Chain2 is then Chain1l Merge(Chain2) should be 10 The Merge function will change Chainl, thus you must return a reference to Chainl | that is return(* this)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
