Presentation is loading. Please wait.

Presentation is loading. Please wait.

April 24, 2017 Chapter 4 Data Abstraction The Walls

Similar presentations


Presentation on theme: "April 24, 2017 Chapter 4 Data Abstraction The Walls"— Presentation transcript:

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

2 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

3 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.”

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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 ? ? ? Size k MAX_LIST k MAX_LIST - 1 K+1 ? ? k MAX_LIST k MAX_LIST - 1 New item Chapter 3: Data Abstraction. The Walls

18 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 ? ? Size k MAX_LIST k MAX_LIST - 1 K ? ? k MAX_LIST k MAX_LIST - 1 Chapter 3: Data Abstraction. The Walls

19 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

20 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

21 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

22 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

23 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

24 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

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

26 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

27 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

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

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

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

31 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

32 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

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


Download ppt "April 24, 2017 Chapter 4 Data Abstraction The Walls"

Similar presentations


Ads by Google