### Abstract Data Type: the concept

• What is an "Abstract Data Type"

• Abstract Data Type:

 Abstract data type (ADT) = a mathematical model for a data structures where the behavior (of the data object) is defined by a set of operation

• Example: the stack

• A stack abstract data type is a data structure that provide the following operations:

Note:

 The stack ADT must remember the order in which the data items were pushed into the stack ADT !!!

• pop(): remove (and return) the last data item that was added by the push() operation

• Illustrated:

• Initially, the stack is empty:

• After the operation push(4):

• After the operation push(7):

• After the operation push(7):

Note: the pop() operation returns the value 7 !

Note:

• A stack maintains the data items in a Last In First Out (LIFO) manner:

 The last item that was added to the stack will be removed first

• Separating the functionality and the implementation

• A method (in any programming language) has 2 aspects:

• What is the method trying to accomplish

 This aspect is called the functionality of the method

• How does the method accomplish the job

 This aspect is called the implementation of the method

• Fact:

 The same functionality can be implemented in many different ways

• Example:

• There are many different ways to sort a list of items

We will study the exact algorithms later in this course

• Here are 2 possible ways:

• Sort method 1 (selection sort)

 Find the smallest item in in the unsorted part Exchange this smallest item with the first item in the unsorted part Repeat this n−1 times (where n is the number of items)

Example 1:

 ``` 5 6 1 4 Exchange 1 <--> 5 1 6 5 4 Exchange 4 <--> 6 1 4 5 6 Keep 5 6 ```

• Sort method 2 (insertion sort)

 Sort the first 2 cards Sort the 3rd card by inserting it into the correct position Sort the 4th card by inserting it into the correct position Repeat this n−1 times (where n is the number of items)

Example:

 ``` 5 6 1 4 keep 5 6 in same order 5 6 1 4 insert 1 in position 1 1 5 6 4 insert 4 in position 2 1 4 5 6 ```

• Therefore:

 The sort function (functionality) can be implemented in many different ways

Example 2:

• We will see later in this course:

• There are many different ways to implement the functionality of a stack ADT:

 Using an array (you may have seen this in CS170 already) Using a linked list

• Understanding functionality and implementation using a popular idiom

• Popular idiom:

• There's more than one way to skin a cat

• This is the functionality:

I.e.: what you want to do

• There are different implementations possible:

I.e.: how you gonna do it....

• Presentation

• The next few webpages:

 We will first study the mechanism that Java provide to implement Abstract Data Types (i.e.,: interface) Then we will see an example how a stack ADT is implemented using an array I then show you a different implementation using a linked list - with without teaching you the details of the linked list. We will study the linked list in detail later in this course.