COSC2006 - Data Structures I Chapter 4 Data Abstraction The Walls.
-
Upload
amos-mcgee -
Category
Documents
-
view
226 -
download
3
Transcript of COSC2006 - Data Structures I Chapter 4 Data Abstraction The Walls.
COSC2006 - Data Structures I
Chapter 4Data Abstraction
The Walls
2
Topics
Introduction ADT Examples Specify ADT Implement ADT
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): A collection of data together with a set of
operations on that data
5
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
6
ADT
Program using
methods
Implementation of
methods
Request to perform
operation
Result of operation
Wall
Slit in Wall
7
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
Chilledwater
Crushedice
Icecubes
Out of ice indicator
Water
8
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
9
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
10
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
11
ADT List Specification
MethodDisplayList
Implementationof ADT-List
Retrieve item at index
DataItem
ADT Wall
CreateList
ListInsert
DestroyList
ListDelete
ListRetrieve
Client
Server
12
Using ADT List
Example: Display list data itemsdisplayList(in aList: List)// Displays the items on the list aLIstfor (Position = 1, through aList.getLength ( ) ){ aList . retrieve (position, dataItem, success)
Display aataItem} / / end for
Example: Replace an item with anotherreplace (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 successfulaList . remove (i, success)if (success)
aList . insert (i, newItem, success)
13
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
14
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
15
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
16
ADT List Array-Based Implementation
Data Structure:private final int MAX_LIST = 100; // max length of listprivate listItemType items [MAX_LIST] ; // array of list itemsprivate 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
17
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 . . . ? ? . . . ?
Size1 2 3 4 k MAX_LIST
0 1 2 3 k-1 MAX_LIST - 1
ADT list positions
Array indexes
Items
K+1 12 3 44 19 10 . . . 5 10 18 . . . ? . . . ?
Size1 2 3 4 k+1 MAX_LIST
0 1 2 3 k MAX_LIST - 1
New item
18
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 . . . ? . . . ?
Size1 2 3 4 k+1 MAX_LIST
0 1 2 3 k MAX_LIST - 1
ADT list positions
Array indexes
Items
K 12 3 44 10 . . . 5 10 18 . . . ? . . . ?
Size1 2 3 4 k MAX_LIST
0 1 2 3 k-1 MAX_LIST - 1
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 (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++; }
if (numItems >= MAX_LIST) { throw new ListException("ListException on add"); }
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