Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Types (ADT)

Similar presentations


Presentation on theme: "Abstract Data Types (ADT)"— Presentation transcript:

1

2 Abstract Data Types (ADT)
Procedural abstraction Information hiding

3 Figure 3.1 Isolated tasks: the implementation of task T does not affect task Q

4 Figure 3.2 A slit in the wall

5 Operations on data Add data to a data collection
Remove data from a data collection Ask questions about the data in a data collection

6 ADTs vs. Data Structures
An abstract data type -- a collection of data and a set of operations on that data. A data structure -- a construct within a programming language that stores a collection of data.

7 Figure 3.3 A dispenser of chilled water, crushed ice, and ice cubes

8 Figure 3.4 A wall of ADT operations isolates a data structure from the program that uses it

9 Figure 3.5 A grocery list

10 ADT List Operations Create an empty list.
Determine whether a list is empty. Determine the number of items on a list. Add an item at a given position in the list. Remove the item at a given position in the list. Remove all the items from the list. Retrieve (get) the item at a given position in the list.

11 ADT List Operations-Pseudocode
createList() isEmpty() size() add(index, item) remove(index) removeAll() get(index)

12 ADT List Operations-Pseudocode
To get a list of the following items: milk, eggs, butter, apples, bread, chicken First, declare aList as an object of List Then, aList.createList() aList.add(1, milk) aList.add(2, eggs) aList.add(3, butter) aList.add(4, apple) aList.add(5, bread) aList.add(6, chicken)

13 ADT List Operations-Pseudocode
After the following statement aList.add(4, nuts) The list will be milk, eggs, butter, nuts, apples, bread, chicken If the following operation is performed aList.remove(5) milk, eggs, butter, nuts, bread, chicken

14 Access and Manipulation of ADT’s Data
Once the behavior (basic operations) of an ADT is designed, access and manipulation of the ADT’s data can be defined based on its operations. displayList - displays all the items on the list aList. replace - replaces the ith item on the list aList with newItem.

15 Access and Manipulation of ADT’s Data - displayList
displayList(aList) for (index = 1 through aList.size()) { dataItem = aList.get(index) Display dataItem }

16 Access and Manipulation of ADT’s Data - replace
replace(aList, i, newItem) if (i >= 1 and i <= aList.size()) { aList.remove(i) aList.add(i, newItem) }

17 Figure 3.6 The wall between displayList and the implementation of the ADT list

18 ADT for appointment book
Make an appointment for a certain date, time, and purpose. Cancel the appointment for a certain date and time. Check whether you have an appointment at a given time and date. Determine the purpose of the appointment at a given time and date.

19 ADT appointment -Pseudocode
createAppointmentBook() isAppointment(date, time) makeAppointment(date, time, purpose) cancelAppointment(date, time) checkAppointment(date, time) More operations based on the operations above, e.g. change the date or time of a particular appointment within the existing appointment book. What needs to be done to achieve that?

20 ADT appointment -Pseudocode
Get the purpose of the old appointment Check if the new date and time is available to appointment If yes, make the appointment at new date and time. If no, remind the user of the existing appointment and the failure to make the new appointment.

21 Implementing ADTs in Java
Java Classes (class Sphere) Java Inheritance (class ColoredSphere) Java Interfaces Java Exceptions

22 Figure 3.7 ADT operations provide access to a data structure

23 Figure 3.8 Violating the wall of ADT operations

24 Figure 3.9 An object’s data and methods are encapsulated

25 An Array-Based Implementation of the ADT List
createList() isEmpty() size() add(newPosition, newItem) remove(index) removeAll() get(index, dataItem)

26 Figure 3.10 An array-based implementation of the ADT list

27 Figure 3.11 Shifting items for insertion at position 3

28 Figure 3.12 a) Deletion causes a gap; b) fill gap by shifting


Download ppt "Abstract Data Types (ADT)"

Similar presentations


Ads by Google