discussion 3: pointerscs61b/sp20/materials/disc/discussion3sol.pdfcs 61b discussion 3: pointers...

8
CS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists L, M, and N after each statement. IntList L = IntList.list(1, 2, 3, 4); IntList M = L.tail.tail; IntList N = IntList.list(5, 6, 7); N.tail.tail.tail = N; L.tail.tail = N.tail.tail.tail.tail; M.tail.tail = L; See last page for solution. Extra: Draw a box and pointer diagram to represent the IntLists L1, L2, and L3 after each state- ment. IntList L1 = IntList.list(1, 2, 3); IntList L2 = new IntList(4, L1.tail); L2.tail.head = 13; L1.tail.tail.tail = L2; IntList L3 = IntList.list(50); L2.tail.tail = L3; See last page for solution ======= CS 61B, Spring 2020, Discussion 3: Pointers 1

Upload: others

Post on 05-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

CS 61B Discussion 3: Pointers Spring 20201 Boxes and PointersDraw a box and pointer diagram to represent the IntLists L, M, and N after each statement.IntList L = IntList.list(1, 2, 3, 4);IntList M = L.tail.tail;IntList N = IntList.list(5, 6, 7);N.tail.tail.tail = N;L.tail.tail = N.tail.tail.tail.tail;M.tail.tail = L;

See last page for solution.

Extra: Draw a box and pointer diagram to represent the IntLists L1, L2, and L3 after each state-ment.IntList L1 = IntList.list(1, 2, 3);IntList L2 = new IntList(4, L1.tail);L2.tail.head = 13;L1.tail.tail.tail = L2;IntList L3 = IntList.list(50);L2.tail.tail = L3;

See last page for solution

=======

CS 61B, Spring 2020, Discussion 3: Pointers 1

Page 2: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

2 Destructive or Nondestructive?Below is a method that takes in an IntList and returns the value of the head of the IntList. Assumethat L is never null./** Returns the head of IntList L. Assumes that L is not null. */public static int getHead(IntList L) {

int listHead = L.head;L = new IntList(5, null);return listHead;

}

Is the above method destructive or nondestructive? Explain.

Nondestructive.

CS 61B, Spring 2020, Discussion 3: Pointers 2

Page 3: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

3 Reversing a Linked ListImplement the following method, which reverses an IntList nondestructively. The original IntListshould not be modified. Instead, the method should return a new IntList that contains the elementsof L in reverse order./** Nondestructively reverses IntList L. */public static IntList reverseNondestructive(IntList L) {

IntList returnList = null;while (L != null) {

returnList = new IntList(L.head, returnList);L = L.tail;

}return returnList;

}

Extra: Implement the following method which destructively reverses an IntList./** Destructively reverses IntList L using recursion. */public static IntList reverseDestructive(IntList L) {

if (L == null || L.tail == null) {return L;

} else {IntList reversed = reverseDestructive(L.tail);L.tail.tail = L;L.tail = null;return reversed;

}

public static IntList reverseDestructiveIterative(IntList L) {if (L == null || L.tail == null) {

return L;}IntList A = L;IntList B = L.tail;A.tail = null;IntList C = null;while (B != null) {

C = B.tail;B.tail = A;A = B;B = C;

}return A;

}}

This can also be implemented using iteration, as shown below./** Destructively reverses IntList L using iteration. */public static IntList reverseDestructive(IntList L) {

if (L == null || L.tail == null) {

CS 61B, Spring 2020, Discussion 3: Pointers 3

Page 4: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

return L;}IntList reversed = L;IntList current = L.tail;reversed.tail = null;IntList next = null;while (current != null) {

next = current.tail;current.tail = reversed;reversed = current;current = next;

}return reversed;

}

CS 61B, Spring 2020, Discussion 3: Pointers 4

Page 5: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

4 Inserting into a Linked ListImplement the following method to insert an element item at a given position position of anIntList L. For example, if L is (1 -> 2 -> 4) then the result of calling insert(L, 3, 2) yieldsthe list (1 -> 2 -> 3 -> 4). This method should modify the original list (do not create an entirelynew list from scratch). Use recursion./** Inserts item at the given position in IntList L and returns the resulting

* IntList. If the value of position is past the end of the list, inserts the

* item at the end of the list. Uses recursion. */public static IntList insertRecursive(IntList L, int item, int position) {

if (L == null) {return new IntList(item, L);

}if (position == 0) {

L.tail = new IntList(L.head, L.tail);L.head = item;

} else {L.tail = insertRecursive(L.tail, item, position - 1);

}return L;

}

Extra: Implement the method described above using iteration. insertIterative is a destruc-tive method and should therefore modify the original list (just like the previous problem, do notcreate an entirely new list from scratch)./** Inserts item at the given position in IntList L and returns the resulting

* IntList. If the value of position is past the end of the list, inserts the

* item at the end of the list. Uses iteration. */public static IntList insertIterative(IntList L, int item, int position) {

if (L == null) {return new IntList(item, L);

}if (position == 0) {

L.tail = new IntList(L.head, L.tail);L.head = item;

} else {IntList current = L;while (position > 1 && current.tail != null) {

current = current.tail;position -= 1;

}IntList newNode = new IntList(item, current.tail);current.tail = newNode;

}return L;

}

CS 61B, Spring 2020, Discussion 3: Pointers 5

Page 6: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

5 Extra: Shifting a Linked ListImplement the following method to circularly shift an IntList to the left by one position destruc-tively. For example, if the original list is (5 -> 4 -> 9 -> 1 -> 2 -> 3) then this method should returnthe list (4 -> 9 -> 1 -> 2 -> 3 -> 5). Because it is a destructive method, the original IntList shouldbe modified. Do not use the word new./** Destructively shifts the elements of the given IntList L to the

* left by one position. Returns the first node in the shifted list. */public static IntList shiftListDestructive(IntList L) {

if (L == null) {return null;

}IntList current = L;while (current.tail != null) {

current = current.tail;}current.tail = L;IntList front = L.tail;L.tail = null;return front;

}

CS 61B, Spring 2020, Discussion 3: Pointers 6

Page 7: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

I Int List L Int List list l 2,3 4

L D TIME.TT slTI2 Int List M L tail tailL D TIME.TT TEM II3 Int List N Int List list 5 6,7L D TIME.TT ITEM INa HIM

4 N tail tail tail NL D TIME.TT ITEM INa HIM5 L tail tail N tail tail tail tail

L D TEH TI ITH ITEM M FN a HIM6Mtail tailL D TITI 1371 124M D IN a HIM I

Page 8: Discussion 3: Pointerscs61b/sp20/materials/disc/discussion3sol.pdfCS 61B Discussion 3: Pointers Spring 2020 1 Boxes and Pointers Draw a box and pointer diagram to represent the IntLists

L L1 Its IIT E

L2 D 1

L1 As IIT

L2 D 141

4 Ll t Elt FA

L2 D t

5 LI TA IIT TA

L2 D t

L3 D told

6 LI D IT TH

i i