Download presentation
Presentation is loading. Please wait.
1
CS2013 Lecture 4 John Hurley Cal State LA
2
2 The List Interface
3
Lower-Level Implementation of Lists
3 Lower-Level Implementation of Lists You will probably never need to code a List class yourself after you graduate from school. Java and most other programming languages have built in ones that are good enough for nearly all cases. However, the next lab will involve coding methods with a singly linked list, and later in the course we will code parts or all of many more-complex data structures. The course web page contains a link to a code package with a partial implementation of a linked list.
4
4 ArrayList Like an array, a list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index. Recall that an array has a fixed length. An array is suitable if your application does not require the data structure to change size.. A list can grow or shrink dynamically. ArrayList and LinkedList are the most common concrete implementations of the List interface. ArrayList is implemented using an underlying array that begins with a default size. If the list outgrows the array, a new, larger array must be created and the contents of the old one copied. For random access using indices, ArrayLists are very efficient, because these operations are very efficiently implemented for arrays. However, inserting or deleting (as distinct from initializing or changing) a value at an arbitrary spot in an array list involves moving all the subsequent elements in the array. If the list never reaches the original size of the array, some of the array is wasted. Therefore, ArrayList is not efficient in memory usage, which is often the bottleneck in present-day applications.
5
5 java.util.ArrayList
6
6 Linked Lists A linked list is a data structure consisting of a group of nodes which represent a sequence of values of the parameterizing type. In the simplest form of linked list, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence. This is called a singly linked list. We will learn more about how linked lists are implemented later; here we will use the Java Collections Framework LinkedList<E> class.
7
7 Linked Lists In a doubly linked list, each node contains a reference to the next node and a reference to the previous node. Either a singly or double linked list may be circular; that is, last node points to the first, and, in a circular doubly-linked list, the first node has a reference to the last Since the items may not be stored in contiguous memory, the JVM traverses the list by following the links. The additional links in these types of linked lists make this process more efficient. Diagrams from Wikipedia:
8
8 java.util.LinkedList Java’s built-in LinkedList class implements a doubly-linked list. It implements the List interface, but implements some of the methods differently than ArrayList does.
9
Node as an Inner Class Singly Linked Lists 9
10
Accessor Methods Singly Linked Lists 10
11
11 Linked Lists Adding and removing elements from any spot in a singly linked list involves changing, at most, one reference in each of two existing elements and setting, at most, two references in a new element. Compare this to the expense of moving all the elements in an array after the insertion point of the new value or of copying all the elements from an old array to a new one. Add a node to a singly-linked list: Inserting a node into a doubly linked list just involves also changing the reference from the subsequent element and adding one from the new element back to the previous one
12
Inserting at the Head Allocate new node Insert new element
Have new node point to old head Update head to point to new node Singly Linked Lists 12
13
Inserting at the Tail Allocate a new node Insert new element
Have new node point to null Have old last node point to new node Update tail to point to new node Singly Linked Lists 13
14
14 Linked Lists Delete a node from a singly-linked list:
15
Removing at the Head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node Singly Linked Lists 15
16
16 Linked Lists Data in a linked list is accessed sequentially; we start at the head of the list and follow the references. This is called traversing the list. node = list.firstNode while node not null (do something with node.data) node = node.next Source for pseudocode: Wikipedia This makes a linked list less efficient than an array list for operations that require a great deal of traversal, because accessing an array element by index is very efficient. Also, memory is allocated on the fly when a node is added. This makes it more likely that different nodes will be far apart in memory, which sometimes makes it more expensive to traverse the list.
17
Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Singly Linked Lists 17
18
18 Linked Lists Course page has link to linkedlist and listcompare packages
19
19 I/O Expense This is a good time to point out that I/O is very expensive. Rerun the List Comparer code above with System.out.println statements inside the loops in the add and get methods.
20
Doubly Linked List A doubly linked list can be traversed forward and backward Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next element node trailer header nodes/positions elements © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 20
21
Insertion p A B C p q A B C X p q A B X C
Insert a new node, q, between p and its successor. p A B C p q A B C X p q A B X C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 21
22
Deletion A B C D p A B C p D A B C
Remove a node, p, from a doubly linked list. A B C D p A B C p D A B C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 22
23
Doubly-Linked List in Java
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 23
24
Doubly-Linked List in Java, 2
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 24
25
Doubly-Linked List in Java, 3
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 25
26
Doubly-Linked List in Java, 4
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 26
27
27 Cycles If traversing a list by following the next references will result in returning to a node already visited, a cycle exists. This happens if one of the next references points back to a node that appeared earlier in the list. A cycle will make it impossible to traverse the list fully or to add a node at the end. The part of the list that constitutes the cycle is equivalent to a circular linked list, but this is much more likely to happen as a result of a programming error than of deliberate design.
28
Cycles and Floyd’s Algorithm
28 Cycles and Floyd’s Algorithm A cycle will result in traversal continuing infinitely. However, we can’t test for that, since there is no specific amount of traversal time after which we can show that there must be a cycle. We could test for cycles by traversing the list, keeping a new list of references to the nodes in the original list, and checking the new list each time we encounter a new node. This would not be very expensive in memory, since we are keeping references. However, the computation required to check the new list each time would increase as the size of the list increases. To test for cycles efficiently, create two references to nodes and set them both equal to head. Use a loop to traverse the list in parallel but at two different speeds (usually one moves one node at a time and the other moves two nodes at a time.) If one of the references reaches the last element (the one whose next is null) there is no cycle. If the two references ever refer to the same node, there is a cycle.
29
29 Iterators An iterator is an object that provides methods for traversing the elements of a collection. Even with Lists, which are not hard to manipulate using loops, iterators sometimes provide easier ways to traverse data structures In many cases, the enhanced for loop is just as convenient as an iterator.
30
30 The List Iterator
31
Iterator public static void main(String[] args) {
31 Iterator public static void main(String[] args) { List<String> names = new LinkedList<String>(); names.add("John"); names.add("Paul"); names.add("George"); names.add("Ringo"); String currName; ListIterator<String> namesIterator = names.listIterator(); while(namesIterator.hasNext()){ currName = namesIterator.next(); System.out.println(currName + " " + (currName.compareTo("Mick") < 0? "earlier than Mick": "not earlier than Mick")); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.