Computer Science 3A - CSC3A10/CSC03A3: Lecture 2: Arrays, Linked Lists and Recursion
Computer Science 3A - CSC3A10/CSC03A3: Lecture 2: Arrays, Linked Lists and Recursion
Computer Science 3A - CSC3A10/CSC03A3: Lecture 2: Arrays, Linked Lists and Recursion
Computer Science 3A - CSC3A10/CSC03A3 1/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
1 Arrays
Array Properties
Arrays Implemented
2 Basic ADT’s
Position ADT
List ADT
3 Singly Linked Lists
Singly Linked List Properties
Node Class
Insertion
Removal
Stack with a Singly Linked List
Queue with a Singly Linked List
Singly Linked List Implementation
Singly Linked List Performance
4 Doubly Linked Lists
Computer Science 3A - CSC3A10/CSC03A3 2/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
6 Recursion
Recursion Properties
Recursion Examples
Content of a Recursive Method
Visualizing recursion
Types of Recursion
Linear Recursion
Tail Recursion
Computer Science 3A - CSC3A10/CSC03A3 3/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion
Multiple Recursion
Exercises
Computer Science 3A - CSC3A10/CSC03A3 4/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Arrays
Computer Science 3A - CSC3A10/CSC03A3 5/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Arrays
Storing in an array
Object to store (such as GameEntry)
High score class
Insertion
Removal
Computer Science 3A - CSC3A10/CSC03A3 6/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Arrays II
Sorting an array
Insertion sort
Algorithm
Java.util
Equals
fill
sort
toString
Computer Science 3A - CSC3A10/CSC03A3 7/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Arrays Implemented
Computer Science 3A - CSC3A10/CSC03A3 8/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Basic ADT’s
Computer Science 3A - CSC3A10/CSC03A3 9/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Postion ADT
The Position ADT models the notion of place within a data structure where a
single object is stored
It gives a unified view of diverse ways of storing data, such as
a cell of an array
a node of a linked list
Just one method:
object element(): returns the element stored at the position
Computer Science 3A - CSC3A10/CSC03A3 10/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
List ADT
Accessor methods:
The List ADT models a first()
sequence of positions last()
storing arbitrary objects prev(p)
next(p)
It establishes a
Update methods:
before/after relation
replace(p, e)
between positions
insertBefore(p, e),
Generic methods: insertAfter(p, e),
size() insertFirst(e)
isEmpty() insertLast(e)
remove(p)
Computer Science 3A - CSC3A10/CSC03A3 11/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 12/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 13/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 14/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
1 p u b l i c c l a s s Node {
2 // I n s t a n c e v a r i a b l e s :
3 private Object element ;
4 p r i v a t e Node n e x t ;
5 /∗ ∗ C r e a t e s a node w i t h n u l l r e f e r e n c e s t o i t s e l e m e n t and n e x t
node . ∗/
6 p u b l i c Node ( ) {
7 this ( null , null ) ;
8 }
9 /∗ ∗ C r e a t e s a node w i t h t h e g i v e n e l e m e n t and n e x t node . ∗/
10 p u b l i c Node ( O b j e c t e , Node n ) {
11 element = e ;
12 next = n ;
13 }
14 // A c c e s s o r methods :
15 public Object getElement () {
Computer Science 3A - CSC3A10/CSC03A3 15/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
16 return element ;
17 }
18 p u b l i c Node g e t N e x t ( ) {
19 return next ;
20 }
21 // M o d i f i e r methods :
22 p u b l i c v o i d s e t E l e m e n t ( O b j e c t newElem ) {
23 e l e m e n t = newElem ;
24 }
25 p u b l i c v o i d s e t N e x t ( Node newNext ) {
26 n e x t = newNext ;
27 }
28 }
Computer Science 3A - CSC3A10/CSC03A3 16/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 17/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 18/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 19/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 20/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 21/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 22/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 23/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 24/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 25/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 26/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 27/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 28/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 29/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 30/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 31/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 32/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
1 Algorithm i n s e r t A f t e r (p , e ) :
2 C r e a t e a new node v
3 v . setElement ( e )
4 v . setPrev ( p ) { l i n k v to i t s p r e d e c e s s o r }
5 v . setNext (p . getNext () ) { l i n k v to i t s s u c c e s s o r }
6 ( p . getNext () ) . setPrev ( v ) { l i n k p ’ s old s u c c e s s o r to v}
7 p . s e t N e x t ( v ) { l i n k p t o i t s new s u c c e s s o r , v }
8 r etu rn v { the p o s i t i o n f o r the element e}
insertAfter algorithm
Computer Science 3A - CSC3A10/CSC03A3 33/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 34/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 35/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 36/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
1 A l g o r i t h m remove ( p ) :
2 t = p . element {a temporary v a r i a b l e to hold the return v a l u e }
3 ( p . g e t P r e v ( ) ) . s e t N e x t ( p . g e t N e x t ( ) ) { l i n k i n g o u t p}
4 (p . getNext () ) . setPrev (p . getPrev () )
5 p . s e t P r e v ( n u l l ) { i n v a l i d a t i n g t h e p o s i t i o n p}
6 p . setNext ( null )
7 return t
remove algorithm
Computer Science 3A - CSC3A10/CSC03A3 37/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 38/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 39/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 40/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Some methods
add
remove
advance [the cursor]
Sorting (definitely easier on a doubly linked list!)
insertion sort (CF 3.27)
Java (CF 3.28)
Computer Science 3A - CSC3A10/CSC03A3 41/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Recursion
Computer Science 3A - CSC3A10/CSC03A3 42/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Recursion Properties
Computer Science 3A - CSC3A10/CSC03A3 43/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Recursion Examples
Computer Science 3A - CSC3A10/CSC03A3 44/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Recursion Examples II
Euclid’s Algorithm (Greatest Common Divisor):
(
a if b = 0,
For a, b ≥ 0, gcd(a, b) =
gcd(b, a mod b) otherwise
1 p u b l i c s t a t i c i n t gcd ( i n t a , i n t b )
2 {
3 i f ( b == 0 )
4 return a ;
5 e l s e i f ( a >= b && b > 0 )
6 r e t u r n gcd ( b , a % b ) ;
7 e l s e r e t u r n gcd ( b , a ) ;
8 }
(
1 if n = 0,
f (n) =
n · f (n − 1) else
Base case(s):
Values of the input variables for which we perform no recursive calls are called
base cases (there should be at least one base case).
Every possible chain of recursive calls must eventually reach a base case.
Recursive calls:
Calls to the current method.
Each recursive call should be defined so that it makes progress towards a base
case.
Computer Science 3A - CSC3A10/CSC03A3 47/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Visualizing recursion
Recursion trace
A box for each recursive
call
An arrow from each caller
to callee
An arrow from each callee
to caller showing return
value
Computer Science 3A - CSC3A10/CSC03A3 48/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Types of Recursion
Linear recursion
Tail recursion
Binary recursion
Multiple recursion
Computer Science 3A - CSC3A10/CSC03A3 49/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Linear Recursion
Computer Science 3A - CSC3A10/CSC03A3 50/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Linear Recursion II
1 A l g o r i t h m Li nea rS um (A , n ) :
2 I n p u t : A i n t e g e r a r r a y A and an i n t e g e r n = 1 , s u c h t h a t A h a s a t l e a s t
n elements
3 Output : The sum o f t h e f i r s t n i n t e g e r s i n A
4 i f n = 1 then
5 return A[ 0 ]
6 else
7 r e t u r n Li nea rS um (A , n − 1 ) + A [ n − 1 ]
Computer Science 3A - CSC3A10/CSC03A3 51/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 52/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Linear Recursion IV
1 A l g o r i t h m R e v e r s e A r r a y (A , i , j):
2 I n p u t : An a r r a y A and n o n n e g a t i v e i n t e g e r i n d i c e s i and j
3 Output : The r e v e r s a l o f t h e e l e m e n t s i n A s t a r t i n g a t i n d e x i and
ending at j
4 i f i < j then
5 Swap A [ i ] and A [ j ]
6 R e v e r s e A r r a y (A , i + 1 , j − 1)
7 return
Reverse Algorithm
Computer Science 3A - CSC3A10/CSC03A3 53/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Linear Recursion V
Computer Science 3A - CSC3A10/CSC03A3 54/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Tail Recursion
Tail recursion occurs when a linearly recursive method makes its recursive call as
its last step.
The array reversal method is an example.
Such methods can be easily converted to non-recursive methods (which saves on
some resources).
Computer Science 3A - CSC3A10/CSC03A3 55/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Tail Recursion II
For example:
1 A l g o r i t h m I t e r a t i v e R e v e r s e A r r a y (A , i , j ) :
2 I n p u t : An a r r a y A and n o n n e g a t i v e i n t e g e r i n d i c e s i and j
3 Output : The r e v e r s a l o f t h e e l e m e n t s i n A s t a r t i n g a t i n d e x i and
ending at j
4
5 w h i l e i < j do
6 Swap A [ i ] and A [ j ]
7 i = i + 1
8 j = j − 1
9 return
Reverse Algorithm
Computer Science 3A - CSC3A10/CSC03A3 56/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion
Binary Sum
Computer Science 3A - CSC3A10/CSC03A3 57/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion II
Computer Science 3A - CSC3A10/CSC03A3 58/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 59/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion IV
Binary Fibonacci
Computer Science 3A - CSC3A10/CSC03A3 60/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion V
Let nk denote number of recursive calls made by BinaryFib(k). Then
Note that the value at least doubles for every other value of nk . That is, nk > 2k/2 . It
is exponential!
Computer Science 3A - CSC3A10/CSC03A3 61/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Binary Recursion VI
Linear Fibonacci
Computer Science 3A - CSC3A10/CSC03A3 62/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Multiple Recursion
Multiple recursion: makes potentially many recursive calls (not just one or two).
Computer Science 3A - CSC3A10/CSC03A3 63/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Multiple Recursion II
1 A l g o r i t h m P u z z l e S o l v e ( k , S , U) :
2 I n p u t : An i n t e g e r k , s e q u e n c e S , and s e t U ( t h e e l e m e n t s u s e d t o t e s t )
3 Output : An e n u m e r a t i o n o f a l l k−l e n g t h e x t e n s i o n s t o S u s i n g e l e m e n t s
in U
4 without r e p e t i t i o n s
5
6 f o r a l l e i n U do
7 Remove e from U { e i s now b e i n g u s e d }
8 Add e t o t h e end o f S
9 i f k = 1 then
10 Test whether S i s a c o n f i g u r a t i o n that s o l v e s the p u z z l e
11 i f S s o l v e s the puzzle then
12 return ‘ ‘ S o l u t i o n found : ‘ ‘ S
13 else
14 P u z z l e S o l v e ( k − 1 , S , U)
15 Add e back t o U { e i s now u n u s e d }
16 Remove e from t h e end o f S
Computer Science 3A - CSC3A10/CSC03A3 64/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Multiple Recursion II
Example trace for PuzzleSolve(3,S,U) where S=() and U={a,b,c}
Computer Science 3A - CSC3A10/CSC03A3 65/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion
Computer Science 3A - CSC3A10/CSC03A3 66/66 Academy of Computer Science and Software Engineering