Sentinel pointer always points to the dummy start node.
Caching the size to avoid traversl when adding/removing.
Caching the last pointer to avoid traversal when adding the last.
Basic: SLList with sentinel and caching size
/** An SLList is a list of integers, which hides the terrible truth
* of the nakedness within. */publicclassSLList{privatestaticclassIntNode{publicint item;public IntNode next;publicIntNode(int i, IntNode n){
item = i;
next = n;
System.out.println(size);}}/* The first item (if it exists) is at sentinel.next. */private IntNode sentinel;privateint size;/** Creates an empty SLList. */publicSLList(){
sentinel =newIntNode(63, null);//do not worry about dummy 63
size =0;}publicSLList(int x){
sentinel =newIntNode(63, null);
sentinel.next =newIntNode(x, null);
size =1;}/** Adds x to the front of the list. */publicvoidaddFirst(int x){
sentinel.next =newIntNode(x, sentinel.next);
size = size +1;}/** Returns the first item in the list. */publicintgetFirst(){return sentinel.next.item;}/** Adds x to the end of the list. */publicvoidaddLast(int x){
size = size +1;
IntNode p = sentinel;/* Advance p to the end of the list. */while(p.next != null){
p = p.next;}
p.next =newIntNode(x, null);}/** Returns the size of the list. */publicintsize(){return size;}}
Improvement1: SLList with sentinel, caching size and last
DLList
Intro
Need DLList to make removing last fast
Basic1: DLList with double sentinels
the double sentinels approach avoids special case
Basic2: DLList with single circular sentinel
the circular sentinel approach avoids special case
Array
Intro
Think of arrays as numbered sequence of memory, whereas lists as named memory.
Think of arrays as instance variables.
Default value is zero not garbage.
Garbage collection.
Creation
x =newint[3];//initialize with all zeros
y =newint[]{1,2,3,4,5};
int[] z ={9,10,11,12,13};
Array of strings
/* Think of array of strings as 2-D array */
String[] s =newString[6];
s[4]="ketchup";
s[2]="muffins";
AList
Intro
Resizing by multipling by 2 is the fastest way.
Assigning to null after removing invokes garbage collection, which is helpful for large list objects.
Resize() should be private.
When adding item, check if resize is needed.
AList Implementation
/* Invariants:
addLast: The next item we want to add, will go into position size
getLast: The item we want to return is in position size - 1
size: The number of items in the list should be size.
*/publicclassAList{privateint[] items;privateint size;/** Creates an empty list. */publicAList(){
items =newint[100];
size =0;}/** Resizes the underlying array to the target capacity. */privatevoidresize(int capacity){int[] a =newint[capacity];
System.arraycopy(items,0, a,0, size);
items = a;}/** Inserts X into the back of the list. */publicvoidaddLast(int x){if(size == items.length){resize(size *2);}
items[size]= x;
size = size +1;}/** Returns the item from the back of the list. */publicintgetLast(){return items[size -1];}/** Gets the ith item in the list (0 is the front). */publicintget(int i){return items[i];}/** Returns the number of items in the list. */publicintsize(){return size;}/** Deletes item from back of the list and
* returns deleted item. */publicintremoveLast(){int x =getLast();
size = size -1;return x;}}
ListsSLListIntroAdd a middle man that supervise the listSentinel pointer always points to the dummy start node.Caching the size to avoid traversl when adding/removing.Caching the last pointer to avoid traversal when adding the last.Basic: SLList w