20
OrderedListADT

OrderedListADT. Overview OrderedListADT: not the same as linked lists The OrderedListADT: operations and sample applications An ArrayList implementation

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

OrderedListADT

OverviewOrderedListADT: not the same as linked listsThe OrderedListADT: operations and sample

applicationsAn ArrayList implementation and its time complexityA LinkedList implementation and its time complexity Java’s built-in implementations of OrderedListADT

OrderedListADT p. 2/21

Our third ADT: OrderedListADTConsider the following operations:add(T element) // in a way that keeps the list

orderedremove (T element) // wherever it iscontains (T element) isEmpty() size()toString()How are these different from stacks and queues?

OrderedListADT p. 3/21

What is the OrderedListADT useful for?An example of something that could be built

better using an OrderedListADT than with a stack or a queue is a structure that models:

A. The undo operation in Word

B. The Back button in a web browser

C. the customers waiting to pay at Donovan

D. a to-do list  

E. the computers at RIC and the network that connects them

OrderedListADT p. 4/21

More applications of OrderedListADTName five additional everyday examples of an

ordered list other than those discussed in the text.

OrderedListADT p. 5/21

The OrderedListADT operationsThe add operation is best described as which of the

following:A. adds an item to the OrderedList at the frontB. adds an item to the OrderedList in its placeC. adds an item to the OrderedList at a random

locationD. adds an item to the OrderedList at the endE. none of the above

OrderedListADT p. 6/21

The OrderedListADT operations (2)The remove operation is best described as

which of the following:A. removes the item at the front of the ListB. removes a specified itemC. removes a randomly chosen itemD. removes the item at the end of the listE. none of the above

OrderedListADT p. 7/21

OrderedListADT vs. StackADT and QueueADTCompare and contrast the operations provided

by an OrderedListADT with those provided by the StackADT and the QueueADT. What’s basically the same? What’s different?

OrderedListADT p. 8/21

The OrderedListADT interfaceConsider the code for the ListADT interface on

page 141 of your text.Why does the interface extend Iterable?

What is the effect of putting extends Iterable in the header of the interface?

Rewrite the interface to create our OrderedListADT.

OrderedListADT p. 9/21

Implementing OrderedListADT with arrays

If we use an array to implement, the following, when (if ever) do we need to copy data?

StackADTQueueADTOrderedListADTExplain with examples and diagrams.

OrderedListADT p. 10/21

Understanding the array-based implementation: remove

Consider the code for the remove operation for an array-based implementation (p. 155).

 Suppose we have a list with four elements with ID numbers 1, 3, 5, and 7, and remove is called to delete element number 5. Explain in complete sentences what happens when the code is executed, with diagrams of the array.

What is the time complexity of the remove operation and why?

OrderedListADT p. 11/21

Understanding the array-based implementation: contains

Consider the code for the contains operation for an array-based implementation (p. 157).

 Suppose we have a list with four elements with ID numbers 1, 3, 5, and 7, and contains is called to determine if the list contains an element with ID number 10. Explain in complete sentences what happens when the code is executed, with diagrams of the array.

 What is the time complexity of the contains operation and why?

OrderedListADT p. 12/21

Understanding the array-based implementation: addConsider the code for the add operation for an

array-based implementation (p. 161). Suppose we have a sorted list with four

elements with ID numbers 1, 3, 5, and 7, and add is called to add a new element with ID number 2. Explain in complete sentences what happens when the code is executed, with diagrams of the array. 

What is the time complexity of the add operation and why?

OrderedListADT p. 13/21

IteratorsWhat is an iterator?Consider the ArrayIterator code (p. 159).Suppose you used it in writing a toString

method. Line by line, how would it work:A. for a queue B. for a stack C. for a listWhat do you think of this class?

OrderedListADTs p. 14/21

Implementing OrderedListADT with linked listsIf we use a linked list to implement

OrderedListADT, implementing add and remove is a little more complicated than it is for a stack or a queue. Explain why, in complete English sentences (add diagrams if you like).

OrderedListADT p. 15/21

Understanding the link-based implementation: removeConsider the code for the remove operation for a

link-based implementation (p. 164). Suppose we have a list with four elements

with ID numbers 1, 3, 5, and 7, and remove is called to delete element number 5. Explain in complete sentences what happens when the code is executed, with box and arrow diagrams of the linked list.

 What is the time complexity of this remove operation and why?

OrderedListADTs p. 16/21

Understanding the link-based implementation: contains and add

Based on the code for remove, write a link-based version of contains.

Based on the code for remove, write a link-based version of add.

What is the time complexity of each of your methods? Explain.

OrderedListADTs p. 17/21

Understanding the link-based implementation: removeCompare the two link-based versions of remove in

the text (pp. 164 and 168). What is the difference in how they handle the example discussed earlier:

Suppose we have a list with four elements with ID numbers 1, 3, 5, and 7, and contains is called to determine if the list contains an element with ID number 10. Explain in complete sentences what happens when the code is executed, with diagrams of the array.

What is the time complexity of the two link-based versions of remove and why?

OrderedListADTs p. 18/21

Java’s Built-In ListsConsider Java’s three implementations of lists: Vector, ArrayList, and LinkedList.

 Do we need two add and two addAll methods? If not, which is (are) the most basic, and how could you use those to write the others?

Suppose you want to implement our QueueADT. What are the disadvantages of defining a class that inherits from Vector or ArrayList?

Explain how you could use containment instead of inheritance to write an implementation of QueueADT using a Java ArrayList.

OrderedListADTs p. 19/21

Coming attractionsNext time we’ll review recursion, in

preparation for looking at a new Abstract Datatype that uses it extensively.

Homework: read Chapter 7 (or the equivalent in the earlier edition).

OrderedListADTs p. 20/21