![Page 1: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/1.jpg)
Lists
CSC212 Lecture 8 D. Thiebaut, Fall 2014
![Page 2: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/2.jpg)
Review
• List =
• Organization of Data in a Linear Fashion, where Order is Important
• Set of actions that can be carried out efficiently on the data.
![Page 3: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/3.jpg)
Typical Actions• Append at front, at end
• Insert in the middle
• Remove from front, from end, in middle
• Get the length
• Test if empty
• Iterate over all elements
• Sort the list
![Page 4: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/4.jpg)
Can you think of a particular set of data
(in every-day life) you would want to
keep in a list?
![Page 5: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/5.jpg)
Three Examples• Road race: Keep track of arrival time for all runners
(Number, Time). What operations can you imagine?
• List of people who…
• List of email addresses of…
![Page 6: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/6.jpg)
Review VectorsReview Vectors
![Page 7: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/7.jpg)
!public class Generic1<T> { private T info; Generic1( T x ) { info = x; } public String toString() { return "info = " + info; }! static public void main( String[] args ) { Generic1<Integer> n = new Generic1( 100 ); Generic1<String> s = new Generic1( "Hello there!" ); System.out.println( n ); System.out.println( s ); }}
!public class Generic2 { private Object info; Generic2( Object x ) { info = x; } public String toString() { return "info = " + info; }! static public void main( String[] args ) { Generic2 n = new Generic2( (Integer) 100 ); Generic2 s = new Generic2( (String) "Hello there!" ); System.out.println( n ); System.out.println( s ); }}
A Note on Notation…
![Page 8: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/8.jpg)
A Small Detour… Assembly Language
5210101123126713-314715
Table
3 i; Table[i] = 0! !!
![Page 9: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/9.jpg)
A Small Detour… Assembly Language
5210101123126713-314715
Table
3 i; Table[i] = 0! mov reg, Table! add reg, mem(i)! mov mem(reg), 0
![Page 10: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/10.jpg)
A Small Detour… Assembly Language
5210101123126713-314715
Table
3 i; Table[i] = 0! mov reg, Table! add reg, mem(i)! mov mem(reg), 0
Accessing data in an array (vector) = CONSTANT TIME!~ 3 ns
![Page 11: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/11.jpg)
Evaluating the Efficiency of Vectors:
• Adding item at tail
• Adding item in front
• Adding item in middle
• Iterate over all elements
• Look at first item in list
• Look at last item in list
![Page 12: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/12.jpg)
A JAVA EXAMPLE 2 Different Syntaxes
![Page 13: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/13.jpg)
import java.util.Iterator;!import java.util.Vector;!!public class Vector2 {! static public void main( String[] args ) {! Vector<Integer> list = new Vector<Integer>( );!! for ( int i=0; i<10; i++ ) ! list.add( i * 3 );!! ! try {! System.out.println( "Element at 4 = " + list.get( 4 ) );! } catch (ArrayIndexOutOfBoundsException e ) {! // do nothing! }! ! ! Iterator<Integer> it = list.iterator();! while ( it.hasNext() ) {! int x = it.next();! System.out.println( x );! }!! }!}
![Page 14: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/14.jpg)
import java.util.Iterator;!import java.util.Vector;!!public class Vector1 {!! static public void main( String[] args ) {!! ! Vector list = new Vector( );!!! ! for ( int i=0; i<10; i++ ) !! ! ! list.add( (Integer) (i * 3) );!! ! !! ! try {!! ! ! System.out.println( "Element at 4 = " + list.get( 4 ) );!! ! } catch (ArrayIndexOutOfBoundsException e ) {!! ! ! // do nothing!! ! }! !! ! !! ! Iterator<Integer> it = list.iterator();!! ! while ( it.hasNext() ) {!! ! ! int x = it.next();!! ! ! System.out.println( x );!! ! }!! }!}
![Page 15: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/15.jpg)
LINKED-LISTS(Chapter 3 in Drozdek)
![Page 16: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/16.jpg)
infonext info
next
infonext
infonext
head
tail
![Page 17: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/17.jpg)
infonext info
next
infonext
infonext
head
tail
![Page 18: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/18.jpg)
https://www.youtube.com/watch?v=VlYH5smg2zo
![Page 19: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/19.jpg)
infonext class intSLLNode {!
!! public int info;! public intSLLNode next;!!! public intSLLNode( int i ) {! this( i, null );! }! public intSLLNode( int i, intSLLNode n ) {! info = i; ! next = n;! }!}
![Page 20: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/20.jpg)
Questions:• How do we implement a Linked List in Java,
once we have intSSLNode?
• What is an empty list?
• How do we mark the end of a list?
• How do we add an element at the front?
• How do we add an element at the end (tail)?
• How do we remove an element from the front? From the end?
![Page 21: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/21.jpg)
public class IntSLList {!! protected IntSLLNode head, tail;!! public IntSLList () {!! ! head = tail = null;!! }!! public boolean isEmpty() {!! ! return (head == null);!! }!! public void addToHead( int x ) {!! ! head = new IntSLLNode( x, head );!! ! if ( tail==null )!! ! ! tail = head;!! }!! public int deleteFromHead() {!! ! int el = head.info();!! ! if ( head==tail )!! ! ! head = tail = null;!! ! else head = head.next;!! ! return el;!! }!! // continues on Page 84 in Drozdek!}
![Page 22: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/22.jpg)
Evaluating the Efficiency of Linked Lists:
• Adding item at tail
• Adding item in front
• Adding item in middle
• Iterate over all
• Look at first item in list
• Look at last item in list
![Page 23: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/23.jpg)
Doubly-Linked Lists: Removing Some
of the Inefficiencies of Singly-Linked Lists
![Page 24: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/24.jpg)
head
tail
infonextprev
infonextprev
infonextprev
infonextprev
![Page 25: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/25.jpg)
Class Exercise
![Page 26: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/26.jpg)
![Page 27: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/27.jpg)
LISTEncapsulation
OOPModularization
![Page 28: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/28.jpg)
Java Lists: Vector
ArrayList LinkedList
![Page 29: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/29.jpg)
LinkedList
import java.util.LinkedList;!!class LinkedListExample {!!! public static void main(String[] args) {! // create a new linked list! LinkedList L = new LinkedList();! ! // add 5 integers to it.! for ( int i=10; i<=50; i+= 10 ) ! L.addFirst( i );! ! // display contents of list! System.out.println( L ); // <== ?! }!}
![Page 30: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/30.jpg)
ArrayList
import java.util.ArrayList;!public class ArrayListExample { public static void main(String[] args) { // create a new linked list ArrayList L = new ArrayList(); // add 5 integers to it. for ( int i=10; i<=50; i+= 10 ) L.add( i ); // display contents of list System.out.println( L ); // <== ? }}
![Page 31: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/31.jpg)
Stacks& Queues
![Page 32: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/32.jpg)
Queue
First In First Out = FIFO
![Page 33: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/33.jpg)
Operations
isEmpty()
enqueue()dequeue()
NOTE: Queue is an interface to LinkedLists.We'll skip examples of how to use them!
for right now…
![Page 34: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/34.jpg)
In Kyung Lee (Inky), Kinect,
and Queues
Photo from http://inkyunglee.me/performance/
![Page 35: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/35.jpg)
https://www.youtube.com/watch?v=DdpnICk9sa0
https://www.youtube.com/watch?v=i9bBkyG5zE8
Inky's movie
Inky's choreography
DT on fire https://www.youtube.com/watch?v=Hf6pQGIYQ6U
http://cs.smith.edu/classwiki/index.php/CSC400-Kinect-Based_Choreography
Research and Special Effects
![Page 36: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/36.jpg)
JAVA PROGRAM
![Page 37: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/37.jpg)
JAVA THREAD
JAVA THREAD
![Page 38: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/38.jpg)
JAVA THREA
D
JAVA THREA
D
Keywords: Threads Asynchronous Synchronized Data Structures
![Page 39: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/39.jpg)
Stack
Last In First Out = LIFO
Push() Pop()
Top()
isEmpty()
![Page 40: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/40.jpg)
import java.util.Stack; !public class StackExample { ! public static void main(String[] args) { Stack stack = new Stack(); for ( int i=10; i<= 50; i+=10 ) { System.out.println( "\nPushing " + i ); stack.push( i ); System.out.println( "The top of the stack is now " + stack.peek() ); } System.out.println( "\nStack: bottom --> " + stack + " <-- top\n" ); while ( ! stack.isEmpty() ) { int x = (int) stack.pop(); System.out.println( "Just popped " + x ); System.out.println( "The stack now contains " + stack.size() + " elements"); } } }
![Page 41: Lists - Smith College · Typical Actions • Append at front, at end • Insert in the middle • Remove from front, from end, in middle • Get the length • Test if empty • Iterate](https://reader030.vdocument.in/reader030/viewer/2022040713/5e18922006420432dc388cda/html5/thumbnails/41.jpg)
Reverse Polish Notation