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

UN
Uni24h
0 lượt tải
12
0

Nội dung tài liệu

Trang 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

Trang 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() {...} }

Trang 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)

Trang 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)

Trang 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)

Trang 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!

Trang 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!

Trang 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?

Trang 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) { }

Trang 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?

Hiển thị 12 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!