“when we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly...
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/1.jpg)
“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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071806/56649f535503460f94c77f1c/html5/thumbnails/13.jpg)