OOP - Lecture 7
OOP - Lecture 7
H D ... Q
new ListNode
12
(b ) firstNode
7 11
new ListNode
12
12 7 11 5
12 7 11 5
12 7 11 5
(b ) firstNode lastNode
12 7 11 5
removeItem
12 7 11 5
12 7 11 5
removeItem
iterator.add("Juliet"); // DHJ|RT
iterator.add("Nina"); // DHJN|RT
iterator.next(); // DHJNR|T
iterator.remove(); // DHJN|T
System.out.println(staff);
System.out.println("Expected: [Diana, Harry, Juliet, Nina, Tom]");
}
}
ArrayList vs LinkedList
ArrayList LinkedList
ArrayList uses the dynamic array structure LinkedList uses the linkedList (doubly) structure
Data manipulation is slower Data manipulation is faster
Data access is faster Data access is slower
Classic Data Structures
• Now we'll examine some common data structures that
are helpful in many situations
• Classic linear data structures include queues and stacks
• Classic nonlinear data structures include trees and
graphs
Queues
• A queue is a list that adds items only to the rear of the
list and removes them only from the front
• It is a FIFO data structure: First-In, First-Out
• Analogy: a line of people at a bank teller’s window
Queues
• Classic operations for a queue
• enqueue - add an item to the rear of the queue
• dequeue (or serve) - remove an item from the front of the
queue
• empty - returns true if the queue is empty