lists - smith college · typical actions • append at front, at end • insert in the middle •...

41
Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014

Upload: others

Post on 23-Oct-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

!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

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

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

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

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

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

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

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

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

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

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://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

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

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

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

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

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

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

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
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

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

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

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

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

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

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

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

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://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

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

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

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

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

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

Reverse Polish Notation