midterm_C1_ans - Student Name Student ID EE2331 Data...

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

Student Name: Student ID: EE2331 Data Structures and Algorithms (Sem A 2014/15) Midterm Test Question 1 Implement the following C++ function. It prints the intersection of two sorted arrays. For example, if A={1,3,4,5,7,8} and B={3,7,8,9}, the output is {3,7,8}. /* Function prints intersection of A[] and B[] n is the number of elements in A[] m is the number of elements in B[] Precondition : arrays A and B are sorted.*/ void printIntersection(int A[], int B[], int n, int m) { } Question 2 The node structure for a singly linked list is defined below. template<class Type> struct nodeType { Type info; nodeType<Type> *link; } Consider the implementation of the linkedListType class. The class has 2 member variables nodeType<Type> *first; // pointer to the first node in the list nodeType<Type> *last; // pointer to the last node in the list Implement a member function lastToFirst() , where the function interface is given below. template<class Type> void linkedListType<Type>::lastToFirst() { // Remove the last node and replace it as the first node.
Image of page 1

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

Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // Your implementation should not call other member functions of the class } Example: the info stored in a node is represented by the symbols A , B , C , and etc. in the diagram. Contents of the list before calling the function lastToFirst() : first last [A]  [B]  [C]  [D]  [E]  NULL Contents of the list after calling the function lastToFirst() : first last [E]  [A]  [B]  [C]  [D]  NULL Solutions Question 1 void printIntersection(int A[], int B[], int n, int m) { int a_index = 0; int b_index = 0; while (a_index < n && b_index < m) { if (A[a_index] == B[b_index]) { print(A[a_index]); a_index++; b_index++; } else if (A[a_index] < B[b_index]) { a_index++; } else { b_index++; } } } Question 2 void linkedListType<Type>::lastToFirst() { if(first != last){ nodeType<Type> *end = first; nodeType<Type> *prev_end; while(end->link != NULL) { prev_end = end; end = end->link; } prev_end->link = 0; last = prev_end; end->link = first; first = end; } }...
View Full Document

  • Spring '14
  • VAN
  • EE2331 Data Structures, void printIntersection, int a_index

{[ 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