CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

Similar documents
Lecture 12: Doubly Linked Lists

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

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

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Chapter 9 Lists. Concepts: The list abstraction Singly linked lists Doubly linked lists Circular lists Vectors as lists

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

COMP 250. Lecture 8. stack. Sept. 25, 2017

Computer Science 136 Exam 2

CS/CE 2336 Computer Science II

doubly linked lists Java LinkedList

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

CS171 Midterm Exam. October 29, Name:

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Lecture 4. The Java Collections Framework

The Java Collections Framework. Chapters 7.5

Stacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so)

Queues Fall 2018 Margaret Reid-Miller

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

csci 210: Data Structures Stacks and Queues

Computer Science 136. Midterm Examination

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS

Introduction to Data Structures

CSC212:Data Structure

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

infix expressions (review)

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

cs Java: lecture #6

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Linked List. ape hen dog cat fox. tail. head. count 5

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

An Introduction to Data Structures

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

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

== isn t always equal?

CSC 321: Data Structures. Fall 2013

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

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

SOLUTIONS (INCOMPLETE, UNCHECKED)

Stacks Fall 2018 Margaret Reid-Miller

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

Java Review: Objects

COS 226 Algorithms and Data Structures Fall Midterm

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Implementing Dynamic Data Structures

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Tutorial 2: Linked Lists

Insertion Sort: an algorithm for sorting an array

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

CSC 321: Data Structures. Fall 2012

[2:3] Linked Lists, Stacks, Queues

Implementing Lists, Stacks, Queues, and Priority Queues

Iterators and Sequences

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

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

Abstract Data Types. Abstract Data Types

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

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Summer Final Exam Review Session August 5, 2009

1. Stack Implementation Using 1D Array

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Csci 102: Sample Exam

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Lists. CITS2200 Data Structures and Algorithms. Topic 9

CE204 Data Structures and Algorithms Part 2

CS 216 Exam 1 Fall SOLUTION

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

8. Fundamental Data Structures

CS 10, Fall 2015, Professor Prasad Jayanti

Stack and Queue. Stack:

Algorithms & Datastructures Laboratory Exercise Sheet 1

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

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

Notes on access restrictions

Transcription:

CSCI 136 Data Structures & Advanced Programming Lecture 13 Fall 2018 Instructors: Bill 2

Announcements Lab today! After mid-term we ll have some non-partner labs It s Lab5 not Lab 4 Mid-term exam is Wednesday, October 17 During your normal lab session You ll have approximately 1 hour & 45 minutes (if you come on time!) Closed-book: Covers Chapters 1-7 & 9, handouts, and all topics up through Linked Lists A sample mid-term and study sheet will be available online Review session: Monday, Oct. 15, 7:00-8:00pm TCL 203 2

Last Time Class extension Abstract base classes Concrete extension classes List: A general-purpose structure Implementing Lists with linked structures Singly and Doubly Linked Lists 3

Today Linked List Wrap-Up The structure5 hierarchy so far Linear Structures The Linear Interface (LIFO & FIFO) The AbstractLinear and AbstractStack classes Stack Implementations StackArray, StackVector, StackList, Stack applications Expression Evaluation PostScript: Page Description & Programming Mazerunning (Depth-First-Search) 4

DoublyLinkedLists Keep reference/links in both directions previous and next DoublyLinkedListNode instance variables DLLN next, DLLN prev, E value Space overhead is proportional to number of elements ALL operations on tail (including removelast) are fast! Additional work in each list operation Example: add(e d, int index) Four cases to consider now: empty list, add to front, add to tail, add in middle 5

