CSC3A - ST1 Notes
CSC3A - ST1 Notes
CSC3A - ST1 Notes
+ Abstract Classes
- Abstract classes are between interfaces and base classes
- They may define method signatures, known as abstract methods, but they
may also have fields and implemented methods known as concrete methods
- May not be instantiated. Child class must implement abstract methods of
parent class.
+ Casting
- Widening Conversions occur when a type is casted or converted up. This is
implicit casting.
- Narrowing Conversions, casting down or explicit casting. Instanceof should
be used before attempting down casting.
+ Generics
- The goal of generic programming is to write one class that can accommodate
any input type. Avoid many explicit casts.
- Type inference
- Not defined at compile time, defined at run-time.
- Instantiate with actual parameters
Pair<String, ArrayList> = new Pair<String, ArrayList>();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Chapter III Fundamental Data Structures
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Linked list is collection of nodes that form a linear sequence.
+ The first node in a Linked List is the Head, the last node is known as the
Tail. The Tail's next reference is Null.
+ Link Hopping refers to traversing the list
+ Singly Linked lists only keep track of the node next in the sequence
+ Singly Linked List
------------------------------------------------------------------------------+ Methods
- size()
- isEmpty()
- first()
- last()
- addFirst(e)
- addLast(e)
- removeFirst()
+ Add element to the Head of List
- Create a new node, with new element
- Set the new nodes next to the Head
- Set the Head to the new node
- Increment List Size
- addFirst(e)
tmp = new Node(e);
tmp.next = head;
head = tmp;
size ++;
+ Add element to Tail
- Create new node, with new element
- Set new node next to null
- Set Tail next to new node
- Set Tail to new node
- Increase List size
- addLast(e)
tmp = new Node(e);
tmp.next = Null;
Tail.next = tmp;
Tail = tmp;
size++;
+ removeFirst()
head = head.next;
size--;
+ Doubly Linked List
------------------------------------------------------------------------------+ Doubly Linked lists keep track of the preceeding and proceeding nodes. Both
next and previous nodes are tracked at each node.
+ Doubly linked lists have nodes at the tail and head that do not contain any
elements. These nodes demarcate the boundaries of the list and are known as
the Sentinel Nodes.
+ Methods
- size()
- isEmpty()
- first()
- last()
- addFirst(e)
- addLast(e)
- removeFirst()
- rmoveLast()
+ addBetween(E e, Node<E> predecessor, Node<E> successor)
Node<E> tmp = new Node<E>(e, predecessor, successor);
predecessor.next = tmp;
successor.previous = tmp;
size++;
+ E remove(Node<E> node)
Node<E> predecessor = node.previous;
Node<E> successor = node.next;
predecessor.next = successor;
successor = predecessor;
size--;
return node.getElement();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Chapter IV Algorithm Analysis
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Seven Functions used in Algorithm Analysis
- Constant Function
- f(n) = c
- Logarithm Function
- x = logn iff b^x =n
- Linear Function
- f(n) = n
- N-log N function
- f(n) = nlog(n)
- Quadratic Function
- f(n) = n^2
- Cubic and other Polynomials
- f(n) = n^3
- Exponential Function
- f(n) = b^n
+ Ordered in increasing Growth Rate
1. 1
2. logn
3. n
4. nlogn
5. n^2
6. n^3
7. 2^n
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Chapter V Recursion
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Recursion can be distilled into the concept; when a method calls itself
+ Types of recursion
- Linear Recursion
- LinearSum(A, n)
if(n == 0)
return A[0]
else
return LinearSum(A, n-1) + A[n-1]
- Tail Recursion
- Makes recursive call on last step
- Binary Recursion
- Multiple Recursion
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Chapter VI Stacks, Queues & Deques
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Stacks are a collection of objects who are inserted and removed according
to the LIFO principal. Or Last-In, First-Out.
+ Java.util.Stack interface
+ The Stack ADT
------------------------------------------------------------------------------- push(e)
- pop()
- top() //returns element at top
- size()
- isEmpty()
+ Queues are a collection of objects that are inserted according to the FIFO
data[k+1] = data[k];
data[i] = e;
size++;
+ Resize array
- Manually
- Create new array larger than current
- Copy contents across
- Dereference old Array
- ArrayCopy Java.lang.System
- Make new destination array, dest[]
- System.arraycopy(Object src, int srcPos, Object dest, int destPos);
+ Position ADT, single method element()
------------------------------------------------------------------------------+ The location of an element within a structure
- getElement()
- first()
- last()
- before(p) //returns the position before p
- after(p) // returns the position after p
- isEmpty()
- size()
- Traversing position list
Position<String> cursor = guests.first();
while(cursor != null)
System.out.println(guests.getElement());
cursor = guests.after(cursor);
+ Updating a Position List
- addFirst(e)
- addLast(e)
- addBefore(p, e)
- addAfter(p, e)
- set(p, e)
- remove(p) //returns element, removes position
+ Position<E> addBetween(E e, Node<E> pred, Node<E> succ)
Node<E> tmp = new Node<>(e, pred, succ );
pred.setNext = tmp;
succ.setPrev = tmp;
size++;
return tmp;
+ Position<E> remove(Position<E> p)
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
E tmp = node.getElement();
node.setElement(null);
node.setPrev(null);
node.setNext(null);
return tmp;
+ Iterator
-------------------------------------------------------------------------------