Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it?...

12
Abstract Data Types

Transcript of Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it?...

Page 1: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

Page 2: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

What’s on the menu?

What’s an abstract data type?

How do you implement it?

ADT List

Page 3: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

Being abstractSometimes, you just want the job to be done: you don’t care how.

You want a car?

Car is Abstract.

It specifies a set of methods and what is the expected result.

It does not care about internal details (engine, shape, color…).

public interface Car{

public void setDriver(People p);

public void drive(Destination d, Roads r);

public boolean addPassenger(People p);

public boolean isFull();

public People getPassenger(int seat);

}

Page 4: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

Being abstractTo use it, you know the methods provided: assume they work.

Let say we have a set of people who need to get into a cab.

We have different cabs.

That’s fine: they all are cars.

Page 5: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

public int storePeople(List<People> p, List<Car> c){

while(!p.isEmpty()){ // as long as there are people to handle

if(c.isEmpty()) return p.size(); // not enough cars

if(c.getLast().isFull()){ // is the current car full?

c.removeLast(); // then try the next one

continue;

} // store the current person into the current car

c.get(currentCar).addPassenger(p.getLast());

p.removeLast(); // this person has been taken care of

}

return 0; // we didn’t run out of cars so nobody’s still waiting

}

Page 6: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

Abstract Data TypeAn Abstract Data Type (ADT) is a collection of data and a set of operations on that data.

Think of it as a contract: here are the things I guarantee you will work, but you don’t know all the details of how I’m going to make them work.

Page 7: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

What’s a List?A List is an Abstract Data Type.

You’ve seen one implementation: ArrayList.

ArrayList al = new ArrayList();

empty

al.add(new Wine(« monopole »));

0

Wine w = new Wine(« bordeau »);

al.add(w);

1

al.add(Cheese.randomSample());

2

al.add(new Bread(10,3,26));

3

al.add(new Clown(« Sarkozy »));

4

al.get(2); // returns an Object (un-typed List)

( (Cheese) al.get(2) ); // forces the typeal.remove(2);

2 3

Page 8: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

The ADT ListWhat are the things you want on a List of items?

• Determine the number of items in the list

• Add an item at a given position in the list

• Remove the item at a given position in the list

• Get the item at a given position in the list

public interface List{

public int size();

public void add(Object o, int i);

public void remove(int i);

public Object get(int i);

public boolean contains(Object o);

}

• Determine whether an item is in the list

Other things can be expressed from these:

isEmpty() == size() > 1removeAll ==while(!(isEmpty))

remove(0);

Page 9: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

Different implementations

ArrayList LinkedList

Based on an Array. Based on pointers.

Page 10: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

ArrayList

At the beginning, we have:• An Array of some fixed size (such as 10).

• An Index to know where we are currently.

• A Total to keep track of the number of elements.

Index: 0

Total: 0

Page 11: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

ArrayList

To add an element:• Put it where you are currently.

• Go to the next cell.

We may not have enough space: you take care of it in Assignment1!

Index: 0

Total: 0

Index: 1

• Increase the total.

Total: 1

Index: 2

Total: 2

Index: 6

Total: 6

Page 12: Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.

Abstract Data Types

ArrayList

To delete an element:• Shift everything up to where you want to delete.

Index: 6

Total: 6

• Decrease the total.

Index: 5

Total: 5