April 24, 2017 Chapter 4 Data Abstraction The Walls

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

Linked Lists Chapter 4.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
CMPT 225 Abstract Data Types.
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.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction: The Walls
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 3 Data.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Chapter 5 Linked Lists II
© 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.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.
1 Object-Oriented Programming Using C++ CLASS 6. 2 Specifying ADTs: The ADT List Except for the first and last items in a list, each item has a unique.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
CSC 205 Java Programming II Abstract Data Type. Abstraction The meaning of abstraction Abstraction arises from a recognition of similarities between certain.
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Data Abstraction: The Walls
Linked Lists Chapter 5 (continued)
Abstract Data Types (ADT)
Data Abstraction: The Walls
Lists Chapter 4.
Stacks.
Chapter 4 Linked Lists
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.
Ch. 4 Data Abstraction Modular programming Procedural abstraction
Abstract Data Types (ADT)
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Data Abstraction: The Walls
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.
Lecture 11 Abstract Data Type
Linked Lists Chapter 5 (continued)
Data Abstraction: The Walls
Linked Lists Chapter 5 (continued)
Presentation transcript:

COSC2006 - Data Structures I April 24, 2017 Chapter 4 Data Abstraction The Walls Chapter 3: Data Abstraction. The Walls

Topics Introduction ADT Examples Specify ADT Implement ADT COSC 2006 April 24, 2017 Topics Introduction ADT Examples Specify ADT Implement ADT Chapter 3: Data Abstraction. The Walls

Information Hiding: Why? “You don’t want to know.” … but also … “If I told you, I’d have to kill you!” … OK, not quite that extreme, but … “A little knowledge is a dangerous thing.”

ADT Abstract data type (ADT): COSC 2006 April 24, 2017 ADT Abstract data type (ADT): A collection of data together with a set of operations on that data Chapter 3: Data Abstraction. The Walls

COSC 2006 April 24, 2017 ADT Why? During a design of a solution, we need to support operations on data (Specification) Design an ADT Specify the operations (Interface) that enable communication with the data After defining ADT operations (specification), we consider their implementation (Implementation) Specify the data structure Specify the details of how operations work Chapter 3: Data Abstraction. The Walls

Request to perform operation COSC 2006 April 24, 2017 ADT Program using method s Implementation of Request to perform operation Result of operation Wall Slit in Wall Chapter 3: Data Abstraction. The Walls

ADT Example: Ice Dispenser COSC 2006 April 24, 2017 ADT Example: Ice Dispenser Specification Data Input: water Output: chilled water, crushed ice, ice cubes, or red light Operations: Chill, Crush, Cube, IsEmpty Don’t care how operations are done inside Chilled water Crushed ice Ice cubes Out of ice indicator Water Chapter 3: Data Abstraction. The Walls

ADT Example: Ice Dispenser COSC 2006 April 24, 2017 ADT Example: Ice Dispenser Implementation: Internal structure of the dispenser Manual Mechanical Electronic We can improve an operation by modifying its module We can add another operation by adding another module to the machine without affecting the other modules Chapter 3: Data Abstraction. The Walls

ADT List Specification Data characteristics Items appear in sequence COSC 2006 April 24, 2017 ADT List Specification Data characteristics Items appear in sequence Has one first element One last element The first item is called the head (front) The last item is called the tail (rear/end) Items are of the same type Chapter 3: Data Abstraction. The Walls

ADT List Specification Operations (Behavior): Create an empty list COSC 2006 April 24, 2017 ADT List Specification Operations (Behavior): Create an empty list Destroy a list Determine whether a list is empty Determine the number of items in the list Insert an item in a given position Delete an item at a given position Retrieve (look at) an item at a given position Chapter 3: Data Abstraction. The Walls

ADT List Specification COSC 2006 April 24, 2017 ADT List Specification Method DisplayList Implementation of ADT-List Retrieve item at index DataItem ADT Wall CreateList ListInsert DestroyList ListDelete ListRetrieve Client Server Chapter 3: Data Abstraction. The Walls

Using ADT List Example: Display list data items COSC 2006 April 24, 2017 Using ADT List Example: Display list data items displayList(in aList: List) // Displays the items on the list aLIst for (Position = 1, through aList.getLength ( ) ) { aList . retrieve (position, dataItem, success) Display aataItem } / / end for Example: Replace an item with another replace (in aList: List, in i: integer, in newItem: ListItemType, out success: boolean) // Replaces the ith item on the list aList with newItem. // success flag indicates whether the replacement was successful aList . remove (i, success) if (success) aList . insert (i, newItem, success) Chapter 3: Data Abstraction. The Walls

Designing an ADT Case Study: List Holidays Problem: Specification: COSC 2006 April 24, 2017 Designing an ADT Case Study: List Holidays Problem: Determine the dates of all holidays in a given year Specification: Data: Month Day Year Operations: Determine date of fist day of a given year firstDay( in year: integer) Determine whether a date is before another date isBefore(in Date1: Date, in Date2: Date) Determine whether a date is a holiday isHoliday(in aDate: Date) Determine the date following a given date nextDay(in aDate: Date) Assumption: Days in a year form a sorted list The list is accessed by giving the year number Chapter 3: Data Abstraction. The Walls

