Pointers (Discussion 2) (Con trỏ và danh sách liên kết) - Christine Zhou

UN
Uni24h
0 次下载
12
0

资料内容

第 3 页

Announcements - Happy three-day weekend! - HW1 due Friday 9/8 - Proj0 due Tuesday 9/18 - Get started ASAP! - Labs are due at midnight on Fridays - Discussion survey: tinyurl.com/czdisc2

第 4 页

Review: Defining Classes // class definition public class IntList { // instance variables int head; IntList tail; // constructors, which will create IntList objects public IntList() {...} public IntList(int value, IntList tail) {...} // you can create methods that use the instance variables! public void insert(int val) {...} public void removeLast() {...} }

第 5 页

Review: Primitives and Reference Types - Two types of values: primitives and reference types - 8 primitive types: char, boolean, int, double, float, byte, short, long - All others are reference types (Strings, self-defined classes like IntList) - Pass-by-value - When Java creates a primitive type value, it will create a box and store that primitive value in the box - When Java creates a reference type value, it will create a box and store the pointer to the reference type value (the value in the box will be the address of the object in memory)

第 6 页

Review: Box and Pointer Diagrams - Will be very helpful in keeping track of everything! - Small example with an int and a Potato object (with an int age and a String variety)

第 7 页

Review: Box and Pointer Diagrams - Let’s try a small example with the IntList we defined a few slides ago. - Draw the B&P for the following code: IntList lst1 = new IntList(1, null); lst1 = new IntList(2, lst1); IntList lst2 = new IntList(3, null); lst2 = new IntList(4, lst2); lst2.head = lst1.head; lst2.tail = lst1.tail; - Take the value living inside the box that we’re looking for, and then do the reassignment (pass-by-value of the box)

第 8 页

1. Boxes and Pointers Draw a box and pointer diagram to represent the IntLists 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; - Remember pass-by-value!

第 9 页

2. Insertion Implement the following method to insert an element into the given position of an IntList. /** Insert a new item at the given position in L and return the resulting * IntList. If the position is past the end of the list, insert a new * node at the end of the list. */ public static IntList insert(IntList L, int item, int position) { } - Draw an example list (maybe 1 3) and insert 2 in between 1 and 3 and see how the pointers change! - Think about the edge cases!

第 10 页

3. Reverse Implement the following method, which reverses an IntList non-destructively. /** Non-destructively reverses an IntList L. Do not modify the original * IntList. */ public static IntList reverseNondestructive(IntList L) { } - What does non-destructively mean? - How do we iterate through this list? Can we go backwards? - We can only go forwards, so what does that say about how we create this reversed list?

第 11 页

4. Shifting a Linked List Implement the following methods to circularly shift an IntList to the left destructively and non-destructively. /** Destructively shifts the elements of the given IntList L to * the left by one position (e.g. if the original list is * (5, 4, 9, 1, 2, 3) then this method should return the list * (4, 9, 1, 2, 3, 5)). Returns the first node in the shifted list. * Don’t use ’new’; modify the original IntList. */ public static IntList shiftListDestructive(IntList L) { }

第 12 页

4. Shifting a Linked List /** Non-destructively shifts the elements of the given * IntList L to the left by one position. Returns the first node in the * shifted list. Don’t modify the original IntList. */ public static IntList shiftListNondestructive(IntList L) { } - Think about what pointers you will need to keep track of (you’ll need more than 1!) - How many base cases are there?

显示前 12 页。下载资料查看完整内容。

评论 (0)

登录 以加入讨论

暂无评论。快来抢沙发吧!