COSC 2006 Data Structures I Chapter 4 Data

  • Slides: 33
Download presentation
COSC 2006 - Data Structures I Chapter 4 Data Abstraction The Walls

COSC 2006 - Data Structures I Chapter 4 Data Abstraction The Walls

Topics n n 2 Introduction ADT Examples Specify ADT Implement ADT

Topics n n 2 Introduction ADT Examples Specify ADT Implement ADT

Information Hiding: Why? n “You don’t want to know. ” … but also …

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

ADT n Abstract data type (ADT): n 4 A collection of data together with

ADT n Abstract data type (ADT): n 4 A collection of data together with a set of operations on that data

ADT Why? n During a design of a solution, we need to support operations

ADT Why? n During a design of a solution, we need to support operations on data (Specification) n n n After defining ADT operations (specification), we consider their implementation (Implementation) n n 5 Design an ADT Specify the operations (Interface) that enable communication with the data Specify the data structure Specify the details of how operations work

ADT Wall Request to perform operation Program using method s 6 Slit in Wall

ADT Wall Request to perform operation Program using method s 6 Slit in Wall Result of operation Implementation of method s

ADT Example: Ice Dispenser n Specification n Data n n n Input: water Output:

ADT Example: Ice Dispenser n Specification n Data n n n Input: water Output: chilled water, crushed ice, ice cubes, or red light Operations: Chill, Crush, Cube, Is. Empty n Don’t care how operations are done inside n Chilled water 7 Water Crushed ice Ice cubes Out of ice indicator

ADT Example: Ice Dispenser n Implementation: n Internal structure of the dispenser n n

ADT Example: Ice Dispenser n Implementation: n Internal structure of the dispenser n n n 8 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

ADT List n Specification n Data characteristics Items appear in sequence n Has one

ADT List n Specification n Data characteristics Items appear in sequence n Has one first element n One last element n The first item is called the head (front) n The last item is called the tail (rear/end) n Items are of the same type n 9

ADT List n Specification n Operations (Behavior): Create an empty list n Destroy a

ADT List n Specification n Operations (Behavior): Create an empty list n Destroy a list n Determine whether a list is empty n Determine the number of items in the list n Insert an item in a given position n Delete an item at a given position n Retrieve (look at) an item at a given position n 10

ADT List Specification Create. List Method Display. List Retrieve item at index Data. Item

ADT List Specification Create. List Method Display. List Retrieve item at index Data. Item Destroy. List. Insert List. Delete List. Retrieve Client ADT Wall Server 11 Implementation of ADT-List

Using ADT List n Example: Display list data items n Example: Replace an item

Using ADT List n Example: Display list data items n Example: Replace an item with another 12 display. List(in a. List: List) // Displays the items on the list a. LIst for (Position = 1, through a. List. get. Length ( ) ) { a. List. retrieve (position, data. Item, success) Display aata. Item } / / end for replace (in a. List: List, in i: integer, in new. Item: List. Item. Type, out success: boolean) // Replaces the ith item on the list a. List with new. Item. // success flag indicates whether the replacement was successful a. List. remove (i, success) if (success) a. List. insert (i, new. Item, success)

Designing an ADT n Case Study: List Holidays n Problem: n n Determine the

Designing an ADT n Case Study: List Holidays n Problem: n n Determine the dates of all holidays in a given year Specification: n Data: n n Operations: n n n Determine date of fist day of a given year first. Day( in year: integer) Determine whether a date is before another date is. Before(in Date 1: Date, in Date 2: Date) Determine whether a date is a holiday is. Holiday(in a. Date: Date) Determine the date following a given date next. Day(in a. Date: Date) Assumption: n n 13 Month Day Year Days in a year form a sorted list The list is accessed by giving the year number

Designing an ADT n Case Study: List Holidays n Using ADT List-Holidays Operations: n

Designing an ADT n Case Study: List Holidays n Using ADT List-Holidays Operations: n After specifying the operations, we can use them to solve other problems independent of the implementation details of the ADT. list. Holydays ( in year : integer ) // Displays the dates of of all holidays in a given year. { date = first. Day ( year ) while ( is. Before ( date, first. Day ( year + 1 ))) { if ( is. Holiday ( date )) write ( date, “ is a holiday “ ) date = next. Day ( date ) } // end while } // end list. Holidays 14

Implementing ADT • • Choose the data structure Implement operations: Write the functions definition

Implementing ADT • • Choose the data structure Implement operations: Write the functions definition that access the data according to the ADT operations n Both the data structures & the functions should be hidden from the client n 15

ADT List Array-Based Implementation n Data Structure: private final int MAX_LIST = 100; //

ADT List Array-Based Implementation n Data Structure: private final int MAX_LIST = 100; // max length of list private list. Item. Type items [MAX_LIST] ; // array of list items private int num. Items; // 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 n Extra function needed n n n Index (Position) n 16 Defined to return the index value, since the client can't access private data members

ADT List Array-Based Implementation Insert Item n n To insert an item at a

