Abstract Data Types Whats on the menu Whats
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 abstract Sometimes, you just want. Car{ the job to be done: you don’t care how. public interface public void Youset. Driver(People want a car? p); public void drive(Destination d, Roads r); public boolean add. Passenger(People p); public boolean is. Full(); public People get. Passenger(int seat); } 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…). 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. Abstract Data Types
public int store. People(List<People> p, List<Car> c){ while(!p. is. Empty()){ // as long as there are people to handle if(c. is. Empty()) return p. size(); if(c. get. Last(). is. Full()){ c. remove. Last(); // not enough cars // is the current car full? // then try the next one continue; } // store the current person into the current car c. get(current. Car). add. Passenger(p. get. Last()); p. remove. Last(); // this person has been taken care of } return 0; // we didn’t run out of cars so nobody’s still waiting } 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 things I guarantee you will work, but you don’t know all the details of how I’m going to make them work. Abstract Data Types
What’s a List? A List is an Abstract Data Type. You’ve seen one implementation: Array. List. empty 0 1 2 3 3 al. get(2); 4 // returns an Object (un-typed List) Wine al. add(Cheese. random. Sample()); al. add(new Array. List w= new al Wine( « = Wine( « new monopole Array. List(); bordeau » )); » ); al. add(new Bread(10, 3, 26)); al. remove(2); al. add(new Sarkozy ( (Cheese) al. get(2) ); Clown( « // forces the type » )); al. add(w); Abstract Data Types
The ADT List What are 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 • Determine whether an item is in the list public interface List{ public int size(); public void add(Object o, int i); Other things can be expressed from these: public void remove(int i); while(!(is. Empty)) is. Empty() remove. All == == size() > 1 remove(0); public Object get(int i); public boolean contains(Object o); } Abstract Data Types
Different implementations Array. List Linked. List Based on an Array. Based on pointers. Abstract Data Types
Array. List Index: 0 Total: 0 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. Abstract Data Types
Array. List Index: 0 Index: 1 2 Index: 6 Total: 1620 To add an element: • Put it where you are currently. • Go to the next cell. • Increase the total. We may not have enough space: you take care of it in Assignment 1! Abstract Data Types
Array. List 6 Index: 5 Total: 56 To delete an element: • Shift everything up to where you want to delete. • Decrease the total. Abstract Data Types
- Slides: 12