An Array-Based Implementation of the ADT List

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Abstract Data Types. Typical operations on data  Add data to a data collection  Remove data from a data collection  Ask questions about the data in.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
April 24, 2017 Chapter 4 Data Abstraction The Walls
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Insertion sort Loop invariants Dynamic memory
Stack: a Linked Implementation
18 Chapter Stacks and Queues
Linked Lists Chapter 5 (continued)
Fundamentals of Programming II Working with Arrays
Lists Chapter 4.
Stacks.
Chapter 4 Linked Lists.
Implementing ArrayList Part 1
Chapter 4 Linked Lists
Pointers and Dynamic Variables
Data Abstraction A technique for increasing the modularity of a program The need to support several operations on the data arise need to define abstract.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks, Queues, and Deques
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Chapter 4 Linked Lists.
Arrays and Linked Lists
Programming in Java Lecture 11: ArrayList
Ch. 4 Data Abstraction Modular programming Procedural abstraction
Stacks, Queues, and Deques
Data Type (how to view it)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked List Intro CSCE 121 J. Michael Moore.
Arrays versus ArrayList
EE 422C Sets.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists Chapter 5 (continued)
Chapter 2 : List ADT Part I – Sequential List
Programming II (CS300) Chapter 07: Linked Lists
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked List Intro CSCE 121.
Dynamic Array: Implementation of Stack
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int numItems; // number of items in list

An Array-Based Implementation of the ADT List public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor

An Array-Based Implementation of the ADT List public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size

Insertion into Array What happens if you want to insert an item at a specified position in an existing array? Write over the current contents at the given index (which might not be appropriate), or The item originally at the given index must be moved up one position, and all the items after that index must shuffled up Illustrate on the board

An Array-Based Implementation of the ADT List public void add(int index, Object item) throws ListException, ListIndexOutOfBoundsException { if (numItems >= MAX_LIST) { throw new ListException("ListException on add:"+ " out of memory"); } // end if if (index < 1 || index > numItems+1) { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add");

An Array-Based Implementation of the ADT List // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) for (int pos = numItems; pos >= index; pos--) { items[pos] = items[pos-1]; } // end for // insert new item items[index-1] = item; numItems++; } //end add

Removal from Arrays What happens if you want to remove an item from a specified position in an existing array? Leave gaps in the array, i.e. indices that contain no elements, which in practice, means that the array element has to be given a special value to indicate that it is “empty”, or All the items after the (removed item’s) index must be shuffled down Illustrate on the board

An Array-Based Implementation of the ADT List public void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) for (int pos = index+1; pos <= size(); pos++) { items[pos-2] = items[pos-1]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if } // end remove

An Array-Based Implementation of the ADT List public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { return items[index-1]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get

An Array-Based Implementation - Summary Good things: Fast, random access of elements Very memory efficient, very little memory is required other than that needed to store the contents (but see bellow) Bad things: Slow deletion and insertion of elements Size must be known when the array is created and is fixed (static) Analyze running time of deletions (remove) and insertion (add). Count only # of times something is written to items.

ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList class is implemented using a dynamic array There is usually no limit on the size of such structures, other than the size of main memory Dynamic arrays are arrays that grow (or shrink) as required In fact a new array is created when the old array becomes full by creating a new array object, copying over the values from the old array and then assigning the new array to the existing array reference variable

Dynamic Array top = 4 6 1 7 8 1 2 3 4 5

Dynamic Array insert 5 top = 4 6 1 7 8 1 2 3 4 5

Dynamic Array top = 5 6 1 7 8 5 1 2 3 4 5

Dynamic Array top = 5 insert 2 6 1 7 8 5 1 2 3 4 5

Dynamic Array top = 6 insert 3 6 1 7 8 5 2 1 2 3 4 5

!The array is full and there is no room for a new item! Dynamic Array top = 6 insert 3 6 1 7 8 5 2 !The array is full and there is no room for a new item! 1 2 3 4 5

So we will create a new, bigger array … Dynamic Array top = 6 insert 3 6 1 7 8 5 2 So we will create a new, bigger array … 1 2 3 4 5

So we will create a new, bigger array … Dynamic Array top = 6 insert 3 6 1 7 8 5 2 So we will create a new, bigger array … 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11

… copy the elements of the old array into it … Dynamic Array top = 6 insert 3 6 1 7 8 5 2 … copy the elements of the old array into it … 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11

… copy the elements of the old array into it … Dynamic Array insert 3 6 1 7 8 5 2 … copy the elements of the old array into it … 1 2 3 4 5 top = 6 6 1 7 8 5 2 1 2 3 4 5 6 7 8 9 10 11

… and finally insert 3 into the new array. Dynamic Array insert 3 6 1 7 8 5 2 … and finally insert 3 into the new array. 1 2 3 4 5 top = 7 6 1 7 8 5 2 3 1 2 3 4 5 6 7 8 9 10 11

The old array will eventually be deleted by Java’s garbage collector Dynamic Array The old array will eventually be deleted by Java’s garbage collector 6 1 7 8 5 2 3 4 top = 7 6 1 7 8 5 2 3 1 2 3 4 5 6 7 8 9 10 11

Dynamic Array Summary Before every insertion, check to see if the array needs to grow Question: When growing array, how much to grow it? Memory efficient? (by 1) Time efficient? 1) How much writing operations we need to grow the array? Assume we are always inserting in the middle of array. 2) Analyze different resizing strategies

Dynamic Array Summary Before every insertion, check to see if the array needs to grow Growing by doubling works well in practice, because it grows very large very quickly 10, 20, 40, 80, 160, 320, 640, 1280, … Very few array re-sizings must be done To insert n items you need to do  log(n) re-sizings While the copying operation is expensive it does not have to be done often

Dynamic Array Problems When the doubling does happen it may be time-consuming And, right after a doubling half the array is empty Re-sizing after each insertion would be prohibitively slow Deleting and inserting in the middle (on average) is still O(n)