Data Structures.list Implementation

Embed Size (px)

Citation preview

  • 8/9/2019 Data Structures.list Implementation

    1/46

    CSE 326: Data Structures

    Part Two: Lists

    Henry KautzAutumn Quarter 2002

  • 8/9/2019 Data Structures.list Implementation

    2/46

    Today

    Abstract versus Concrete Data Types

    List ADT

    Iterators

    Comparing implementations

    Sparse vectors

    Nested lists Sparse arrays

    Expressions

  • 8/9/2019 Data Structures.list Implementation

    3/46

    Abstract vs. Concrete Data Types

    Abstract Data Type (ADT) Mathematical description of an object and the set of

    operations on the object List, Stack, Tree, Heap, Graph,

    One ADT may specialize another ADT

    One ADT may implement another ADT

    Concrete Data Type Implementation of an ADT using some set ofprimitive

    data types and operations of known complexity Primitives: integers, arrays, pointers or references

    Object-oriented programming languages (Java, C++) letyou explicitly define new concrete data types thatcorrespond to ADTs.

  • 8/9/2019 Data Structures.list Implementation

    4/46

    List ADT

    Mathematical description: asequence of items

    Ai precedes Ai+1 for 1 i < n

    Operations

    First() = position

    Value(position) = item

    Next(position) = position

    Length() = integer

    Insert(item,position)

    Delete(position)

    ( A1 A2 An-1 An )length = n

    What other operations

    might be useful?

  • 8/9/2019 Data Structures.list Implementation

    5/46

    List ADT

    Mathematical description: asequence of items

    Ai precedes Ai+1 for 1 i < n

    Operations

    First() = position

    Value(position) = item

    Next(position) = position

    Length() = integer

    Insert(item,position)

    Delete(position)

    ( A1 A2 An-1 An )length = n

    What other operations

    might be useful?

    Kth(integer)=item

    SetKth(item,integer)

    Find(item)=position

  • 8/9/2019 Data Structures.list Implementation

    6/46

    Specialization Hierarchy

    ListProperty: Sequence

    First()=pos Value(pos)=item Kth(integer)=item

    Next(pos)=pos Length()=integer SetKth(item,integer)Insert(item,pos) Delete(pos) Find(item)=position

    StackProperty: LIFO

    Push(item)

    Pop()=item

    IsEmpty()=true/false

    QueueProperty: FIFO

    Enqueue(item)

    Dequeue()=item

    IsEmpty()=true/false

    VectorProperty: randomaccess

    Kth(int) = item

    SetKth(item,integer)

  • 8/9/2019 Data Structures.list Implementation

    7/46

    Implementation Hierarchy

    ListComplexity: Unspecified

    First()=pos Value(pos)=item Kth(integer)=item

    Next(pos)=pos Length()=integer SetKth(item,integer)

    Insert(item,pos) Delete(pos) Find(item)=position

    Linked List

    (1) for:

    (n) for:

    Array

    (1) for:

    (n) for:

  • 8/9/2019 Data Structures.list Implementation

    8/46

    Specialization and Implementation

    HierarchiesList

    Stack Queue Vector

    Linked List

    Sorted Vector

  • 8/9/2019 Data Structures.list Implementation

    9/46

    Concrete Data Types

    List

    Linked List

    Linked List using

    References

    Whats an alternative

    implementation?

    nodeB.value = b;

    nodeC.value = c;

    list = nodeB;

    nodeB.next = nodeC

    b c

  • 8/9/2019 Data Structures.list Implementation

    10/46

    Concrete Data Types

    List

    Linked List

    Linked List using

    References

    nodeB.value = b;

    nodeC.value = c;

    list = nodeB;

    nodeB.next = nodeC

    b c

    Linked List using

    Arrays

    c b

    0 2

    list = 4;

    1 2 3 4 5

  • 8/9/2019 Data Structures.list Implementation

    11/46

    Linked Lists in C

    struct node{Object element;

    struct node * next; }

    Everything else is a pointer to a node!

    typedef stuct node * List;

    typedef struct node * Position;

    a b c L

  • 8/9/2019 Data Structures.list Implementation

    12/46

    Linked Lists in Java version 1

    References to objects are implicitpointers

    class ListNode{

    Object element;

    ListNode next; }

    class List{

    Listnode head;

    Listnode find(Object item) {

    Listnode n = head;

    while (n != null) {

    if (n.element == item)

    return n; }

    return null; }

  • 8/9/2019 Data Structures.list Implementation

    13/46

    Data Hiding

    Good programming style hides internal details of an objectfrom the rest of the program

    Guarantees that data structure always works as expected cannot

    easily be corrupted

    Here, must make details of ListNode and List public

    Type returned by find

    For iterating through a list:

    ListNode n;

    for (n = mylist.head; n!= null; n = n.next){

    v = n.element;

    do something on each v

    }

  • 8/9/2019 Data Structures.list Implementation

    14/46

    Iterators Introduce a new public class to explicitly represent a position in a list

    public class LinkedListItr {

    ListNode current;

    public Object retrieve() {

    return current.element; }

    public void advance() {

    current = current.next; }

    Then:

    LinkedListItr i;

    for (i = mylist.first(); !i.pastEnd(); i.advance){

    do something on each v.retrieve()

    }

  • 8/9/2019 Data Structures.list Implementation

    15/46

    Abstract Iterators

    Iterators can also be defined for an arrayimplementation of lists:

    public class ArrayListItr {

    Object [] data;

    integer current;

    public Object retrieve() {

    return data[current]; }

    public void advance() {

    current = current+1; }

    We can create an abstractiterator that works forboth

    linked list and array implements of List

  • 8/9/2019 Data Structures.list Implementation

    16/46

    Abstract Iterator

    abstract class ListItr {

    abstract Object retrieve();

    abstract void advance();

    }

    class LinkedListItr extends ListItr { }

    class ArrayListItr extends ListItr { }

    Why do this?

  • 8/9/2019 Data Structures.list Implementation

    17/46

    Array Implementation of Linked Lists

    How do we implement

    Delete(position) ?

    Insert(element, position)?

    F O A R N R T

    3 8 6 4 -1 10 5

    Data

    Next

    1 7 92 3 4 5 6 8 10

    First = 2

  • 8/9/2019 Data Structures.list Implementation

    18/46

    Free Cell Management

    When an item is removed from the list, mustreclaim the unused cell for later use

    Can use same array to manage a second list of

    unused cells

    F O A R N R T

    7 9 03 8 6 4 -1 10 5

    Data

    Next

    1 7 92 3 4 5 6 8 10

    First = 2 Free = 1

  • 8/9/2019 Data Structures.list Implementation

    19/46

    Memory Management

    Keeping a free cell list is an example of a memory

    management strategy

    How is memory managed in C?

    C++?

    Java?

  • 8/9/2019 Data Structures.list Implementation

    20/46

    Summary: ComplexityLinked list Array Sorted

    array

    Kth(int)

    Find(e)

    Insert(e,pos)

    Next(pos)

    InsertAnywhere(e)

  • 8/9/2019 Data Structures.list Implementation

    21/46

    To ADT or NOT to ADT?

    Issue: when to bypass / expand List ADT?

    Using general list (stack) operations:

    List reverse(List x) {

    y = new List;

    while (! x.isEmpty())

    y.Push( x.Pop() )return y; }

    Disadvantages?

  • 8/9/2019 Data Structures.list Implementation

    22/46

    Destructive Method

    Reverse() {

    ListNode x, last, tmp;

    x = head;

    last = null;

    while (x.next != null){tmp = x.next;

    x.next = last;

    last = x;

    x = tmp;}

    head = x; }

    a b c x

    a b c x

    Faster in practice?

    Asymptotically faster?

  • 8/9/2019 Data Structures.list Implementation

    23/46

    Slow Reverse

    List reverse(List x) {

    y = new List;

    for (i=1; i

  • 8/9/2019 Data Structures.list Implementation

    24/46

    Polynomial ADT

    Possible linked list implementation:

    Aiis the coefficient of the xi-1 term:

    5 + 2x + 3x2 ( 5 2 3 )

    7 + 8x ( 7 8 )

    3 + x2 ( 3 0 2 )

    Problem?

    List ADT

  • 8/9/2019 Data Structures.list Implementation

    25/46

    4 + 3x2001

    ( 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )

  • 8/9/2019 Data Structures.list Implementation

    26/46

    Sparse Vector Data Structure:

    4 + 3x2001

    4

    0

    3

    2001

    ( )

    class Term { int coeff, power; }

    class PolyNode extends ListNode {Term data; }

    class Polynomial extends List {

    Polynode header;

    }

  • 8/9/2019 Data Structures.list Implementation

    27/46

    Addition of Two Polynomials?

    Complexity?

    1050 31200

    15+10x50 +3x1200

    150p

    30

    50

    4

    100

    5+30x50 +4x100

    5

    0

    q

  • 8/9/2019 Data Structures.list Implementation

    28/46

    Addition of Two Polynomials

    One pass down each list: (n+m)

    1050 31200

    15+10x50 +3x1200

    150p

    30

    50

    4

    100

    5+30x50 +4x100

    5

    0

    q

    4

    100

    3

    1200

    40

    50

    20

    0

    r

  • 8/9/2019 Data Structures.list Implementation

    29/46

    Sparse Matrices

    How could we represent this compactly?

    1 8 0 3 3 0 0 0

    0 0 0 0 0 0

    0 0 0 0 0 0

    0 0 0 9 9 0 0

    0 0 0 0 0 00 0 0 0 0 2 7

  • 8/9/2019 Data Structures.list Implementation

    30/46

    Sparse Matrices

    How could we represent this compactly?

    ( (row (column data) (column data) )

    (row ( (column data) (column data) ) )

    ( (1 (1 18) (2 33)(4 (4 99))

    (5 (5 27)) )

    1 8 0 3 3 0 0 0

    0 0 0 0 0 0

    0 0 0 0 0 0

    0 0 0 9 9 0 0

    0 0 0 0 0 00 0 0 0 0 2 7

  • 8/9/2019 Data Structures.list Implementation

    31/46

    Nested Polymorphic Lists

    Polymorphic elements in list may be of different

    types

    Trivial in Java, doable butpainful in C++ Nested elements of a list is are lists

    Nested polymorphic list elements of list may be

    of non-list types (e.g. int) or lists

  • 8/9/2019 Data Structures.list Implementation

    32/46

    Universal Data Structure

    NPLs can be used to implement any of the other

    linked data structures well cover in the course

    Trees, heaps, graphs, disjoint sets LISP programming language that uses NPLs to

    representboth data and programs

    LISP pioneered (in the 1950s!) many ideas that are the

    basis of modern programming languages Recursion, garbage collection, interactive programming

    environments

    JAVA combines the best features of LISP with the best features of

    C++ (strict type checking, objects and inheritance)

    http://www.hypermeta.com/graphics/powered-by-lisp.gifhttp://www.hypermeta.com/graphics/powered-by-lisp.gifhttp://www.hypermeta.com/graphics/powered-by-lisp.gifhttp://www.hypermeta.com/graphics/powered-by-lisp.gif
  • 8/9/2019 Data Structures.list Implementation

    33/46

    Programs As Lists

    (progn

    (setq x 10)

    (setq y 1)

    (while (greater-than x 1)

    (progn

    (setq y (times y x))(setq x (minus x 1)))))

  • 8/9/2019 Data Structures.list Implementation

    34/46

    -Lisp

    Lets see how easy it is to build our ownprogramming language interpreter

    (plus (times (minus 6 2) 14) 8)

    Grammar for an expression:

    expression :: = integer

    | ( symbol {expression}* )

    symbol ::= [a-z]+

    repeat 0 ormore times

    repeat 1 or

    more times

  • 8/9/2019 Data Structures.list Implementation

    35/46

    Lists

    For these slides: convention that head field of list

    points directly to first node of list (no dummy node)

    class Node {

    Object element;

    Node next; }

    class List {

    Node head;

    }

  • 8/9/2019 Data Structures.list Implementation

    36/46

    Read/Eval/Print Loop

    The top level program:

    While (not EOF)

    Print( Eval( ReadExpression()))

  • 8/9/2019 Data Structures.list Implementation

    37/46

    Reading Expressions

    ReadExpression() {

    t = GetToken();

    if (t is numeric)

    return new Integer(t);

    if (t is ())

    return ReadList();

    if (t is )) return null;

    else return new Symbol(t);

    }

    see Weiss

    for

    tokenizer

  • 8/9/2019 Data Structures.list Implementation

    38/46

    ReadList

    ReadList() {

    e = ReadExpression();

    if (e == null)

    return a new empty List;

    else return

    Push( e, ReadList() );}

    Why is the recursive call inside the Push?

  • 8/9/2019 Data Structures.list Implementation

    39/46

  • 8/9/2019 Data Structures.list Implementation

    40/46

    EvalList

    EvalList(Node n){

    if (n == null)

    return a new empty list;

    else return

    Push( Eval(n.element),

    EvalList(n.next) );}

  • 8/9/2019 Data Structures.list Implementation

    41/46

    Apply

    Apply(f, params){

    if (f is plus)

    return sum of params

    else if (f is minus)

    return difference of params

    else } params is always a list

    of ints it has been

    fully evaluated!

  • 8/9/2019 Data Structures.list Implementation

    42/46

    Thats It!

    33 lines of pseudo-code

    About 100 lines of Java

    What about variables? If statements? Loops?

  • 8/9/2019 Data Structures.list Implementation

    43/46

    Adding Variables

    Keep an symbol table list of variable/value pairs

    ( (a 15) (b 22) (c 2) )

    Add to Eval: to evaluate a variable, find it in the symbol table and

    return its value if it is not the table, error.

    to set a variable, check for the special form

    (set symbol expression)

  • 8/9/2019 Data Structures.list Implementation

    44/46

    Eval with VariablesEval( e ){

    if (e is an Integer)

    return its value;

    if (e is a List)

    f = e.head.element;

    if (f is set){

    var = e.head.next.element;

    val = e.head.next.next.element;

    Put (var,val) in symbol table; }else return Apply(f,

    EvalList(e.head.next))

    else return es value in symbol table;

    }

  • 8/9/2019 Data Structures.list Implementation

    45/46

  • 8/9/2019 Data Structures.list Implementation

    46/46

    Coming Up

    Sorting

    Weiss Chapter 7

    Know how the following work: Bubble Sort (selection sort)

    Merge Sort

    Quicksort