1 / 11

Implementing the List ADT

Implementing the List ADT. Computer Science 4 Mr. Gerb. Reference: Objective: Understand how to implement an ADT. Review. We created an ADT called a list We defined a number of operations on list We decided the inputs and outputs of those operations

meris
Télécharger la présentation

Implementing the List ADT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementing the List ADT Computer Science 4 Mr. Gerb Reference: Objective: Understand how to implement an ADT.

  2. Review • We created an ADT called a list • We defined a number of operations on list • We decided the inputs and outputs of those operations • At no time did we discuss how those operations would be implemented. • But we could use the operations perfectly well without knowing. • Encapsulation!!!

  3. How could we implement a list? // Stores a list as an array class MyList implements List { Object[] storage = new Object[10]; int listSize=0; ... //Methods } • Some notes • “implements List” tells Java that List has been defined as an interface, a set of operations • Array initially given a size of 10. We will deal later with the case when this is not enough.

  4. Implementing the clear method //Very simple, list size = 0 public void clear() { listSize=0; }

  5. Implement the size operator public int size() { return listSize; }

  6. Implementing the get operator // This won’t work: public Object get(int N) { return storage[N]; } • What happens if N >= listSize?

  7. A better implementation of get public Object get(int N) } if (N>=listSize || N<0) throw new ArrayIndexOutOfBoundsException(); else return storage[N]; } • Can implement set in a similar way

  8. Implementing remove, move all subsequent elements down public Object remove(int N) } if (N>=listSize || N<0) throw new ArrayIndexOutOfBoundsException(); else { for (int (m=N+1;m<listSize;++m) storage[m-1]=storage[m]; --listSize; } }

  9. Implementing add, array might be smaller than listSize //Double the size of the array if no space public void add(Object obj) { if (storage.length<=listSize) { Object[] s=new Object[storage.length*2]; for (m=0; m<listSize; ++m) s[m]=storage[m]; storage=s; } storage[listSize] = obj; ++listSize; }

  10. Two parts of the implementation • Algorithms • Process by which operations occur • Examples: Moving down items during a remove or doubling the array during add. • Data structures • How data in the ADT is stored • In this case, an array and an integer

  11. Summary • Implement an ADT with a class • In Java, define the ADT as an interface • Use the implements keyword in the class definition. • Two parts of an implementation • Algorithms • Data structures • Implementation deals with many details that the ADT doesn’t need to deal with.

More Related