Download presentation
Presentation is loading. Please wait.
1
List Representation - Array
황승원 Fall 2010 CSE, POSTECH
2
Introduction Data Object a set of instances or values Examples:
Boolean = {false, true} Digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Letter = {A, B, C, …, Z, a, b, c, …, z} String = {a, b, …, aa, ab, ac,…} DS class? An individual instance is either primitive or composite. An element is the individual component of a composite instance.
3
Introduction Data Structure
data object together with the relationships among instances and elements that comprise an instance Among instances of integer 369 < 370 280+4 = 284 Among elements that comprise an instance 369 3 is more significant than 6 3 is immediately to the left of 6 9 is immediately to the right of 6
4
Introduction Abstract Data Type (ADT)
ADT is a collection of data and a set of operations that can be performed on the data. It enables us to think abstractly about the data … We can separate concepts from implementation. Typically, we choose a data structure and algorithms that provide an implementation of an ADT.
5
Linear List Definitions Examples
Linear list is a data object whose instances are of the form (e1,e2,…,en) ei is an element of the list. e1 is the first element, and en is the last element. n is the length of the list. When n = 0, it is called an empty list. e1 comes before e2, e2 comes before e3, and so on. Examples student names order by their alphabets a list of exam scores sorted by descending order
6
Linear List Operations
size() - determine list size get(theIndex) - get element with given index indexOf(theElement) - determine the index of an element remove(theIndex) - remove and return element with given index add(theIndex, theElement) - add an element so that the new element has a specified index
7
Data Structure Specification
Language independent Abstract Data Type Java Interface Abstract Class
8
ADT for Linear List AbstractDataType LinearList { instances
ordered finite collections of zero or more elements operations isEmpty(): return true iff the list is empty, false otherwise size(): return the list size (i.e., number of elements in the list) get(index): return the indexth element of the list indexO f(x): return the index of the first occurrence of x in the list, return -1 if x is not in the list remove(index): remove and return the indexth element, elements with higher index have their index reduced by 1 add(theIndex, x): insert x as the indexth element, elements with theIndex >= index have their index increased by 1 output(): output the list elements from left to right }
9
Linear List as Java Interface
public interface LinearList { public boolean isEmpty(); public int size(); public Object get(int index); public int indexOf(Object elem); public Object remove(int index); public void add(int index, Object obj); public String toString(); } Should add comments that describe the parameters, return value, and functionality of each method. Looks a lot like the abstract data type specification but the data types, return type specification, name of output method (toString) are all peculiar to Java.
10
Implementing An Interface
public class ArrayLinearList implements LinearList { // code for all LinearList methods must be provided here }
11
Formula-based Representation of Linear List
It uses an array to store the elements of linear list. Individual element is located in the array using a mathematical formula. typical formula location(i) = i – 1 ith element of the list is in position i-1 of the array element [0] [1] [2] [3] [4] MaxSize-1 length=5
12
Is array the best way to implement?
add(0) remove(0) add(0) remove(0) add(0) remove(0)
13
Create An Empty List ArrayLinearList a = new ArrayLinearList(100),
b = new ArrayLinearList(), For the second example, the class ArrayLinearList must implement the interface LinearList. c and f are actually set to null. So, they really are not referencing an empty linear list but the null list. If ArrayLinearList has methods that are not listed in LinearList then we’ll need to typecast d,e,f to ALL before performing any of these additional methods ((ArrayLinearList) d).newMethod().
14
Using A Linear List System.out.println(a.size()); a.add(0, 2);
b.add(0, 2); b.remove(0); if (a.isEmpty()) a.add(0, 5);
15
The Class ArrayLinearList
/** array implementation of LinearList */ package dataStructures; import java.util.*; // has Iterator interface import utilities.*; // has array resizing class public class ArrayLinearList implements LinearList { // data members protected Object [] element; // array of elements protected int size; // number of elements in array // constructors and other methods come here } /** => documentation comment. Used by javadoc to create html documentation files. The documentation files index.html, packages.html, etc. that are part of the codes that you have downloaded were produced by running javadoc. However, the above comment doesn’t appear in the produced documentation, because the doc comment must immediately precede the class, method, or field it applies to. Package statement must come first; import statements must come next. Packages are like folders, used to organize/store your programs in folders for easy access and permit name reuse across folders/packages. Import java.lang.* is implicit. Import statements specify a path relative to (or one that extends) a path contained in classpath. Default: visible in package only. Protected: in package and subclasses (I.e., classes that extend this one). Private: only in class. Public: visible to all classes in all packages.
16
A Constructor /** create a list with initial capacity initialCapacity
IllegalArgumentException when * initialCapacity < 1 */ public ArrayLinearList(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); // size has the default initial value of 0 element = new Object [initialCapacity]; } Note that code should verify input data and throw exceptions when this data is incorrect. Need have a throws statement in method header only if method throws an uncaught exception of type other than RuntimeException and Error.
17
The Method isEmpty /** @return true iff list is empty */
public boolean isEmpty() {return size == 0;}
18
The Method size() public int size() {return size;}
current number of elements in list */ public int size() {return size;}
19
The Method get /** @return element with specified index
IndexOutOfBoundsException when * index is not between 0 and size - 1 */ public Object get(int index) { checkIndex(index); return element[index]; }
20
The Method checkIndex /** @throws IndexOutOfBoundsException when
* index is not between 0 and size - 1 */ void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); } Note that checkIndex is not a public method. Visible only within the package. Could have made it protected instead. Then it would be visible only within the package dataStructures and classes that extend ALL.
21
The Method indexOf index of first occurrence of theElement, * return -1 if theElement not in list */ public int indexOf(Object theElement) { // search element[] for theElement for (int i = 0; i < size; i++) if (element[i].equals(theElement)) return i; // theElement not found return -1; } Note the use of the equals method. Default Object.equals method compares only references. This method must be overridden by user defined data types so that indexOf works correctly.
22
The Method remove { checkIndex(index);
public Object remove(int index) { checkIndex(index); // valid index, shift elements with higher index Object removedElement = element[index]; for (int i = index + 1; i < size; i++) element[i-1] = element[i]; element[--size] = null; // enable garbage collection return removedElement; }
23
The Method add public void add(int index, Object theElement) {
if (index < 0 || index > size) // invalid list position throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); // valid index, make sure we have space if (size == element.length) // no space, double capacity element = ChangeArrayLength.changeLength1D(element, 2 * size);
24
The Method add for (int i = size - 1; i >= index; i--)
// shift elements right one position for (int i = size - 1; i >= index; i--) element[i + 1] = element[i]; element[index] = theElement; size++; }
25
changeLength1D public static Object [] changeLength1D (Object [] a, int n, int newLength) { if (n> newLength) throw new IllegalArgumentException (“new length is too small”); Object [] new Array = (Object []) Array.newInstance (a.getClass().getComponentType(), newLength); System.arraycopy(a,0,newArray,0,n); return newArray; } Empty list with n inserts = O(n*n) // when adding by 1 = ? // when multiplying by 2 (See p165)
26
Coming Up Next READING: Ch 5.1~5.3
NEXT: Linear list– linked presentation (Ch 6)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.