Data Structures & Algorithms_lecture 3

download Data Structures & Algorithms_lecture 3

of 50

Transcript of Data Structures & Algorithms_lecture 3

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    1/50

    DATA STRUCTURESANDALGORITHMS

    Lecture Notes 3

    Prepared by nan TAHRALI

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    2/50

    2

    ROAD MAP Abstract Data Types (ADT)

    The List ADT

    Implementation of Lists

    Array implementation of lists

    Linked list implementation of lists

    Cursor implementation of lists

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    3/50

    3

    Abstract Data Types (ADT) Definition :

    Is a set of operation

    Mathematical abstraction

    No implementation detail

    Example :Lists, sets, graphs, stacks are examples of

    ADT along with their operations

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    4/50

    4

    Why ADT ? Modularity

    divide program into small functions

    easy to debug and maintain easy to modify

    group work

    Reuse do some operations only once

    Easy to change of implementation transparent to the program

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    5/50

    5

    THE LIST ADT Ordered sequence of data items called

    elements A

    1

    , A2

    , A3

    , ,AN

    is a list of size N size of an empty list is 0 Ai+1 succeeds Ai Ai-1 preceeds Ai

    position of Ai is i first element is A1called head last element is AN called tail

    Operations ?

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    6/50

    6

    THE LIST ADT Operations

    PrintList

    Find FindKth

    Insert

    Delete

    Next

    Previous

    MakeEmpty

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    7/50

    7

    THE LIST ADT Example:

    the elements of a list are

    34, 12, 52, 16, 12

    Find (52) 3

    Insert (20, 3) 34, 12, 52, 20, 16, 12

    Delete (52) 34, 12, 20, 16, 12

    FindKth (3) 20

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    8/50

    8

    Implementation of Lists Many Implementations

    Array

    Linked List

    Cursor (linked list using arrays)

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    9/50

    9

    ROAD MAP Abstract Data Types (ADT)

    The List ADT

    Implementation of Lists

    Array implementation of lists

    Linked list implementation of lists

    Cursor implementation of lists

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    10/50

    10

    Array Implementation of List ADT Need to define a size for array

    High overestimate (waste of space)

    Operations Running TimesPrintList O(N)

    Find

    Insert O(N) (on avarage half needs to be moved)

    Delete

    FindKthNext O(1)Previous

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    11/50

    11

    Array Implementation of List ADT Disadvantages :

    insertion and deletion is very slow

    need to move elements of the list

    redundant memory space

    it is difficult to estimate the size of array

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    12/50

    12

    ROAD MAP Abstract Data Types (ADT)

    The List ADT

    Implementation of Lists

    Array implementation of lists

    Linked list implementation of lists

    Cursor implementation of lists

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    13/50

    13

    Linked List Implementation of Lists Series of nodes

    not adjacent in memory

    contain the element and a pointer to a node containing its

    succesor

    Avoids the linear cost of insertion and deletion !

    A1 A4A2 A3

    A1 500 A4 0A2 400 A3 666

    350 500 400 666

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    14/50

    14

    Linked List Implementation of Lists Insertion into a linked list

    A2 400

    X

    A1 500 A4 0A3 666

    350 500 400 666

    A2 530 X 400A1 500 A4 0A3 666

    350 500 400 666530

    530

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    15/50

    15

    Linked List Implementation of Lists Deletion from a linked list

    A2 400A1 500 A4 0A3 666

    350 500 400 666

    A2 666A1 500 A4 0

    350 500 666

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    16/50

    16

    Linked List Implementation of Lists

    Need to know where the first node is

    the rest of the nodes can be accessed

    No need to move the list for insertionand deletion operations

    No memory waste

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    17/50

    17

    Linked List Implementation of Lists

    Linked List Array

    PrintList O(N) (traverse the list) O(N)

    Find

    FindKth (L,i) O(i) O(1)

    Delete O(1) O(N)

    Insert

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    18/50

    18

    Programming Details

    There are 3 special cases for linked lists

    Insert an element at the front of the list

    there is no really obvious way Delete an element from the front of the list

    changes the start of the list

    Delete an element in general

    requires to keep track of the node before the deleted one

    How can we solve these three problems ?

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    19/50

    19

    Programming Details

    Keep a header node in position 0

    A1 A4A2 A3header

    Write a FindPrevious routine

    returns the predecessor of the cell

    To delete the first element FindPrevious routine returns the position of

    header

    Use of header node is controversial !

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    20/50

    20

    Type decleration for link list nodetemplate

    class List; // Incomplete declaration.

    template

    class ListItr; // Incomplete declaration.

    template

    class ListNode {

    ListNode( const Object & theElement = Object( ),ListNode*n=NULL) : element(theElement),next(n) {}

    Object element;

    ListNode *next;

    friend class List;

    friend class ListItr;

    };

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    21/50

    21

    Iterator class for linked liststemplate

    class ListItr {

    public:

    ListItr( ) : current( NULL ) { }

    bool isPastEnd( ) const { return current == NULL; }

    void advance( )

    { if( !isPastEnd( ) ) current = current->next; }

    const Object & retrieve( ) const

    { if( isPastEnd( ) )

    throw BadIterator( );

    return current->element; }

    private:

    ListNode *current; // Current position

    ListItr(ListNode *theNode):current( theNode ) { }

    friend class List; // Grant access to constructor

    };

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    22/50

    22

    List class interfacetemplate

    class List {

    public:

    List( );

    List( const List & rhs );

    ~List( );

    bool isEmpty( ) const;

    void makeEmpty( );

    ListItr zeroth( ) const;

    ListItr first( ) const;

    void insert( const Object & x, const ListItr & p );

    ListItr find( const Object & x ) const;ListItr findPrevious( const Object & x ) const;

    void remove( const Object & x );

    const List & operator=( const List & rhs );

    private:

    ListNode *header;

    };

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    23/50

    23

    Function to print a list

    template

    void printList( const List &the List)

    {

    if (theList.isEmpty())

    cout

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    24/50

    24

    Some list one-liners/* Construct the list */

    template

    List::List( )

    {header = new ListNode;

    }

    /* Test if the list is logically empty */

    template

    bool List::isEmpty( ) const

    {

    return header->next == NULL;

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    25/50

    25

    Some list one liners/* Return an iterator representing the header node

    template

    ListItr List::zeroth( ) const

    {

    return ListItr( header );}

    /* Return an iterator representing the first nodein the list. This operation is valid for emptylists. */

    template

    ListItr List::first( ) const

    {

    return ListItr( header->next );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    26/50

    26

    Find routine

    /* Return iterator corresponding to the firstnode containing an item x. Iterator isPastEndif item is not found. */

    template ListItr List::find( constObject & x ) const

    {

    ListNode *itr = header->next;

    while( itr != NULL && itr->element != x )

    itr = itr->next;

    return ListItr( itr );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    27/50

    27

    Deletion routine for linked lists

    /* Remove the first occurrence of an item x. */

    template

    void List::remove( const Object & x )

    {

    ListItr p = findPrevious( x );

    if( p.current->next != NULL )

    {

    ListNode *oldNode = p.current->next;p.current->next = p.current->next->next;

    delete oldNode;

    }

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    28/50

    28

    findPrevious-the find routine foruse with remove

    /*Return iterator prior to the first node containing anitem x.

    template ListItr List::findPrevious( const Object &

    x ) const

    {

    ListNode *itr = header;

    while( itr->next != NULL && itr->next->element != x )itr = itr->next;

    return ListItr( itr );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    29/50

    29

    Insertion routine for linked lists

    /* Insert item x after p. */

    template

    void List::insert( const Object & x,const ListItr & p )

    {

    if( p.current != NULL )

    p.current->next = new ListNode

    ( x, p.current->next );}

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    30/50

    30

    makeEmpty and List destructor

    /* Make the list logically empty. */

    template

    void List::makeEmpty( )

    {

    while( !isEmpty( ) )remove( first( ).retrieve( ) );

    }

    /* Destructor */

    template

    List::~List( ){

    makeEmpty( );

    delete header;

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    31/50

    31

    List copy routines: operator=

    /*Deep copy of linked lists.

    template

    const List & List::operator=( constList & rhs )

    {ListItr ritr = rhs.first( );

    ListItr itr = zeroth( );

    if( this != &rhs )

    {

    makeEmpty( );for( ; !ritr.isPastEnd( ); ritr.advance( ),itr.advance( ))

    insert( ritr.retrieve( ), itr );

    }

    return *this;

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    32/50

    32

    List copy routines : copy constructor

    /* Copy constructor

    template

    List::List( const List & rhs )

    {

    header = new ListNode;

    *this = rhs;

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    33/50

    33

    Doubly Linked List

    Traversing list backwards

    not easy with regular lists Insertion and deletion more pointer fixing

    Deletion is easier

    Previous node is easy to find

    A1 A2 A3

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    34/50

    34

    Circulary Linked List

    Last node points the first

    A1 A2 A3

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    35/50

    35

    ROAD MAP

    Abstract Data Types (ADT)

    The List ADT

    Implementation of ListsArray implementation of lists

    Linked list implementation of lists

    Cursor implementation of lists

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    36/50

    36

    Cursor Implementation of Linked List

    Problems with linked list implementation:

    Same language do not support pointers !

    Then how can you use linked lists ?

    new and free operations are slow

    Actually not constant time

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    37/50

    37

    Cursor Implementation of Linked List

    SOLUTION: Implement linked list on an array

    called CURSOR

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    38/50

    38

    Cursor Implementation of Linked List

    Cursor operation simulates the features

    Collection of structures

    uses array for nodes

    Array index is pointer

    new and delete operation

    Keep a free list

    newreturns an element from freelist

    delete place the node in freelist

    Freelist

    Use cell 0 as header

    All nodes are free initially

    0 is a NULL pointer

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    39/50

    39

    Cursor Implementation of Linked List

    If L = 5, then L represents list (A, B, E)

    If M = 3, then M represents list (C, D, F)

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    40/50

    40

    Iterator for cursorimplementation of linked liststemplate

    class ListItr

    {

    public:

    ListItr( ) : current( 0 ) { }

    bool isPastEnd( ) const {return current == 0; }void advance( ){

    if( !isPastEnd( ) )

    current = List::cursorSpace[ current ].next; }

    const Object & retrieve( ) const {

    if( isPastEnd( ) ) throw BadIterator( );

    return List::cursorSpace[ current ].element; }

    private:

    int current; // Current position

    friend class List;

    ListItr( int theNode ) : current( theNode ) { }

    };

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    41/50

    41

    Class skeleton for cursor-based List

    template

    class ListItr; // Incomplete declaration.

    template

    class List

    {public:

    List( );

    List( const List & rhs );

    ~List( );

    bool isEmpty( ) const;

    void makeEmpty( );ListItr zeroth( ) const;

    ListItr first( ) const;

    void insert( const Object & x, const ListItr & p );

    ListItr find( const Object & x ) const;

    ListItr findPrevious( const Object & x ) const;

    void remove( const Object & x );

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    42/50

    42

    Class skeleton for cursor-based List

    public:

    struct CursorNode

    {

    CursorNode( ) : next( 0 ) { }

    private:

    CursorNode( const Object & theElement, int n )

    : element( theElement ), next( n ) {}

    Object element;

    int next;

    friend class List;

    friend class ListItr;

    };

    const List & operator=( const List & rhs );

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    43/50

    43

    Class skeleton for cursor-based List

    private:

    int header;

    static vector cursorSpace;

    static void initializeCursorSpace( );

    static int alloc( );

    static void free( int p );

    friend class ListItr;

    };

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    44/50

    44

    cursorSpace initialization

    /* Routine to initialize the cursorSpace. */

    template

    void List::initializeCursorSpace( )

    {static int cursorSpaceIsInitialized = false;

    if( !cursorSpaceIsInitialized )

    {

    cursorSpace.resize( 100 );

    for( int i = 0; i < cursorSpace.size( ); i++ )cursorSpace[ i ].next = i + 1;

    cursorSpace[ cursorSpace.size( ) - 1 ].next = 0;

    cursorSpaceIsInitialized = true;

    }

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    45/50

    45

    Routines : alloc and free

    /* Allocate a CursorNode

    template

    int List::alloc( )

    {

    int p = cursorSpace[ 0 ].next;cursorSpace[ 0 ].next = cursorSpace[ p ].next;

    return p;

    }

    /* Free a CursorNode

    template void List::free( int p )

    {

    cursorSpace[ p ].next = cursorSpace[ 0 ].next;

    cursorSpace[ 0 ].next = p;

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    46/50

    46

    Short routines for cursor-based lists

    /* Construct the list

    template

    List::List( )

    {

    initializeCursorSpace( );header = alloc( );

    cursorSpace[ header ].next = 0;

    }

    /* Destroy the list

    template List::~List( )

    {

    makeEmpty( );

    free( header );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    47/50

    47

    Short routines for cursor-based lists

    /* Test if the list is logically empty. return true ifempty

    template

    bool List::isEmpty( ) const

    { return cursorSpace[ header ].next == 0;

    }

    /* Return an iterator representing the first node inthe list. This operation is valid for empty lists.

    template

    ListItr List::first( ) const

    {

    return ListItr( cursorSpace[ header ].next );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    48/50

    48

    find routine - cursor implementation

    /*Return iterator corresponding to the first node containingan item x. Iterator isPastEnd if item is not found.

    template

    ListItr List::find( const Object & x ) const

    {

    int itr = cursorSpace[ header ].next;

    while( itr != 0 && cursorSpace[ itr ].element != x )

    itr = cursorSpace[ itr ].next;

    return ListItr( itr );

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    49/50

    49

    insertion routine-cursor implementation

    /* Insert item x after p.

    template

    void List::insert(const Object & x,const ListItr & p)

    {

    if( p.current != 0 ){

    int pos = p.current;

    int tmp = alloc( );

    cursorSpace[ tmp ] = CursorNode( x, cursorSpace[ pos ].next );

    cursorSpace[ pos ].next = tmp;

    }

    }

  • 7/29/2019 Data Structures & Algorithms_lecture 3

    50/50

    deletion routine - cursor implementation

    /* Remove the first occurrence of an item x.

    template

    void List::remove( const Object & x )

    {

    ListItr p = findPrevious( x );int pos = p.current;

    if( cursorSpace[ pos ].next != 0 )

    {

    int tmp = cursorSpace[ pos ].next;

    cursorSpace[ pos ].next = cursorSpace[ tmp ].next;free ( tmp );

    }

    }