ADT List Array-Based Implementation Insert Item n n To insert an item at a given position n Shift items from the position on to the right, then insert the new item Array indexes 0 K Size 1 12 1 2 3 k-1 3 19 10 2 3 4 . . . 5 10 18 Items k K+1 Size 17 12 1 1 2 3 3 44 19 2 4 3 . . . ? ? . . . ? MAX_LIST ADT list positions Array indexes New item 0 MAX_LIST - 1 k 10 . . . Items 5 10 18. . . k+1 ADT list positions MAX_LIST - 1 ? . . . ? MAX_LIST

ADT List Array-Based Implementation n Delete Item n To delete an item n Shift

ADT List Array-Based Implementation n Delete Item n To delete an item n Shift the elements to the left to fill the gap left by the deleted item Array indexes 0 K+1 Size 1 12 1 2 3 3 44 19 2 4 3 k 10 . . . 5 10 MAX_LIST - 1 18. . . Items ? k+1 . . . ? MAX_LIST ADT list positions Array indexes 0 K 18 Size 12 1 1 3 2 2 3 44 3 k-1 10 4 . . . Items 5 10 MAX_LIST - 1 18 k ADT list positions . . . ? . . . ? MAX_LIST

Array Implementation of List: Issues n n 19 Array has fixed physical size; List

Array Implementation of List: Issues n n 19 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. //

Array Implementation (page 1) / **************************** // Array-based implementation of the ADT list. // ***************************** public class List. Array. Based implements List. Interface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int num. Items; // number of items in list public List. Array. Based() { items = new Object[MAX_LIST]; num. Items = 0; } // end default constructor private int translate(int position) { return position - 1; } // end translate 20

Array Implementation (page 1) / **************************** // Array-based implementation of the ADT list. //

Array Implementation (page 1) / **************************** // Array-based implementation of the ADT list. // ***************************** public class List. Array. Based implements List. Interface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int num. Items; // number of items in list public List. Array. Based() { items = new Object[MAX_LIST]; num. Items = 0; } // end default constructor private int translate(int position) { return position - 1; } // end translate 21

Array Implementation (page 2) public boolean is. Empty() { return (num. Items == 0);

Array Implementation (page 2) public boolean is. Empty() { return (num. Items == 0); } // end is. Empty public int size() { return num. Items; } // end size public void remove. All() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; num. Items = 0; } // end remove. All 22

Array Implementation (page 2) public boolean is. Empty() { return (num. Items == 0);

Array Implementation (page 2) public boolean is. Empty() { return (num. Items == 0); } // end is. Empty public int size() { return num. Items; } // end size public void remove. All() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; num. Items = 0; } // end remove. All 23

Array Implementation (page 3) public void add(int index, Object item) throws List. Index. Out.

Array Implementation (page 3) public void add(int index, Object item) throws List. Index. Out. Of. Bounds. Exception { if (num. Items >= MAX_LIST) { throw new List. Exception("List. Exception on add"); } if (index >= 1 && index <= num. Items+1) { // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == num. Items+1) for (int pos = num. Items; pos >= index; pos--) { items[translate(pos+1)] = items[translate(pos)]; } // insert new items[translate(index)] = item; num. Items++; } 24 else { // index out of range throw new List. Index. Out. Of. Bounds. Exception( "List. Index. Out. Of. Bounds. Exception on add"); } } //end add

Array Implementation (page 4) public void remove(int index) throws List. Index. Out. Of. Bounds.

Array Implementation (page 4) public void remove(int index) throws List. Index. Out. Of. Bounds. Exception { if (index >= 1 && index <= num. Items) { // 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)]; } num. Items--; Trace this … notice something funny at the end? } else { // index out of range throw new List. Index. Out. Of. Bounds. Exception( "List. Index. Out. Of. Bounds. Exception on remove"); } } //end remove 25

Array Implementation (page 5) public Object get(int index) throws List. Index. Out. Of. Bounds.

Array Implementation (page 5) public Object get(int index) throws List. Index. Out. Of. Bounds. Exception { if (index >= 1 && index <= num. Items) { return items[translate(index)]; } else { // index out of range throw new List. Index. Out. Of. Bounds. Exception( "List. Index. Out. Of. Bounds. Exception on get"); } } // end get } 26 // end List. Array. Based

Review n The specifications of an ADT’s operations indicate ______. n n 27 what

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

Review n A(n) ______ allows two modules to communicate with each other. n n

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

Review n n n In the following list: John, Kate, Fred, Mark, Jon, Adam,

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

Review n The items in the ADT list are referenced by ______. n n

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

Review n The insertion operation of the ADT list can insert new items ______.

Review n The insertion operation of the ADT list can insert new items ______. n n 31 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 n In the ADT list, when an item is deleted from position i

Review n In the ADT list, when an item is deleted from position i of the list, ______. n n 32 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 n Which of the following operations of the ADT list changes the list?

Review n Which of the following operations of the ADT list changes the list? n n 33 remove is. Empty Size get