{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2sol

# hw2sol - CMPS 20 HW2 solution-1Notes If P is null then L...

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

CMPS 20 HW2 solution ---------------------------------------------- 1- Notes: - If P is null then L points to the rightmost node and thus, all links point to the left - If L is null then P points to the leftmost node and thus, all the links point to the right - If the list is empty then both P and L are null - For our method to be able to change P and L they must be declared as members of the class in which the method is defined. We call this class LL. In this case the methods do not have to have P and L passed to them as parameters, because they are accessible as class members. class LL { Node P,L; void moveRight() { if (P==null) return; // nothing to do, already at rightmost place Node tmp; // a temp place to hold the reference of the node to the right of P tmp = P.next; P.next = L; L = P; P = tmp; } void moveLeft() { if (L==null) return; // nothing to do, already at leftmost place Node tmp; // a temp place to hold the reference of the node to the left of L tmp = L.next; L.next = P; P = L; L = tmp; } } ----------------------------------------------------------------------------------- -------------------------------------- 2- This approach of merging two sorted lists to form a new sorted list is called "Merge Sort". Assume that both lists hold integer numbers sorted in an ascending order. The first implementation will create (allocate) new nodes for the new list while the second one will use the same nodes of the two lists and just change their links such that they become the new list. The following is true for both implementations given below: 1- It takes two parameters: linked lists A and B. We assume that the linked list class of which A and B are instances, has a public member field called firstNode which refers to the first node of the list. If this member was private we wouldn t be able to develop the second implementation because it requires access to the references of

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

View Full Document
the nodes themselves, not only the contents of the nodes. In other words: we can develop the first implementation in two ways (1- assuming private members and using public interface methods, 2- assuming non-private members) while the second implementation can be implemented only in the second way. So, for consistency, we develop both implementations in the second way; this will make comparing the two implementations easier for you. 2- It returns a new linked list object called C 3- The logic is as follows: we will go over both lists from the smallest number (first node) to the largest number (last node). We will pick a number (a node) from one of the lists at a time and add it to the new list. Every time we pick the list that
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

hw2sol - CMPS 20 HW2 solution-1Notes If P is null then L...

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

View Full Document
Ask a homework question - tutors are online