Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. Linked Lists.

Similar presentations


Presentation on theme: "4. Linked Lists."— Presentation transcript:

1 4. Linked Lists

2 Why they’re cool ArrayLists: LinkedLists (just the reverse)
+: Infrastructure is already there (arrays) -: performance hit when capacity is reached (resize op) +: Lean memory usage: Array itself: 4/8 bytes per slot (a reference [32-bit = 4bytes, 64-bit = 8 bytes]) The actual data we’re storing Other items: A used-count (4 bytes) (static) capacity-increase amount (4 bytes) -: Require contiguous memory for the array. LinkedLists (just the reverse) We’ll explore the memory usage shortly.

3 Review: Nested Classes
public class Outer { protected class Inner } Inner mSampleInst; A class within a class! We actually saw it with ArrayList.ArrayListItearator Why? Data Hiding (note the protected) The user of Outer need not even know about Inner! Encapsulation Data Hiding. Also implies we only need worry about it in Outer. Most LinkedList implementations have a hidden Node class. Contains the data… …and references to the neighboring Nodes.

4 Code View: Basic Structure
class LinkedList<E> { protected class Node E mData; // Note how the generic was “inherited” Node mNext;// Reference to the following node // (or null if there is none) Node mPrev;// Similar for preceding. This makes // our Llist a doubly-linked list. } protected Node mStart; // Sometimes called the head protected Node mEnd; // Sometimes called the tail protected int mSize; // Number of nodes public void add(E new_val) { … } public void remove(E val) { … } // additional top-level methods. LinkedList<String> LL = new LinkedList<String>(); LL.addLast(“Sue”); LL.addLast(“Bob”); LL.addLast(“Tom”);

5 Logical View Empty List List with 3 elements mStart: (null) mEnd:
mSize: 0 mStart: mEnd: mSize: 3 mData: “Bob” mNext: mPrev: mData: “Sue” mData: “Tom”

6 Memory View Stack Heap “Sue”’s node “Tom”’s node “Bob”’s node
addr var contents Notes LL 40000 (in main program) 50000 mStart field of list pointed to by LL 40004 55000 mEnd field of llist pointed to by LL 40008 3 mSize field llist pointed to by LL 72000 mData field 50004 60000 mNext field 50008 0 (null) mPrev field 71000 55004 55008 70000 60004 60008 “Bob” mData of Bob-node “Tom” mData of Tom-node “Sue” mData of Sue-node Memory View Stack Heap “Sue”’s node “Tom”’s node “Bob”’s node Note how the nodes are not in contiguous order (nor in the order they were allocated)! Also note the mData field is also a reference to data elsewhere on the heap!

7 Iteration Not an iterator (yet)
A way to “visit” each node in a linked-list Used in most operations (internally!) Forward-iteration Backwards-iteration (doubly-linked lists only) You figure it out! Node cur = mStart; while (cur != null) { // Do something to cur.mData // (and / or mNext, mPrev) // Advance iteration cur = cur.mNext; }

8 Major methods java.util.linkedList
void addFirst(E val); // (to end) void addLast(E val); void add(int index, E val); // insert at that pos. // 0: same as addFirst // size: same as addLast void clear(); int indexOf(E val); // -1 if not found. E get(int index); void remove(int index); // make both get and remove use a protected // method called getNodeByPosition(int index). int size(); Object[] toArray(); String toString(); // I want “<[first][second]…>” // or “<empty>” Iterator<E> iterator();

9 Freebie: Add (at index)
void add(int index, E val); First: determine all cases case1: list is empty => call addFirst case2: list is non-empty and index = 0 => call addFirst case3: list is non-empty and index = size => call addLast case4: list is non-empty and 0 < index < size => work! case5: invalid index – raise an Exception Algorithm (for case4): Allocate a new node (disconnected) Iterate until cur is equal to index-1 Re-route references (see logical view, next slide) Add one to size Victory!

10 Add @ Index, cont. Logical view new_node: mData: <whatev> mNext:
mPrev: cur: mData: <whatev> mNext: mPrev: after traversal: cur is at index

11 code Implementation // Note: this is in the LinkedList class (but outside // the nested Node class) void add(int index, E val) throws IndexOutOfBoundesException { if (position < 0 || position > mSize) // Covers case 5 -- fast-fail! throw new IndexOutOfBoundsException("Invalid index: " + position); if (position == 0 || mSize == 0) addFirst(value); // Covers case 1 & 2 else if (position == mSize) addLast(value); // Covers case 3 else // Everything else falls under case 4 -- we're somewhere in the // middle (continued on next slide)

12 code Implementation, cont.
// ... Create the new node (this is done in addFirst and addLast too... Node node_node = new Node(value); // ... traverse to the correct spot (so cur is at desired location) Node cur = mStart; int curPos = 0; while (curPos != position && cur != null) { cur = cur.mNext; curPos++; } // ... re-route the pointers node_node.mNext = cur; node_node.mPrev = cur.mPrev; cur.mPrev.mNext = node_node; cur.mPrev = node_node; // We now have one more node. Note: I put it in the else b/c // addFirst and addLast do it themselves – we don't want to do it twice! mSize++; } // end of else from last slide } // end of add function body

13 Lab 4 Make test program together You finish implementing the methods
Like we did in Lab3 Ask questions – I’m more forthcoming in labs like this!


Download ppt "4. Linked Lists."

Similar presentations


Ads by Google