Designing an ADT Case Study: List Holidays COSC 2006 April 24, 2017 Designing an ADT Case Study: List Holidays Using ADT List-Holidays Operations: After specifying the operations, we can use them to solve other problems independent of the implementation details of the ADT. listHolydays ( in year : integer ) // Displays the dates of of all holidays in a given year. { date = firstDay ( year ) while ( isBefore ( date, firstDay ( year + 1 ))) { if ( isHoliday ( date )) write ( date, “ is a holiday “ ) date = nextDay ( date ) } // end while } // end listHolidays Chapter 3: Data Abstraction. The Walls

Implementing ADT Choose the data structure Implement operations: COSC 2006 April 24, 2017 Implementing ADT Choose the data structure Implement operations: Write the functions definition that access the data according to the ADT operations Both the data structures & the functions should be hidden from the client Chapter 3: Data Abstraction. The Walls

ADT List Array-Based Implementation COSC 2006 April 24, 2017 ADT List Array-Based Implementation Data Structure: private final int MAX_LIST = 100; // max length of list private listItemType items [MAX_LIST] ; // array of list items private int numItems; // length of list Each operation will need access to both array Items & the list's length Size , They should be made as private data members of the class Implementation of Operations Extra function needed Index (Position) Defined to return the index value, since the client can't access private data members Chapter 3: Data Abstraction. The Walls

ADT List Array-Based Implementation COSC 2006 April 24, 2017 ADT List Array-Based Implementation Insert Item To insert an item at a given position Shift items from the position on to the right, then insert the new item ADT list positions Array indexes Items K 12 3 19 10 . . . 5 10 18 . . . ? ? . . . ? Size 1 2 3 4 k MAX_LIST 0 1 2 3 k-1 MAX_LIST - 1 K+1 12 3 44 19 10 . . . 5 10 18 . . . ? . . . ? 1 2 3 4 k+1 MAX_LIST 0 1 2 3 k MAX_LIST - 1 New item Chapter 3: Data Abstraction. The Walls

ADT List Array-Based Implementation COSC 2006 April 24, 2017 ADT List Array-Based Implementation Delete Item To delete an item Shift the elements to the left to fill the gap left by the deleted item ADT list positions Array indexes Items K+1 12 3 44 19 10 . . . 5 10 18 . . . ? . . . ? Size 1 2 3 4 k+1 MAX_LIST 0 1 2 3 k MAX_LIST - 1 K 12 3 44 10 . . . 5 10 18 . . . ? . . . ? 1 2 3 4 k MAX_LIST 0 1 2 3 k-1 MAX_LIST - 1 Chapter 3: Data Abstraction. The Walls

Array Implementation of List: Issues Array has fixed physical size; List ADT has variable logical size ADT-specific exceptions are informative The array and its logical size are private data fields Gaps are bad: must shift elements when adding or deleting

Array Implementation (page 1) / ******************************************************** // 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 public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor private int translate(int position) { return position - 1; } // end translate

Array Implementation (page 1) / ******************************************************** // 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 public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor private int translate(int position) { return position - 1; } // end translate

Array Implementation (page 2) public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size public void removeAll() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; numItems = 0; } // end removeAll

Array Implementation (page 2) public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size public void removeAll() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; numItems = 0; } // end removeAll

Array Implementation (page 3) public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (numItems >= MAX_LIST) { throw new ListException("ListException on add"); } if (index >= 1 && index <= numItems+1) { // 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[translate(pos+1)] = items[translate(pos)]; } // insert new item items[translate(index)] = item; numItems++; else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add"); } } //end add

Array Implementation (page 4) 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[translate(pos-1)] = items[translate(pos)]; } numItems--; else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } //end remove Trace this … notice something funny at the end?

Array Implementation (page 5) public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { return items[translate(index)]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end get } // end ListArrayBased

Review The specifications of an ADT’s operations indicate ______. what the operations do how to implement the operations how to store the data in the ADT how to carry out the operations

Review A(n) ______ allows two modules to communicate with each other. data structure axiom Interface client

Review In the following list: John, Kate, Fred, Mark, Jon, Adam, Drew which element is the tail of the list? John Mark Drew Adam

Review The items in the ADT list are referenced by ______. name value position number position name

Review The insertion operation of the ADT list can insert new items ______. only at the front of the list only at the end of the list only in the middle of the list into any position of the list

Review In the ADT list, when an item is deleted from position i of the list, ______. the position of all items is decreased by 1 the position of each item that was at a position smaller than i is decreased by 1 the position of each item that was at a position greater than i is decreased by 1 the position of each item that was at a position smaller than i is increased by 1 while the position of each item that was at a position greater than i is decreased by 1

Review Which of the following operations of the ADT list changes the list? remove isEmpty Size get