BIL 221 - Veri Yapıları 1998-99 Güz Yarıyılı Arasınav-2 Cevap Anahtarı -------------------------- Soru 1) Ford&Topp, Written Exercise 7.5) (Solution is in the book) template void InsertOrder (T A [], int size, const T& elem) { int i=0; // assume size > 0 while (i i; j--) // move tail to right A[j] = A[j-1]; A[i] = elem; // insert the element } int operator< (Student s1, Student s2) { return strcmp(s1.name, s2.name) < 0; } ostream& operator<< (ostream& ostr, Student s) { cout< class DummyType { private: T sval; // stored value T *dval; // points the a second stored value public: // Constuctor and copy constructor DummyType(T svalue, T dvalue); DummyType(const DummyType& x); // Assignment operator DummyType& operator= (const DummyType& x); // Destructor ~DummyType( ); // Data handling methods void SetValue(T svalue, T dvalue); // sets the stored values // Stream input and output friend ostream& operator<< (ostream& ostr, const DummyType& x); friend ostream& operator>> (istream& istr, const DummyType& x); } template DummyType::DummyType(T svalue, T dvalue) { sval = svalue; dval = new T(dvalue); } template DummyType::DummyType(const DummyType& x) { sval = x.svalue; dval = new T(*x.dvalue); } template DummyType& DummyType::operator= (const DummyType& x) { sval = x.svalue; *dval = *x.dvalue; } template DummyType::~DummyType( ) { delete dval; } template void DummyType::SetValue(T svalue, T dvalue) { sval = svalue; *dval = dvalue; } template friend ostream& operator<< (ostream& ostr, const DummyType& x) { ostr << sval; ostr << *dval; } template friend ostream& operator>> (istream& istr, const DummyType& x) { istr >> sval; istr >> *dval; } Soru 3) Ford&Topp, Similar to Written Exercise 9.12) template void DeleteKey(Node* &head, T key) { //maintain pointers to previous and current nodes Node *prevPtr=NULL, *currPtr=head, *p; //traverse the list, performing the deletions while (currPtr != NULL) { if (currPtr->data == key) { //check for a match with key p = currPtr; // remember the current node address in p currPtr = currPtr->NextNode(); //move currPtr forward if (prevPtr == NULL) {//see if deletion occured at front of list head = currPtr; else prevPtr->DeleteAfter(); delete p; } else { //no match, advance the pointers prevPtr = currPtr; currPtr = currPtr->NextNode(); } } } Soru 4) Ford&Topp, Similar to Written Exercise 9.8) template void MergeLists(Node *L1, Node *L2, Node* &L3) { L3 = NULL; Node *pL1=L1, *pL2=L2, *pL3=L3; while (pL1 != NULL && pL2 != NULL) { if (pL1->data <= pL2->data) { Append(L3, pL3, pL1, 1); pL1 = pL1->NextNode(); } else { Append(L3, pL3, pL2, 1); pL2 = pL2->NextNode(); } } // add remaining nodes of the longer list to the end of L3 if (pL1 != NULL) Append(L3, pL3, pL1, 0); else Append(L3, pL3, pL2, 0); } template void Append(Node* &headL3, Node* &rearL3, Node* pL12, int onenode) { Node *newNode; if (pL12 == NULL) return; //source list (L1 or L2) is empty if (headL3 == NULL) { //destination list (L3) is empty headL3 = new Node(pL12->data); rearL3 = headL3; if (onenode) return; pL12 = pL12->NextNode(); } // if destination list (L3) is not empty. // L3 is empty but onenode is false then append the remainder of list L. while (pL12 != NULL) { newNode = new Node(pL12->data); rearL3->InsertAfter(newNode); rearL3 = newNode; if (onenode) return; pL12 = pL12->NextNode(); } }