Lists - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Lists

Description:

This lecture introduces the list ADT: What it is (conceptual) Why we use it (applications) ... F2.2AO2 Data Structures and Algorithms ... 10 best albums. Teams ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 25
Provided by: stuart154
Category:
Tags: albums | lists

less

Transcript and Presenter's Notes

Title: Lists


1
Lists Pauline A. Wilcox and Brian
Palmer Department of Computer Science
2
Aims
  • This lecture introduces the list ADT
  • What it is (conceptual)
  • Why we use it (applications)
  • How we implement it (implementation)

3
Conceptual View
  • Linear List
  • L (e0, e1, e2, e3, , en-1)
  • relationships
  • e0 is the zeroth (or front) element
  • en-1 is the last element
  • ei immediately precedes ei1

4
Example Applications
  • Example Lists
  • Students in F22AO2 (Humphrey, Emma, Stuart)
  • Questions in exam in F22AO2 (q1, q2, q3, q4)
  • Days of the week (Sun, Mon, Tue, Wed, Thu, Fri,
    Sat)
  • World's longest rivers
  • 10 best albums
  • Teams
  • Underlies, or is a more general case of, other
    data structures such as stack and queue

5
List Operations
  • Useful set of operations
  • Determine list size
  • Get element with given index
  • Determine the index of an element
  • Remove and return element with given index
  • Add an element at a specified index

6
List Size and Get Element
  • Assume L (a,b,c,d,e)
  • Determine list size
  • size 5
  • Get element with given index
  • get(0) a
  • get(2) c
  • get(4) e
  • get(-1) error
  • get(9) error

7
Find Index of List Element
  • Assume L (a,b,d,b,a)
  • Determine the index of an element
  • indexOf(d) 2
  • indexOf(a) 0
  • indexOf(z) -1

8
Remove Element
  • Assume L (a,b,c,d,e,f,g)
  • Remove and return element with given index
  • remove(2) returns c
  • and L becomes (a,b,d,e,f,g)
  • index of d,e,f, and g decrease by 1
  • remove(-1) error
  • remove(20) error

9
Add Element
  • Assume L (a,b,c,d,e,f,g)
  • Add an element at a specified index
  • add(0,h)
  • modified list L (h,a,b,c,d,e,f,g)
  • index of a,b,c,d,e,f, and g increase by 1
  • add(2,h)
  • modified list L (a,b,h,c,d,e,f,g)
  • index of c,d,e,f, and g increase by 1
  • add(10,h) error
  • add(-6,h) error

10
Describing the ADT
  • 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
  • indexOf(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(index, x) insert x as the indexth
    element, later elements
  • have their index increased by 1
  • output() output the list elements from
    left to right

11
List in Java
  • List is an interface
  • Apart from methods specified in the Collection
    interface, it contains these methods
  • E get(int index)
  • E set(int index, E element)
  • void add(int index, E element)
  • E remove(int index)
  • int indexOf(Object o)
  • int lastIndexOf(Object o)
  • List contains all the ADT methods, except output,
    instead of which it provides, as all collections
    do, an iterator

12
Iterators
  • Iterators enable us to iterate over or traverse a
    collection
  • Traversal is straightforward enough with a list
    but can be a bit of a business with other data
    structures
  • Iterator is an interface (in java.util)
  • It contains only three methods
  • hasNext()
  • next()
  • remove() optional
  • Most collections have an iterator() method that
    will return an implementation of these methods

13
Java List classes
  • Ten classes implement the List interface
  • They include Stack (which we've used already),
    ArrayList and LinkedList
  • The ArrayList is based on a resizable array. In
    general, it works faster than the LinkedList
  • The LinkedList is a doubly-linked list and is
    more flexible in application than the ArrayList.
  • LinkedList provides methods to get, remove and
    insert at either end of the list. So it can be
    used as a stack, a queue, or a double-ended queue
    (deque).

14
Algorithm analysis
  • Algorithms differ in respect of intrinsic
    complexity and running time
  • If there is not a lot of data to process,
    differences in running time are negligible use
    the algorithm that you find easiest
  • As the amount of data grows, the running time
    grows in significance
  • O Big-Oh analysis relates to running time

15
Running times
  • Function
  • c
  • log N
  • N
  • N log N
  • N2
  • 2N
  • Source Data Structures etc, by Mark Weiss
  • Name
  • Constant
  • Logarithmic
  • Linear
  • N log N
  • Quadratic
  • Exponential

16
O
  • Big-Oh represents the order of time something
    will take
  • T(n) is O(F(n)) if there are positive constants c
    and n0 such that T(n) lt cF(n) when n gt n0
  • The Time needed for n operations is of the Order
    of a certain function of n
  • Big-Oh represents a theoretical upper bound of
    the time taken to carry out algorithmic
    operations
  • Note that a higher order function might in fact
    be quicker in a certain range, depending on the
    size of the constants

17
O and English
  • O (1)
  • O (log n)
  • O (n)
  • O (n log n)
  • O (n2)
  • O (2n)
  • Constant
  • Log / logarithmic
  • Linear
  • n log n (linear-log)
  • Quadratic
  • Exponential

18
ArrayList and LinkedList and time
  • The lists are similar.
  • size, isEmpty, get, set, and iterator run in
    constant time O(1). The other ArrayList
    operations run in linear time, O(n), except for
    add
  • Where operations with the same name are described
    as constant for both kinds of list, the constant
    will be smaller for the ArrayList
  • add and remove in the ArrayList need time to
    shift elements along
  • Sometimes add requires the underlying array to
    increase in capacity. This takes time, which is
    averaged over all adds to give what's called
    amortized constant time (cf paying tax monthly)
  • With the LinkedList, add and remove are constant
    (O(1))
  • If the most common list operations in an
    application are going to be add and remove, use a
    LinkedList. Otherwise, an ArrayList

19
Interfaces Recommended
  • Java specifies data structures as interfaces
  • e.g. java.util.List
  • We can declare an interface as a variable, but we
    have to instantiate an implemented class
  • ListltStringgtlist new ArrayListltStringgt()
  • ArrayList implements List
  • The variable list will have only the methods of a
    List

20
Iterators
  • Java Iterator interface
  • public interface Iterator
  • boolean hasNext()
  • Object next()
  • void remove() // Optional
  • This permits you to examine the elements of a
    data structure one at a time.

21
Example code (ill-constructed)
  • List ltIntegergt list new ArrayListltIntegergt()
  • // Note boxing of int values
  • list.add(0, 4)
  • list.add(0, 3)
  • list.add(0, 2)
  • list.add(0, 1)
  • list.add(0, 0)
  • Iterator it list.iterator()
  • while (it.hasNext())
  • // Note unboxing of Integer values
  • System.out.println(it.next())

22
Alternative Approaches
  • Iterator Approach
  • Iterator it list.iterator()
  • while (it.hasNext())
  • System.out.println(it.next())
  • Or using (the new) for loop
  • for (Integer i list)
  • System.out.println(list.get(i))

23
Merits Of An Iterator
  • Complexity
  • it is often possible to implement the method next
    so that its complexity is less than that of get
  • A data structure does not necessarily have a get
    by index method
  • The Iterator provides a uniform way to traverse
    the elements of a data structure

24
Activities
  • Bookmark Java API
  • http//java.sun.com/j2se/1.3/docs/api/index.html
  • Java Tutorial
  • http//java.sun.com/docs/books/tutorial/
Write a Comment
User Comments (0)
About PowerShow.com