Download presentation
Presentation is loading. Please wait.
1
OrderedListADT
2
Overview OrderedListADT: not the same as linked lists The OrderedListADT: operations and sample applications An ArrayList implementation and its time complexity A LinkedList implementation and its time complexity Java’s built-in implementations of OrderedListADT OrderedListADT p. 2/21
3
Our third ADT: OrderedListADT Consider the following operations: add(T element) // in a way that keeps the list ordered remove (T element) // wherever it is contains (T element) isEmpty() size() toString() How are these different from stacks and queues? OrderedListADT p. 3/21
4
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
5
More applications of OrderedListADT Name five additional everyday examples of an ordered list other than those discussed in the text. OrderedListADT p. 5/21
6
The OrderedListADT operations The add operation is best described as which of the following: A. adds an item to the OrderedListat the front B. adds an item to the OrderedList in its place C. adds an item to the OrderedList at a random location D. adds an item to the OrderedList at the end E. none of the above OrderedListADT p. 6/21
7
The OrderedListADT operations (2) The remove operation is best described as which of the following: A. removes the item at the front of the List B. removes a specified item C. removes a randomly chosen item D. removes the item at the end of the list E. none of the above OrderedListADT p. 7/21
8
OrderedListADT vs. StackADT and QueueADT Compare 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
9
The OrderedListADT interface Consider 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
10
Implementing OrderedListADT with arrays If we use an array to implement, the following, when (if ever) do we need to copy data? StackADT QueueADT OrderedListADT Explain with examples and diagrams. OrderedListADT p. 10/21
11
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
12
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
13
Understanding the array- based implementation: add Consider 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
14
Iterators What 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 list What do you think of this class? OrderedListADTs p. 14/21
15
Implementing OrderedListADT with linked lists If 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
16
Understanding the link-based implementation: remove Consider 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
17
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
18
Understanding the link-based implementation: remove Compare 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
19
Java’s Built-In Lists Consider 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
20
Coming attractions Next 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.