Presentation is loading. Please wait.

Presentation is loading. Please wait.

The List Interface Cmput 115 - Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.

Similar presentations


Presentation on theme: "The List Interface Cmput 115 - Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based."— Presentation transcript:

1 The List Interface Cmput 115 - Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/1/00

2 ©Duane Szafron 20002 About This Lecture  In this lecture we will learn about an Interface called List and write an example program that uses one of its implementations.

3 ©Duane Szafron 20003 Outline  Lists  Structure Interface Hierarchy for List  Util Interface Hierarchy for List  The Store, Collection and List Interfaces  Example of Using Lists - The Free List

4 ©Duane Szafron 20004 A List  A List is a non-indexed collection object that can grow and shrink at either end. "Fred""Barney""Wilma""Fred""Barney""Wilma"”Pebbles"”Betty""Fred""Barney""Wilma"”Betty"”Pebbles"”Fred"”Barney"”Wilma"

5 ©Duane Szafron 20005 Structure Collection Hierarchy  In the structure package and in java.util, Vectors and Arrays are containers, but they are not called collections.  The structure package defines the following interface hierarchy. Store Collection List

6 ©Duane Szafron 20006 Util Collection Hierarchy  In the java.util package, there is no Store interface.  In addition, the interfaces Collection and List have more messages defined than in the structure package.  The java.util package defines the following interface hierarchy. Collection List

7 ©Duane Szafron 20007 Structure Interface - Store public interface Store { public int size(); //post: returns the number of elements contained in // the store. public boolean isEmpty(); // post: returns the true iff store is empty. public void clear(); // post: clears the store so that it contains no // elements. } code based on Bailey pg. 18

8 ©Duane Szafron 20008 Structure Interface - Collection public interface Collection extends Store { public boolean contains(Object anObject); // pre: anObject is non-null // post: returns true iff the collection contains the object public void add(Object anObject); // pre: anObject is non-null // post: the object is added to the collection. The // replacement policy is not specified public void remove(Object anObject); // pre: anObject is non-null // post: removes an object “equal” to anObject public iterator elements(); // post: return an iterator for traversing the collection } code based on Bailey pg. 19

9 ©Duane Szafron 20009 Structure Interface - List 1 public interface List extends Collection { public void addToHead(Object anObject); // pre: anObject is non-null // post: the object is added to the beginning of the list public void addToTail(Object anObject); // pre: anObject is non-null // post: the object is added to the end of the list public Object peek(); // pre: list is not empty // post: returns the first object in the list without // modifying the list code based on Bailey pg. 99

10 ©Duane Szafron 200010 Structure Interface - List 2 public Object tailPeek(); // pre: list is not empty // post: returns the last object in the list without // modifying the list public Object removeFromHead(); // pre: list is not empty // post: removes and returns first object from the list public Object removeFromTail(); // pre: list is not empty // post: removes and returns last object from the list code based on Bailey pg. 99

11 ©Duane Szafron 200011 List Example - Free-Lists  One common application of Lists is as a container to hold objects that can be used for a while in a program and then later returned to the container for re-use.  Such a container of re-usable objects is called a free-list.

12 ©Duane Szafron 200012 Free-Lists of Arrays  For example, consider a free-list that contains various sizes of Arrays of ints.  When we need an array of a particular size, we remove it from the free-list, use it until we don’t need it anymore and add it back to the free-list.  We can then re-use this same array later when we need another array of the same size.

13 ©Duane Szafron 200013 Free Lists of Arrays- Problem  There is a problem if we want to use a free-list of arrays.  To remove an element from the list that is an array of a particular size, we need to call the remove(Object) method and pass in an Object that is equal to the one we want to remove.  If the free-list contained arrays, this would mean that we would have to create another array of the same size and then fill in the correct values in order to remove an array from the free-list.  If you must create another array anyway, there is no need to use a free-list.

14 ©Duane Szafron 200014 Free Lists of Arrays- ArrayNode  To solve this problem, we create a new class called ArrayNode where each instance holds an Array and an int size.  We define equality on this class by just comparing the size values of two ArrayNodes.  To remove an ArrayNode from a free-list, we create an ArrayNode that has the particular size value we are looking for, but no actual array.  When the ArrayNode is removed from the free-list we can either discard the ArrayNode and keep the array or use the value field in the ArrayNode for other purposes.

15 ©Duane Szafron 200015 Class ArrayNode 1 public class ArrayNode { // An instance of this class contains an int and an array. /* Instance Variables */ protected int value; protected int data[ ]; /* Constuctors */ public ArrayNode(int value, int data[ ]) { // post: The receiver has the given value and array data. this.value = value; this.data = data; } code based on Bailey pg. 102

16 ©Duane Szafron 200016 Class ArrayNode 2 /* Methods */ public int getValue() { // post: return the value in the ArrayNode return this.value; } public void setValue(int newValue) { // post: the value in the ArrayNode is the new value this.value = newValue; } public int[ ] getData() { // post: return the array in the ArrayNode return this.data; } code based on Bailey pg. 102

17 ©Duane Szafron 200017 Class ArrayNode 3 public boolean equals (Object node) { // pre: node is non-null // post: return true if the receiver is equal to the // argument. In this case if they have the same value ArrayNode other; other = (ArrayNode) node; return (this.value = other.value); } code based on Bailey pg. 102

18 ©Duane Szafron 200018 Class ArrayNode 4 public String toString () { // post: return my display string String result; int index; result = "[value: " + this.value + " data:"; if (data != null) for (index = 0; index < data.length; index++) for (index = 0; index < data.length; index++) result = result + " " + data[index]; result = result + "]"; return result; }} code based on Bailey pg. 102

19 ©Duane Szafron 200019 List Example - Free Lists 1 public class ExampleFreeList { /*This class contains the main program for an example program that uses free lists of ArrayNodes of various sizes. program that uses free lists of ArrayNodes of various sizes. It reads lines from input. Each line contains a sequence of ints. There are three kinds of input lines. One kind is called an output line and it contains a single negative int. The second kind is called an input line and it starts with two positive ints and contains multiple other ints. The third kind of line is a stop line and it contains a zero. It reads lines from input. Each line contains a sequence of ints. There are three kinds of input lines. One kind is called an output line and it contains a single negative int. The second kind is called an input line and it starts with two positive ints and contains multiple other ints. The third kind of line is a stop line and it contains a zero. The absolute value of the first int on a line is called an id. On an input line, the second int says how many more ints are on that line. These other ints are called data ints. When an input line is found, an ArrayNode of the proper size is removed from the free list and the id int and data ints are stored in it and this ArrayNode is put into a data list. The absolute value of the first int on a line is called an id. On an input line, the second int says how many more ints are on that line. These other ints are called data ints. When an input line is found, an ArrayNode of the proper size is removed from the free list and the id int and data ints are stored in it and this ArrayNode is put into a data list. code based on Bailey pg. 103

20 ©Duane Szafron 200020 List Example - Free Lists 2 When an output line is found, the id int and all of the data ints from the input line with the same id are displayed on the screen. The ArrayNode that contains these ints is removed from the data list and the array is returned to the free list. When a stop line is found, the program stops. When an output line appears whose id has no corresponding id in an ArrayNode in the data list, or when an input line contains a number of data ints for which there is no correct sized ArrayNode in the free list, the program stops. code based on Bailey pg. 103

21 ©Duane Szafron 200021 List Example - Free Lists 3 public static void main (String args[]) { List freeList; List data; ReadStream stream; boolean success; freeList = makeFreeList(); data = new SinglyLinkedList(); stream = new ReadStream(); success = true; while (success) // success fails when stop occurs success = nextLine(stream, data, freeList); success = nextLine(stream, data, freeList);} code based on Bailey pg. 103

22 ©Duane Szafron 200022 Example Input 1 2 68 37 4 5 2 4 6 8 10 -4 -5 3 2 36 85 9 2 14 15 68 37 1 2 4 4 6 8 10 Display id=4 data=(2,4,6,8,10) 36 85 3STOP STOP

23 ©Duane Szafron 200023 List Example - Free Lists 4 public static List makeFreeList () { // post: return a free list containing 2 ArrayNodes of // each size:2 to 10 inclusive. List freeList; List freeList; int index; int index; freeList = new SinglyLinkedList(); freeList = new SinglyLinkedList(); for (index = 1; index < 11; index++) { for (index = 1; index < 11; index++) { freeList.add(new ArrayNode(index, new int[index])); freeList.add(new ArrayNode(index, new int[index])); } return freeList; return freeList;} code based on Bailey pg. 103

24 ©Duane Szafron 200024 Illustrating the List Structure 1 10 2 3 freeList - a list of ArrayNode 1 2 3 10 1 dataList

25 ©Duane Szafron 200025 List Example - Free Lists 5 public static boolean nextLine (Stream stream, List data, List freeList) { List freeList) { // pre: data, stream and freeList are non-null /* post: read an int from the stream. If the int is positive, process an input line and return true if it was successful and process an input line and return true if it was successful and false otherwise. If the int is negative, process an output line false otherwise. If the int is negative, process an output line and return true if it was successful and false otherwise. and return true if it was successful and false otherwise. If the int was zero, return false. */ If the int was zero, return false. */ int id; boolean success; int id; boolean success; stream.skipWhite(); id = stream.readInt(); stream.skipWhite(); id = stream.readInt(); if (id > 0) success = input (id, stream, data, freeList); if (id > 0) success = input (id, stream, data, freeList); else if (id < 0) success = output (-id, data, freeList); else if (id < 0) success = output (-id, data, freeList); else success = false; //stop condition reached else success = false; //stop condition reached return success; return success;} code based on Bailey pg. 103

26 ©Duane Szafron 200026 List Example - Free Lists 6 public static boolean input (int id, ReadStream stream, List data, List freeList) { List data, List freeList) { // pre: id > 0, stream and freeList are non-null /* post: read an int from the stream. If the int is not positive, return false. Otherwise, find an ArrayNode in the free list that is the size of this int. If none exists return false. If one exists, read that many ints into the array, move the ArrayNode to the data list, using the value field for the id and return true. */ int count; ArrayNode node; stream.skipWhite(); count = stream.readInt(); if (count <= 0) return false; code based on Bailey pg. 104

27 ©Duane Szafron 200027 List Example - Free Lists 7 node = new ArrayNode(count, null); // Now remove node from freeList if one exists // Now remove node from freeList if one exists node = (ArrayNode) freeList.remove(node); if (node == null) return false; return false;node.setValue(id); readData(count, stream, node.getData()); data.add(node); return true; } code based on Bailey pg. 103

28 ©Duane Szafron 200028 List Example - Free Lists 8 public static void readData (int size, ReadStream stream, int anArray[]) { stream, int anArray[]) { // pre: size > 0, stream is non-null, anArray.length >= size // post: read size number of ints from the stream to the array int index; Assert.pre((size > 0) && (anArray.length >= size) && (anArray.length >= size) && (stream != null), “Array too small”); (stream != null), “Array too small”); for (index = 0; index < size; index++) { stream.skipWhite(); stream.skipWhite(); anArray[index] = stream.readInt(); anArray[index] = stream.readInt();}}

29 ©Duane Szafron 200029 List Example - Free Lists 9 public static boolean output (int id, List data, List freeList) { // pre: data and freeList are non-null // post: Find the ArrayNode in the data list with the given id as // its value. If it is not found, return false. Otherwise, output // the ArrayNode, change the value of the ArrayNode to the length // of its array and transfer it from the data list to the free list. ArrayNode node; node = new ArrayNode(id, null); node = (ArrayNode) data.remove(node); if (node == null) return false; // no match with existing node System.out.println(node); node.setValue(node.getData().length); // find length of // existing node // existing nodefreeList.add(node); return true; } code based on Bailey pg. 104


Download ppt "The List Interface Cmput 115 - Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based."

Similar presentations


Ads by Google