Download Data Structures and ADTs • Abstract Data Types (ADTs) are user

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures and ADTs
• Abstract Data Types (ADTs) are user defined data types. An
ADT has 2 parts:
1. A name or type, specifying a set of data (e.g. Dictionary).
2. Descriptions of all the operations (or methods) that do things
with that type (e.g. find, insert, remove)
The descriptions indicate what the operations do, not how
they do it.
• A data structure is a systematic way of organizing and accessing
data from a computer (e.g. array, linked list).
Data structures are used to implement ADTs.
1
ADT Dictionary
find (key):
returns a record with the given key, or
null if no record has the given key
insert(key,data): inserts a new record with given key and data
ERROR if the dictionary already contains a
record with the given key
remove(key):
removes the record with the given key
ERROR if there is no record with the given key
2
Re-usability
• Re-usability:
– If data structure is useful for one application, it is probably
useful for another.
– Therefore, we should design it to be as re-usable as possible.
Method-A
Method-B
Method-C
Data Structure
Application 1
Application 2
6
Implementation 2
Abstract Data Types
• Preferred way of designing and implementing data structures.
• Uses 2 general principles: information hiding and re-usability.
• Information hiding:
– User data structure should not need to know details of its
implementation.
– We should be able to change implementation without affecting applications that use it.
– Therefore, implementation information should be hidden.
Application
Method-A
Method-A
Method-B
Method-B
Method-C
Method-C
Data Structure
Data Structure
Implementation 1
Implementation 2
5
Java Interface for ADT Dictionary
public interface Dictionary {
public Object find(Object key);
public void insert(Object key, Object data)
throws DuplicatedKeyException;
public void remove(Object key)
throws NoKeyException;
}
3
A Java Implementation for ADT Dictionary
public class LinkedListDictionary implements Dictionary {
protected int size;
protected DNode head, tail;
public LinkedListDictionary() {
size = 0;
head = new DNode(null, null,null);
tail = new DNode(null,null,null);
head.setNext(tail);
}
public Object find(Object key) {
if (size == 0) return null;
else {
...
}
}
public void insert (Object key, Object data) throws
DuplicatedKeyException {
...
}
4
The Position ADT
Position is an ADT with just one operation:
element(): Returns the data item
stored at this position.
7