c109s07f

c109s07f - CSE 109 Finaly Exam Saturday 5 May 2007...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Finaly Exam Saturday 5 May 2007 4:00-7:00 PM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<< 1. Assume the definition of class Link below, meant to be used to construct a linked list ending in a NULL pointer. Write a function remove() that removes the first link it encounters containing a given number. If the number is not in the list, remove() does nothing. Give the declaration Link *hd; the call to remove the first link, if any, containing 10 would look like remove(hd,10). class Link{ public: int j; Link *next; }; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Link * temp; if(h==NULL) return; if(h->j==t){ temp=h; h=h->next; delete temp; //avoid memory leak } else remove(h->next,t); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write a template for the class Magnitude that stores an instance of some variable or object for which "<", "==", and "<<" are defined. Instances of the class should respond to the binary operators "<", "==", and "<=". Below is some sample code. Magnitude <int> m(5), n(3); cout<<"("<<m<<"=="<<n<<")=="<<(m==n)<<endl; //OUTPUT: (5==3)==0 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> template <class C> class Mag{ public: Mag(const C&c=NULL); template <class A> friend bool operator<(const Mag<A>&a, const Mag &b); template <class A> friend bool operator<=(const Mag<A>&a, const Mag &b); template <class A> friend bool operator==(const Mag<A>&a, const Mag &b); template <class A> friend ostream & operator<<(ostream &out,const Mag &m); private: C x; }; } template <class A> M template <class A> return a.x<b.x; } template <class A> return a.x<b.x || a.x==b.x;
Background image of page 1
} template <class A> return a.x==b.x; } template <class A> out<<m.x; return out; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 3. This questions refers to a language with the syntax given by the following syntax diagram A and associated diagrams (where parentheses, (), indicate circles and brackets, [], indicate boxes):
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

c109s07f - CSE 109 Finaly Exam Saturday 5 May 2007...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online