210 likes | 330 Views
CS203 Lecture 4 . John Hurley Cal State LA. More Data Structures. Memorize This! A data structure is a systematic way to organize information in order to improve the efficiency of algorithms that will use the data . More Data Structures.
E N D
CS203 Lecture 4 John Hurley Cal State LA
More Data Structures Memorize This! A data structure is a systematic way to organize information in order to improve the efficiency of algorithms that will use the data
More Data Structures So far we have studied arrays and lists, specifically ArrayLists, in detail, and also discussed stacks. As you know, this class is called "Programming With Data Structures," and obviously, data structures are the main topic of the course. Over the next few weeks, we will introduce a wide array (ha!) of new data structures. Most of these data structures can be used as collections of wide ranges of parameterized types We will first learn to use some of the data structure types included with the JDK, then implement one or two on our own, then return to using the built-in ones.
Java Collection Framework A collection is a container object that holds a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named sets, lists, and maps.
Java Collection Framework hierarchy, cont. Set and List are subinterfaces of Collection.
The Collection Interface The Collection interface is the root interface for manipulating a collection of objects.
ArrayList and LinkedList 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 is fixed once it is created. An array is suitable if your application does not require insertion or deletion of elements. A list can grow or shrink dynamically. ArrayList and LinkedList are the most common concrete implementations of the List interface. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. ArrayList is implemented using an underlying array that begins with a default size. If the list outgrows the array, a new array must be created and the contents of the old one copied. Inserting values at arbitrary spots in the list involves moving all the subsequent elements. If your application requires the insertion or deletion of elements from arbitrary locations in the list, use a LinkedList.
Linked Lists A linked list is a data structure consisting of a group of nodes which represent a sequence. 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 in a few weeks; this week we will use the Java Collections Framework LinkedList<E> class.
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 Diagrams from Wikipedia: http://en.wikipedia.org/wiki/Linked_list
Linked Lists Adding and removing elements from any spot in a 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 periodically creating a new array and copying all the elements from an old 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
Linked Lists Delete a node from a singly-linked list:
java.util.LinkedList LinkedList implements a doubly-linked list. It is a subclass of a hierarchy of List classes, but more importantly it implements the List interface.
package demos; import java.util.*; public class TestArrayAndLinkedList { public static void main(String[] args) { List<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(1); // 1 is autoboxed to new Integer(1) arrayList.add(2); arrayList.add(3); arrayList.add(1); arrayList.add(4); arrayList.add(0, 10); arrayList.add(3, 30); System.out.println("A list of integers in the array list:"); System.out.println(arrayList); LinkedList<Object> linkedList = new LinkedList<Object>(arrayList); linkedList.add(1, "red"); linkedList.removeLast(); linkedList.addFirst("green"); System.out.println("Display the linked list backward:"); for (inti = linkedList.size() - 1; i >= 0; i--) { System.out.print(linkedList.get(i) + " "); } } }
Linked Lists Based on the info above, you might expect linked lists to be much more efficient than array lists. However, 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 whilenode not null (do something with node.data) node = node.next Source for pseudocode: Wikipedia This may make 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 efficiently implemented. 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 makes it more expensive to traverse the list.
package listcompare; public class StopWatch { private long elapsedTime; private long startTime; private booleanisRunning; public StopWatch() { reset(); } public void start() { if (isRunning) { return; } isRunning = true; startTime = System.currentTimeMillis(); } public void stop() { if (!isRunning) { return; } isRunning = false; long endTime = System.currentTimeMillis(); elapsedTime = elapsedTime + endTime - startTime; }
public long getElapsedTime() { if (isRunning) { long endTime = System.currentTimeMillis(); return elapsedTime + endTime - startTime; } else { return elapsedTime; } } public void reset() { elapsedTime = 0; isRunning = false; } }
package listcompare; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; public class ListComparer { Random r = new Random(); public void arrayListSequentialAdds() { List<Integer> al = new ArrayList<Integer>(); for (int counter = 0; counter < 100000; counter++) { al.add(0, counter); //System.out.println(counter); } } public void linkedListSequentialAdds() { List<Integer> ll = new LinkedList<Integer>(); for (int counter = 0; counter < 100000; counter++) { ll.add(0, counter); //System.out.println(counter); } } public void arrayListRandomAdds() { List<Integer> al = new ArrayList<Integer>(); for (int counter = 0; counter < 100000; counter++) { al.add(r.nextInt(al.size() + 1), counter); //System.out.println(counter); } } public void linkedListRandomAdds() { List<Integer> ll = new LinkedList<Integer>(); for (int counter = 0; counter < 100000; counter++) { ll.add(r.nextInt(ll.size() + 1), counter); //System.out.println(counter); } } }
package listcompare; public class ListDriver { public static void main(String[] args) { StopWatch s = new StopWatch(); ListComparer l = new ListComparer(); s.reset(); s.start(); l.arrayListSequentialAdds(); s.stop(); System.out.println("Array List sequential exercise took " + s.getElapsedTime() + " ms"); s.reset(); s.start(); l.linkedListSequentialAdds(); s.stop(); System.out.println("Linked List sequential exercise took " + s.getElapsedTime() + " ms"); s.reset(); s.start(); l.arrayListRandomAdds(); s.stop(); System.out.println("Array List random exercise took " + s.getElapsedTime() + " ms"); s.reset(); s.start(); l.linkedListRandomAdds(); s.stop(); System.out.println("Linked List random exercise took " + s.getElapsedTime() + " ms"); } }
I/O Expense This is a good time to point out that I/O is very expensive. Rerun the code above with the System.out.println statements uncommented.