“when we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly...

13
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell Thought for the Day

Upload: frank-patterson

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.”– Joseph Campbell

Thought for the Day

Page 2: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

New Class: IntegerList

• Same public methods as IntegerVector– Clients will see almost no difference

• Very different “internal” implementation

Page 3: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

IntegerList

first, numElementsfirst, numElementsadd, get, set, position, remove, length

Class Diagram

Page 4: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

Internal Details

public class IntegerList { private class ListNode { public int data; public ListNode next; } // class ListNode

private ListNode first; // Pointer to the // first ListNode in an IntegerList private int numElements; // Number of // elements in an IntegerList . . . } // class IntegerList

An Inner Class

Page 5: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

The Inner Class ListNode

• ListNode is private, so can only be used inside IntegerList

• data and next are public, so can be accessed outside ListNode

public class IntegerList { private class ListNode { public int data; public ListNode next; } // class ListNode . . . } // class IntegerList

Page 6: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

Structure of a ListNode Object

• We can picture a single ListNode object as follows:

ListNode

data

next

-56

private class ListNode { public int data; public ListNode next; } // class ListNode

Page 7: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

A Linked List

• Made up of many ListNode objects “linked” together by the next fields

ListNode

data

next

-56 19 3 -2

Page 8: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

Locating the First Element in the List

• We use the first member of the IntegerList class

ListNode

data

next

-56 19 3 -2

IntegerList

first

numElements 4

Page 9: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

Implementation of the Class• Constructor

– Must create an “empty” list

public IntegerList ()// Constructor { first = null; numElements = 0; } // constructor

IntegerList

first

numElements 0

Page 10: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

Adding a New Element• Simple Case: add as first element

• Need to– Create a new list node– Link it into the list

public void add (int item)// Place the new item into a list { ListNode node = new ListNode(); node.data = item; node.next = first; first = node; numElements++; } // add

Page 11: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

public void add (int item) { ListNode node = new ListNode(); node.data = item; node.next = first; first = node; numElements++; } // add

ListNode

data

next

3 -8

IntegerList

first

numElements 2

Adding 17

0

node

17

3

Page 12: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell

More Flexibility: Adding Nodes at Any Position

• Need to work through the linked list to find the correct position

Page 13: “When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell