c109s07t2 - CSE 109 Test 1 Wednesday 11 April 2007...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Test 1 Wednesday 11 April 2007 <<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1. Assume the definition of class Link below, meant to be used to construct a linked list ending in a NULL pointer. Overload the operator "<<" such that the code below the definition of Link would display the linked list in the following form: 6-->4-->3-->NULL class Link{ public: int j; Link *next; }; } Link * t; //some code to build the list cout<<t<<endl; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ostream & operator<<(ostream &out,Link *k){ while(k!=NULL){ out<<k->j<<"--> "; k=k->next; } out<<"NULL"; return out; } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write a template for the class Comaprable that stores an instance of some variable for which "<" is defined. Instances of the class should respond to the methods min() (max()) by returning an instance of Comparable containing the value from the smaller (larger) of two instances. For example, the code below should produce the indicated output. Comparable <int> a(5),b(3); cout<<a.min(b)<<" "<<a.max(b)<<endl; // [3] [5] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< template <class X> class Comparable{ public: Comparable(const X &x); Comparable min(const Comparable &t)const; Comparable max(const Comparable &t)const;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern