Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSci 161 Lists and “Linear Structures” CSci 161 1 Arrays all elements must have the same type fixed size size is declared when the array is created occupies a contiguous chunk of memory an element's address is calculated as a certain offset into that chunk address of a[i]=a+(i)*(element size) a random access structure CSci 161 2 The Arrays Class The Arrays class provides support for common array operations searching sorting copying comparing filling creating a printable representation of the array CSci 161 3 Homework Exercise Write a Java program to a) create an integer array of size 100, initializing each element to the value 1000 using Arrays.fill() b) add a random value between 0 and 1000 to each array element, using an instance of the Random class and its nextInt(int n) method c) sort the array, using Arrays.sort() d) display the array, using Arrays.toString() to create something that is displayable CSci 161 4 Java Collections Framework a unified architecture for representing and manipulating collections allows them to be manipulated independently of the details of their representation this means they are “abstract data types” (ADTs) provided as part of the Java API to reduce programming effort and increase performance based on fourteen collection interfaces Collections classes implementation various combinations of these interfaces CSci 161 5 Collections Interfaces Collection<E> Not extensions of Collection: extended by Map<K,V> Set<E> List<E> SortedSet<E> NavigableSet<E> Queue<E> Deque<E> BlockingQueue<E> BlockingDeque<E> SortedMap<K,V> NavigableMap<K,V> ConcurrentMap<K,V> ConcurrentNavigableMap<K,V> we'll worry about these later CSci 161 6 Other Important Interfaces Cloneable RandomAccess Iterable<E> Serializable CSci 161 7 Vectors imported from java.util supports generic types: Vector<E> implements a growable array of objects note: at each point in time, it really is an array retrofitted (in Java version 1.2) to be a part of the Collections Framework implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess CSci 161 8 ArrayLists imported from java.util supports generic types: ArrayList<E> implements a growable array of objects note: at each point in time, it really is an array provided as part of Java 1.2 implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess not synchronized CSci 161 9 Homework Exercise Repeat the earlier array exercise using a Vector instead of an array. Repeat the earlier array exercise using an ArrayList instead of an array. CSci 161 10 A Different Strategy Arrays, Vectors and ArrayLists allocate space before it is actually needed if a Vector or ArrayList fills up, it will grow this requires allocation of an entirely new contiguous chunk of memory Linked Lists allocate space as it is needed in a sense, they are always full in a sense, they never fill up links connect the non-contiguous memory chunks CSci 161 11 Linked Lists dynamic allocation additional overhead to store link field sequential access only header locates 1st element storage need not be contiguous CSci 161 12 Nodes these are the chunks we hook together to form a linked list 2 fields data field (primitive data value stored here, or a reference to an object) link field CSci 161 13 A Linked List is an Object fields must have a header field to locate 1st node constructors a zero-parameter constructor to create an empty linked list possibly others methods insert, delete, search are the fundamental ones update, sort, ... could be useful in some cases CSci 161 14 A Node is also an object fields data next (link field) constructors 0-parameter constructor ... (data=null, next=null) 1-parameter constructor ... (data!=null, next=null) 2-parameter constructor ... (data!=null, next!=null) methods none are really essential unless fields are private 161 accessor and mutatorCSci methods for private fields 15 Inserting into a linked list // head = pointer to first node (null for empty list) n = new Node(data) if (inserting n at front of list) n.next = head head = n else find predecessor node p n.next = p.next p.next = n CSci 161 16 Deleting from a linked list // head = pointer to first node (null for empty list) if (deleting first element) n = head.data head = head.next else find predecessor node, p, of node to delete n = p.next.data p.next = n.next return n; CSci 161 17 Searching a linked list // head = pointer to first node // data = value to search for Node p = head while (p != null && p.data != data) p=p.next if (p == null) return null else return p.data CSci 161 18 Doubly-linked lists header points at 1st node (the “entry point” into the list) every node has a pointer to its successor and a pointer to its predecessor null pointers mark both “endpoints” CSci 161 19 Inserting into a doubly-linked list CSci 161 20 Deleting from a doubly-linked list CSci 161 21 Searching a doubly-linked list CSci 161 22 Circular Linked Lists header points at 1st node (the entry point into the list) the “last” node points to the first node, rather than having a null value in its link field avoid infinite loops! CSci 161 23 Inserting into a circular list // head = pointer to first node (null for empty list) n = new Node(data) if (inserting n into an empty list) head = n.next = n else if (inserting n at front of non-empty list) n.next = head head = n Node p = head while (p.next != head) // while p is not the last element p = p.next p.next = n else find predecessor node p (a while loop?) n.next = p.next p.next = n CSci 161 24 Deleting from a circular list // head = pointer to first node (null for empty list) if (deleting first element) n = head.data head = head.next Node p = head while (p.next != head) // while p is not the last element p = p.next p.next = head else find predecessor node, p, of node to delete (while loop?) n = p.next.data p.next = n.next return n; CSci 161 25 Searching a circular linked list CSci 161 26 Homework Exercise CSci 161 27 Stacks LIFO data structure in a “pure” stack, at most one element (the “top” element) is accessible at any point in time there are many different ways to implement a stack CSci 161 28 Stack Interface public interface Stack { /* adds an object to the stack */ public void push(Object x); /* removes most recently added item */ public Object pop(); /* returns (a copy of?) most recently added item, but does not remove it from the stack */ public Object top(); } CSci 161 29 Stack Implementations Using an array Using a linked list store items in array prefix insert at front remove from front 1st element at index 0 2nd element at index 1 etc remove item with highest index a helper variable counts the number of items in the stack CSci 161 30 Queues FIFO data structure only the two ends (front and rear) are accessible at any point in time add to the rear, remove from the front there are many ways to implement a queue CSci 161 31 Queue Interface public interface Queue { /* adds an object to the queue */ public void enqueue(Object x); /* removes “oldest” item */ public Object dequeue(); /* returns (a copy of?) oldest item, but does not remove it from the queue */ public Object front(); } CSci 161 32 Queue Implementations Using arrays Using linked lists maintain two helper variables 2 node pointers first node last node index of “front” item, incremented (wrap) when removing index of “last” item, incremented (wrap) when inserting delete first node when removing insert after last node empty condition front=(last+1)%array.length CSci 161 33 Homework Exercise CSci 161 34 Self-Adjusting Lists Keep items in a list (array, linked list, or other) Move data as necessary, so that the next item to be requested will be easy and fast to retrieve traditional examples: most frequently requested item first most recently requested item first The list uses a “heuristic”, which is not guaranteed to work every time ... it makes an “intelligent guess” CSci 161 35 Homework Exercise CSci 161 36 Performance Characteristics True or False? Searching unordered lists of length N has time complexity O(N) Searching ordered lists of length N can have time complexity O(log(N)) ... Inserting/deleting from stack or queue has time complexity O(1) if your implementation is good Inserting in an ordered list has time complexity O(N) ... sequential access or items must move Deleting from a list has time complexity O(N) ... search for item and (possibly) move data CSci 161 37 Homework Exercise CSci 161 38