L4_up_print

L4_up_print - CSE 12: Basic data structures and...

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

View Full Document Right Arrow Icon
CSE 12 : Basic data structures and object-oriented design Jacob Whitehill jake@mplab.ucsd.edu Lecture Four 4 Aug 2011 Thursday, August 4, 2011 Linked lists, continued. Thursday, August 4, 2011 Review from last lecture Last lecture we looked brie f y at how a linked list could be conceptualized as a “chain” of nodes. A Node is simply a “link” in the chain. _next _data Node _next _data _next _data Node Node Object Object Object null _head _tail Thursday, August 4, 2011 Review from last lecture Each Node contains a reference to an Object that the user wants to store ( node._data ). Each Node also contains a reference to the next “link” ( Node ) in the chain ( node._next ). _next _data Node _next _data _next _data Node Node Object Object Object null _head _tail Thursday, August 4, 2011 Nodes Node s in a LinkedList play an analogous role to the “slots” (elements) of an array in an ArrayList. o1 o2 o3 o1 Node Node Node null o2 o3 int _numElements: 3 _head ArrayList LinkedList Object[] _underlyingStorage list.add(o1); list.add(o2); list.add(o3); Thursday, August 4, 2011 Elements of an array In an array, there is no need to link the elements using pointers because array elements are always adjacent to each other in memory. For an Object[] array, the address of element 1 is just 4 bytes more than the address of element 0. o1 o2 o3 _numElements: 3 ArrayList _underlyingStorage Thursday, August 4, 2011 Elements of an array o1 o2 o3 Contents Address 8192 8196 8200 ... ... 8204 ... ... Object[] _underlyingStorage Arrays are always stored contiguously in memory (in one big chunk): Addr of element i = BaseAddr + i * 4 Easy to jump to a particular index using the [] operator. Thursday, August 4, 2011 Nodes of a linked list With linked lists, nodes can be allocated anywhere in memory. No need for contiguity; hence, more f exible. However, this means that it takes more effort to compute the address of any particular node. We must “iterate through” all nodes before it. Thursday, August 4, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Finding a particular node Let’s assume we have a linked list containing 3 nodes. We have a _head pointer to the f rst node. How do we access the _data contained in the 3rd node? _next _data Node _next _data _next _data Node Node null _head _tail Thursday, August 4, 2011 Finding a particular node final Object thirdElement = _head._next._next._data ; _next _data Node _next _data _next _data Node Node null _head _tail Thursday, August 4, 2011 Finding a particular node Alternatively, we could use a for -loop: Node cursor = _head; for (int i = 0; i < 2; i++) { // Why only 2? cursor = cursor._next; } final Object thirdElement = cursor._data; _next _data Node _next _data _next _data Node Node null _head _tail Thursday, August 4, 2011 Iterating through the whole list Suppose we wish to iterate through the entire list and print out the _data in each node? Node cursor = _head;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

L4_up_print - CSE 12: Basic data structures and...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online