Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Similar documents
16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Linked Structures. See Section 3.2 of the text.

Interfaces & Generics

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

csci 210: Data Structures Stacks and Queues

CS24 Week 4 Lecture 2

C27a: Stack and Queue

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Notes on access restrictions

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

cs Java: lecture #6

Linked List Nodes (reminder)

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Generics. IRS W-9 Form

More Data Structures (Part 1) Stacks

Basic Data Structures

An Introduction to Data Structures

Data Structures And Algorithms

Basic Data Structures 1 / 24

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Stacks and Queues

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Dynamic Data Structures

Introduction to Software Design

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Collections & Maps. Lecture 12. Collections & Maps

Stacks and Queues. David Greenstein Monta Vista

Data Structures Brett Bernstein

Introduction to Java. CSC212 Lecture 6 D. Thiebaut, Fall 2014

CS171 Midterm Exam. October 29, Name:

DNHI Homework 3 Solutions List, Stacs and Queues

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

16. Dynamic Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

CS 314 Exam 2 Fall 2017

CS2012 Programming Techniques II

Data Structures and Algorithms

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

1 P age DS & OOPS / UNIT II

8. Fundamental Data Structures

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Queues Fall 2018 Margaret Reid-Miller

11. Collections of Objects

Implementing Lists, Stacks, Queues, and Priority Queues

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CS 314 Exam 2 Spring

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

ECE Fall st Midterm Examination (120 Minutes, closed book)

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Fun facts about recursion

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Advanced Java Concepts Unit 3: Stacks and Queues

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Csci 102: Sample Exam

Stacks and Queues. Fundamentals of Computer Science.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 143. Computer Programming II

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Strux Language Reference Manual

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Standard ADTs. Lecture 19 CS2110 Summer 2009

CS 216 Exam 1 Fall SOLUTION

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

ITI Introduction to Computing II

1. Introduction. Lecture Content

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Advanced Java Concepts Unit 2: Linked Lists.

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Spring 2018 Discussion 6: February 19, 2018

Data Abstraction and Specification of ADTs

Complexity, General. Standard approach: count the number of primitive operations executed.

1. Stack Implementation Using 1D Array

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Linked List. April 2, 2007 Programming and Data Structure 1

PA3 Design Specification

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Stacks Fall 2018 Margaret Reid-Miller

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Strux. Sophie Stadler srs2231 Manager Millie Yang my2440 Tester. 26 September 2017

14. Stacks and Queues

Transcription:

Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014

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.

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

Can you think of a particular set of data (in every-day life) you would want to keep in a list?

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

Review Vectors

A Note! public class Generic2 { private Object info; on Notation! public class Generic1<T> { private T info;! Generic2( Object x ) { info = x; } public String tostring() { return "info = " + info; } static public void main( String[] args ) { Generic2 n = new Generic2( (Integer) 100 );! Generic1( T x ) { info = x; } public String tostring() { return "info = " + info; } static public void main( String[] args ) { Generic1<Integer> n = new Generic1( 100 ); } } Generic2 s = new Generic2( (String) "Hello there!" ); System.out.println( n ); System.out.println( s ); Generic1<String> s = new Generic1( "Hello there!" ); } } System.out.println( n ); System.out.println( s );

A Small Detour Assembly Language 10 11 52 10 Table 12 23 ; Table[i] = 0! 13 67 i 3! 14-3! 15 7

A Small Detour Assembly Language 10 11 52 10 Table 12 23 ; Table[i] = 0! 13 67 i 3 mov reg, Table! 14-3 add reg, mem(i)! 15 7 mov mem(reg), 0

A Small Detour Assembly Language 10 11 52 10 Table 12 23 ; Table[i] = 0! 13 67 i 3 mov reg, Table! 14-3 add reg, mem(i)! 15 7 mov mem(reg), 0 Accessing data in an array (vector) = CONSTANT TIME! ~ 3 ns

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

A JAVA EXAMPLE 2 Different Syntaxes

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 );! }!! }! }

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 );!!! }!! }! }

LINKED-LISTS (Chapter 3 in Drozdek)

head tail info next info next info next info next

head tail info next info next info next info next

https://www.youtube.com/watch?v=vlyh5smg2zo

info next 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;! }! }

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?

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

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

Doubly-Linked Lists: Removing Some of the Inefficiencies of Singly-Linked Lists

head tail info next prev info next prev info next prev info next prev

Class Exercise

LIST Modularization OOP Encapsulation

Java Lists: Vector ArrayList 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 ); // <==?! }! } LinkedList

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 ); // <==? ArrayList

Stacks & Queues

Queue First In First Out = FIFO

Operations dequeue() enqueue() isempty() NOTE: Queue is an interface to LinkedLists. We'll skip examples of how to use them! for right now

In Kyung Lee (Inky), Kinect, and Queues Photo from http://inkyunglee.me/performance/

Inky's movie https://www.youtube.com/watch?v=ddpnick9sa0 Inky's choreography DT on fire https://www.youtube.com/watch?v=i9bbkyg5ze8 https://www.youtube.com/watch?v=hf6pqgiyq6u Research and Special Effects http://cs.smith.edu/classwiki/index.php/csc400-kinect- Based_Choreography

JAVA PROGRAM

JAVA THREAD JAVA THREAD

JAVA THREA D JAVA THREA D Keywords: Threads Asynchronous Synchronized Data Structures

Stack Push() Pop() Top() isempty() Last In First Out = LIFO

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"); }

Reverse Polish Notation