public class DoublyLinkedNode<E> { protected E data; protected DoublyLinkedNode<E> nextelement; protected DoublyLinkedNode<E> previouselement; // Constructor inserts new node between existing nodes public DoublyLinkedNode(E v, { } data = v; DoublyLinkedNode<E> next, DoublyLinkedNode<E> previous) nextelement = next; if (nextelement!= null) // point next back to me nextelement.previouselement = this; previouselement = previous; if (previouselement!= null) // point previous to me previouselement.nextelement = this;

DoublyLinkedList Add Method public void add(int i, E o) { Assert.pre((0 <= i) && (i <= size()), "Index in range."); if (i == 0) addfirst(o); else if (i == size()) addlast(o); else { // Find items before and after insert point DoublyLinkedNode<E> before = null; DoublyLinkedNode<E> after = head; // search for ith position while (i > 0) { before = after; } after = after.next(); i--; // before, after refer to items in slots i-1 and i // continued on next slide

DoublyLinkedList Add Method // Note: Still in else block! // before, after refer to items in slots i-1 and i // create new value to insert in correct position // Use DLN constructor that takes parameters // to set its next and previous instance variables DoublyLinkedNode<E> current = new DoublyLinkedNode<E>(o,after,before); } } count++; // adjust size

public E remove(e value) { DoublyLinkedNode<E> finger = head; while ( finger!= null &&!finger.value().equals(value) ) finger = finger.next(); if (finger == null) return null; // fix next field of previous element if (finger.previous()!= null) finger.previous().setnext(finger.next()); else head = finger.next(); } // fix previous field of next element if (finger.next()!= null) finger.next().setprevious(finger.previous()); else tail = finger.previous(); count--; return finger.value();

CircularlyLinkedLists Use next reference of last element to reference head of list Replace head reference with tail reference Access head of list via tail.next ALL operations on head are still fast : O(1) time addlast() is now fast O(1) time Only modest additional complexity in implementation Can cyclically reorder list by changing tail node Question: What s a circularly linked list of size 1? 10

Duane s Structure Hierarchy The structure5 package has a hierarchical structure A collection of interfaces that describe---but do not implement---the functionality of one or more data structures A collection of abstract classes provide partial implementations of one or more data structures To factor out common code or instance variables A collection of concrete (fully implemented) classes to provide full functionality of a data structure 11

AbstractList Superclass abstract class AbstractList<E> implements List<E> { } public void addfirst(e element) { add(0, element); } public E getlast() { return get(size()-1);} public E removelast() { return remove(size()-1); } AbstractList provides some of the list functionality Code is shared among all sub-classes (see Ch. 7 for more info) public boolean isempty() { return size() == 0; } Concrete classes (SLL, DLL) can override the code implemented in AbstractList Abstract classes in general do not implement every method For example, size() is not defined although it is in the List interface Can t create an AbstractList directly Concrete list classes extend AbstractList, implementing missing functionality class Vector extends AbstractList { public int size() { return elementcount; } } 12

The Structure5 Universe (almost)

The Structure5 Universe (so far)

The Structure5 Universe (soon)

Linear Structures What if we want to impose access restrictions on our lists? I.e., provide only one way to add and remove elements from list No longer provide access to middle Key Examples: Order of removal depends on order elements were added LIFO: Last In First Out FIFO: First In First Out 16

Examples FIFO: First In First Out (Queue) Line at dining hall Data packets arriving at a router LIFO: Last In First Out (Stack) Stack of trays at dining hall Java Virtual Machine stack 17

The Structure5 Universe (next)

Linear Interface How should it differ from List interface? Should have fewer methods than List interface since we are limiting access Methods: Inherits all of the Structure interface methods add(e value) Add a value to the structure. E remove(e o) Remove value o from the structure. But this is awkward---why? int size(), isempty(), clear(), contains(e value), Adds E get() Preview the next object to be removed. E remove() Remove the next value from the structure. boolean empty() same as isempty() 19

Linear Structures Why no random access? I.e., no access to middle of list More restrictive than general List structures Less functionality can result in Simpler implementation Greater efficiency Approaches Use existing structures (Vector, LL), or Use underlying organization, but simplified 20

Stacks Examples: stack of trays or cups Can only take tray/cup from top of stack What methods do we need to define? Stack interface methods New terms: push, pop, peek Only use push, pop, peek when talking about stacks Push = add to top of stack Pop = remove from top of stack Peek = look at top of stack (do not remove) 21

Notes about Terminology When using stacks: pop = remove push = add peek = get In Stack interface, pop/push/peek methods call add/remove/get methods that are defined in Linear interface But add is not mentioned in Stack interface (it is inherited from Linear) Stack interface extends Linear interface Interfaces extend other interfaces Classes implement interfaces 22

Stack Implementations Array-based stack int top, Object data[ ] Add/remove from index top Vector-based stack Vector data Add/remove from tail List-based stack SLL data Add/remove from head + all operations are O(1) wasted/run out of space +/ most ops are O(1) (add is O(n) in worst case) potentially wasted space + all operations are O(1) +/ O(n) space overhead (no wasted space) 23

Stack Implementations structure5.stackarray int top, Object data[ ] Add/remove from index top structure5 StackVector Vector data Add/remove from tail structure5.stacklist SLL data Add/remove from head + all operations are O(1) wasted/run out of space +/ most ops are O(1) (add is O(n) in worst case) potentially wasted space + all operations are O(1) +/ O(n) space overhead (no wasted space) 24

Summary Notes on The Hierarchy Linear interface extends Structure add(e val), empty(), get(), remove(), size() AbstractLinear (partially) implements Linear AbstractStack class (partially) extends AbstractLinear Essentially introduces stack-ish names for methods push(e val) is add(e val), pop() is remove(), peek() is get() Now we can extend AbstractStack to make concrete Stack types StackArray<E>: holds an array of type E; add/remove at high end StackVector<E>: similar, but with a vector for dynamic growth StackList<E>: A singly-linked list with add/remove at head We implement add, empty, get, remove, size directly push, pop, peek are then indirectly implemented 25

The Structure5 Universe (so far)