Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List."— Presentation transcript:

1 Abstract Data Types

2 What’s on the menu? What’s an abstract data type? How do you implement it? ADT List

3 Abstract Data Types Being abstract Sometimes, 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); }

4 Abstract Data Types Being abstract To 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.

5 Abstract Data Types public int storePeople(List p, List 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 }

6 Abstract Data Types Abstract Data Type An 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.

7 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 type al.remove(2); 2 3

8 Abstract Data Types The ADT List What 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() > 1 removeAll == while(!(isEmpty)) remove(0);

9 Abstract Data Types Different implementations ArrayList LinkedList Based on an Array.Based on pointers.

10 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

11 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

12 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


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

Similar presentations


Ads by Google