ADT Part1 Handout

download ADT Part1 Handout

of 11

Transcript of ADT Part1 Handout

  • 8/8/2019 ADT Part1 Handout

    1/11

    1

    58

    Linked Lists, Recursion, and ADTsOverview

    Recursion Singly-Linked Lists Abstract Data Types

    References Bruno R. Preiss: Data Structures and Algorithms with Object-

    Oriented Design Patterns in C++. John Wiley & Sons, Inc. (1999)

    Richard F. Gilberg and Behrouz A. Forouzan: Data Structures - APseudocode Approach with C. 2nd Edition. Thomson (2005)

    Russ Miller and Laurence Boxer: Algorithms Sequential & Parallel.2nd Edition. Charles River Media Inc. (2005)

    Stanley B. Lippman, Jose Lajoie, and Barbara E. Moo: C++ Primer.4th Edition. Addison-Wesley (2006)

    59

    Recursion If a procedure that contains within its body calls to itself,

    then this procedure is called to be recursively defined.

    This approach of program specification is called recursion andis found not only in programming.

    If we the define a procedure recursively, then there mustexist at least one sub-problem that can be solved directly,

    that is without calling the procedure again.

    Note: A recursively defined procedure must always contain adirectly solvable sub-problem. Otherwise, this procedure doesnot terminate.

    60

    Impossible Structures

  • 8/8/2019 ADT Part1 Handout

    2/11

    2

    61

    Application of Recursion Recursion is an important problem-solving technique

    in which a given problem is reduced to smallerinstances of the same problem.

    The general structure of a recursive definition is

    X = X

    Right-hand sideLeft-hand side

    62

    Factorials The factorial for positive integers is

    n! = n * (n - 1) * * 1

    The recursive definition:

    n! =1 if n = 0

    n * (n - 1) ! if n > 0

    "#$$%$$

    63

    Calculating Factorials The recursive definition tells us exactly how to calculate a

    factorial:

    4! = 4 * 3! Recursive step: n=4= 4 * (3 * 2!) Recursive step: n=3= 4 * (3 * (2 * 1!)) Recursive step: n=2= 4 * (3 * (2 * (1 * 0!))) Recursive step: n=1= 4 * (3 * (2 * (1 * 1))) Stop condition: n=0= 4 * (3 * (2 * 1))= 4 * (3 * 2)= 4 * 6= 24

  • 8/8/2019 ADT Part1 Handout

    3/11

    3

    64

    Recursive Factorialint factorial( int n )

    {

    if (n == 0)

    return 1; // stop condition

    else

    return n * factorial( n - 1 ); // recursive step

    }

    65

    Towers of Hanoi Problem:

    Move disks from a start peg to a target peg using amiddle peg.

    Challenge:

    All disks have a unique size and at no time must a biggerdisk be placed on top of a smaller one.

    66

    Towers of Hanoi: ConfigurationStart:

    Finish:

    Start Middle Target

    Start Middle Target

  • 8/8/2019 ADT Part1 Handout

    4/11

    4

    67

    A Recursive Solution

    2. Move 1 disk from Start to Target:

    3. Move n-1 disks from Middle to Target:

    1. Move n1 disks from Start to Middle:T

    S

    S M

    S

    T

    T

    M

    M

    68

    A Recursive Solution: IntermediateTS M

    TS M

    TS M

    TS M

    1:

    4:

    3:

    2:

    69

    The Recursive Procedurevoid move( int n, int start, int target, int middle ){

    if ( n > 0 ){

    move( n-1, start, middle, target ); cout

  • 8/8/2019 ADT Part1 Handout

    5/11

    5

    70

    Recursion is a prerequisite for linked lists!

    71

    Problems with Arrays An array is a contiguous storage that provides

    insufficient abstractions for handling addition anddeletion of elements.

    Addition and deletion require n/2 shifts on average.

    The computation time is O(n).

    Resizing effects performance.

    72

    Deletion Requires Shift3 5 29 20 4

    Delete 5

    3 29 20 4

    42029

  • 8/8/2019 ADT Part1 Handout

    6/11

    6

    73

    Addition Requires Shift3 29 20 4

    Insert 50 after 29

    3 29 50 20 4

    420

    74

    A linked list is a sequence of data items, each connected tothe next by a pointer called next.

    A data item may be a primitive value, a composite value, oreven another pointer.

    A linked list is a recursive data structure whose nodes refersto nodes of the same type.

    Linked Lists

    Data Data Data Nil

    75

    Nodesclass Node{

    public:

    DataTypedata;

    Node*next;

    Node( DataType aData, Node* aNext )

    {

    data = aData;

    next = aNext;

    }

    };

    DataType is application-specific

  • 8/8/2019 ADT Part1 Handout

    7/11

    7

    76

    Can we be more specific?

    77

    Templates Templates are blueprints from which classes

    and/or functions automatically generated by thecompiler based on a set of parameters.

    Each time a template is used with differentparameters is used, a new version of the class or

    function is generated.

    A new version of a class or function is calledspecialization of the template.

    78

    Node Class Templatetemplateclass Node{public: DataTypedata; Node* next;

    Node( DataType aData, Node* aNext ){

    data = aData;next = aNext;

    }};

  • 8/8/2019 ADT Part1 Handout

    8/11

    8

    79

    Class Template InstantiationtypedefNode IntegerNode;typedefNode NodeOfIntegerNode;

    Types uses as arguments cannot be classes withlocal scope.

    Once instantiated, a class template can be used asany other class.

    80

    The Need for Pointers A linked-list is a dynamic data structure with a

    varying number of nodes.

    Access to a linked-list is through a pointer variablein which the base type is the same as the nodetype:

    Node* pListOfInteger = (Node*)0;

    Here (Node *)0 stands for Nil.

    81

    Node ConstructionNode *p, *q;

    p = newNode( 5, (Node*)0 );

    q = newNode( 7, p );

    7

    5 Nilp

    q 5 Nil

  • 8/8/2019 ADT Part1 Handout

    9/11

    9

    82

    b:

    a:

    Node AccessNode *p, *q;p = newNode( 5, (Node*)0 );q = newNode( 7, p );

    a = p->data;b = q->next->data;

    7

    5 Nilp

    q 5 Nil

    83

    Insert a NodeNode r*;

    r = new Node( 6, (Node*)0);

    r->next = p;

    q->next = r;

    6

    5 Nilp

    r 5 Nil

    7q 5 Nil6

    Observe the side effect on the node s!

    84

    Delete a Node

    q->next = q->next->next;

    6

    5 Nilp

    r 5 Nil

    7q 5 Nil

    Observe there is no side effect on the node s!

    7q 5 Nil6

  • 8/8/2019 ADT Part1 Handout

    10/11

    10

    85

    Insert at the BeginningNode *pList = (Node*)0;

    pList = newNode( 5, pList );

    pList = newNode( 7, pList );

    7

    5 NilpList

    pList 5 Nil

    86

    Insert at the End To insert a new node at the end of a linked list we

    need to search for the end:

    Node *pList = (Node*)0;

    Node **pToEnd = &pList;

    while ( *pToEnd != (Node*)0 )

    {pToEnd = &(*pToEnd->next); // pointer to next

    }

    *pToEnd = new Node( 9, (Node*)0 );

    87

    Insert at the End : The Pointers5

    9 NilpToEnd

    pList 7 Nil

    5pList 9 Nil7

    *

  • 8/8/2019 ADT Part1 Handout

    11/11

    11

    88

    Insert at the end preserves the orderof list nodes!

    89

    Insert at the End with Aliasing Rather than using a Pointer-to-Pointer we can just record the

    last next pointer.

    Node *pLastNode = (Node*)0;

    Node *pNewNode = new Node( 9, (Node*)0 );

    if ( *pList == (Node*)0 )

    {pLastNode = pNewNode; // make new node last

    pList = pLastNode; // set first node}

    else{

    pLastNode->next = pNewNode; // append new nodepLastNode = pNewNode; // make new node last

    }

    90

    Complications with Singly-Linked Lists

    The deletion of a node at the end of a listrequires a search from the top to find thenew last node.