1-Java_CollectionFramework_ADT_10a
1-Java_CollectionFramework_ADT_10a
• Specification
A set of data
Specifications for a number of operations to be
performed on the data
• Design
A lay-out organization of the data
Algorithms for the operations
• Goals of Design: fast operations
Implementation of a Data Structure
• Representation of the data using built-in data types of
the programming language (such as int, double, char,
strings, arrays, structs, classes, pointers, etc.)
• Language implementation (code) of the algorithms
for the operations
Data3
Data2 Data2
Data1 Data1 Data1
Data2
Data1 Data1
7
Stack
Data3
Data2 Data2
Data1 Data1 Data1
Data3 Data3
Data2
9
Queues
12
Design of the Stack and Queue Classes
There are two ways to design the stack and queue classes:
Using inheritance: You can declare the stack class by extending
the array list class, and the queue class by extending the linked
list class.
13
Composition is Better
Both designs are fine, but using composition is better
because it enables you to declare a complete new stack
class and queue class without inheriting the unnecessary
and inappropriate methods from the array list and linked
list.
14
MyStack and MyQueue
MyStack MyStack
-list: MyArrayList
+isEmpty(): boolean Returns true if this stack is empty.
+getSize(): int Returns the number of elements in this stack.
+peek(): Object Returns the top element in this stack.
+pop(): Object Returns and removes the top element in this stack.
+push(o: Object): Object Adds a new element to the top of this stack.
+search(o: Object): int Returns the position of the specified element in this stack.
MyQueue MyQueue
-list: MyLinkedList
+enqueue(element: Object): void Adds an element to this queue.
+dequeue(): Object Removes an element from this queue.
+getSize(): int Returns the number of elements from this queue.
15
GENERAL LINEAR LISTS
• list manages its own size; user of the list does not
need to worry about overfilling it
18
Java's List interface
• Java also has an interface java.util.List
to represent a list of objects:
(a partial list)
public void add(int index, Object o)
Inserts the specified element at the specified position in this list.
23
The problem of position
• The code on the previous slide is wasteful
because it throws away the position each time
– every call to get has to re-traverse the list!
• it would be much better if we could somehow
keep the list in place at each index as we looped
through it
24
List Iterator
• ListIterator type
• Gives access to elements inside a linked list
• Encapsulates a position anywhere inside the linked
list
• Protects the linked list while giving access
A List Iterator
A Conceptual View of the List Iterator
List Iterator
• Think of an iterator as pointing between two
elements
▪ Analogy: Like the cursor in a word processor
points between two characters
• The listIterator method of the LinkedList class gets a
list iterator
LinkedList<String> employeeNames = ...;
ListIterator<String> iterator =
employeeNames.listIterator();
List Iterator
• Initially, the iterator points before the first element
• The next method moves the iterator:
iterator.next();
• next throws a NoSuchElementException if you are
already past the end of the list
• hasNext returns true if there is a next element:
if (iterator.hasNext())
iterator.next();
List Iterator
• The next method returns the element that the iterator
is passing:
while iterator.hasNext()
{
String name = iterator.next();
//Do something with name
}
List Iterator
• Shorthand for loop:
for (String name : employeeNames)
{
// Do something with name
}
Behind the scenes, the for loop uses an iterator to
visit all list elements
List Iterator
• LinkedList is a doubly linked list
▪ Class stores two links:
o One to the next element, and
o One to the previous element
• To move the list position backwards, use:
▪ hasPrevious
▪ previous
Adding and Removing from a
LinkedList
Continued
ListTester.java (cont.)
Program Run:
Diana Harry Juliet Nina Tom
Expected: Diana Harry Juliet Nina Tom
Self Check
Do linked lists take more storage space than arrays of the same size?
Answer: Yes, for two reasons. You need to store the node references,
and each node is a separate object. (There is a fixed overhead to store
each object in the virtual machine.)
Self Check
Why don’t we need iterators with arrays?
Answer: An integer index can be used to access any array location.