midterm_C1_ans

# midterm_C1_ans - Student Name Student ID EE2331 Data...

• Test Prep
• 3
• 100% (2) 2 out of 2 people found this document helpful

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.

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

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

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern