data structures it

Upload: romeofatima

Post on 24-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Data Structures IT

    1/43

    FAP:UC-BCF 1

    dvanced Data Structures

    Chapter II:

    Data Structures

  • 7/25/2019 Data Structures IT

    2/43

    FAP:UC-BCF 2

    Terminologies

    Data Types:Refer to the different kinds of data that a variable may hold in aprogramming languages

    Examples:

    integer

    real

    floatchar

    string

  • 7/25/2019 Data Structures IT

    3/43

    FAP:UC-BCF 3

    Terminologies

    Data Objects:Refer to the set of elements

    Example: data object integer refers to

    D = {0, 1, 2, 3, }

  • 7/25/2019 Data Structures IT

    4/43FAP:UC-BCF 4

    Terminologies

    Data Structures:

    Describe the set of objects and how they are related.

    Describe the set of operations that can be applied on theelements of a data object.

    Typically this describe the more complex data types such asarrays, stacks, queues, trees, graphs and operations of how

    to manipulate these.

  • 7/25/2019 Data Structures IT

    5/43FAP:UC-BCF 5

    Records

    A structure that can have a number of heterogeneouselements.Declaration: Recor dType

    t ypedef st r uct

    {

    f i el d1;

    f i el d2;

    f i el d3; f i el dN;

    }Recor dType;

    f i el d1

    f i el d2

    f i el d3

    .

    .

    f i el dN

  • 7/25/2019 Data Structures IT

    6/43FAP:UC-BCF 6

    Records

    Field1, field2, field3 fieldN can be of any data type (i.e. integer,float, real, record)

    To define a variable for the record:

    Recor dType A;Assigning a value to a record field:

    A. f i el d1 = ;

    A. f i el d2 = ;To retrieve a value from a record field:

    pr i nt f ( %d, A. f i el d1) ;

    pr i nt f ( %c, A. f i el d2) ;

    pr i nt f ( %d: 4: 2, A. f i el d3) ;

  • 7/25/2019 Data Structures IT

    7/43FAP:UC-BCF 7

    Arrays

    Consecutive set of memory locations is a set of pairs index and a value finite, ordered set of homogeneouselements.

    Forms:

    - one-dimensional array

    - n-dimensional array

  • 7/25/2019 Data Structures IT

    8/43FAP:UC-BCF 8

    Arrays

    2 data types Base type of component type

    Index type

    Declaration i nt A[ 10] ; char B[ 45] ;

    2 basic operations Extraction

    Storing

  • 7/25/2019 Data Structures IT

    9/43

    FAP:UC-BCF 9

    Arrays

    If an array is declared to be A[n], then:

    n = number of elements

    If an array is declared to be A[n][m], thenn*m = number of elements

    if given n-dimensional array declarationA[b][c][d][n] = b,c, n

  • 7/25/2019 Data Structures IT

    10/43

    FAP:UC-BCF 10

    Arrays

    To determine the ith element of a Single-dimensionarray:

    A[ i ] = + ( i ) * esi ze

    wher e:

    - base or st ar t i ng addr ess

    i el ementesi ze el ement si ze i n byt es

    Exampl e: Det er mi ne t he addr ess of 5t h el ement of an

    i nt eger ar r ay A wi t h a st ar t i ng addr ess of 2000

  • 7/25/2019 Data Structures IT

    11/43

    FAP:UC-BCF 11

    Arrays

    To determine the ith element of a Two-dimension array:

    A[ i ] [ j ] = + [ ( i ) *( UB2) +( j ) ] * esi ze

    wher e:

    UB2 upper bound of t he 2nd di mensi on

    - base or st ar t i ng addr ess

    esi ze el ement si ze i n byt es

  • 7/25/2019 Data Structures IT

    12/43

    FAP:UC-BCF 12

    Arrays

    To determine the ith element of a Three-dimension array:

    A[ i ] [ j ] [ k] = +[ ( i ) *( UB2) *( UB3) +( j ) *( UB3) +( k) ] *esi ze

    wher e:

    UB3 upper bound of t he 3r d di mensi on

    UB2 upper bound of t he 2nd di mensi on - base or st ar t i ng addr ess

    esi ze el ement si ze i n byt es

  • 7/25/2019 Data Structures IT

    13/43

    FAP:UC-BCF 13

    Arrays

    Exercises:

    1. Given A[10][3][3][6], =2000, esize=4 bytes:

    a. f i nd t he f or mul a t o r epr esent an el ement i n a 4-di mensi onal ar r ay.

    b. f i nd t he t ot al number of el ement s

    c. f i nd t he addr ess of A[ 2] [ 2] [ 0] [ 4]

    2. Given X[8][3][6][2][3], =3000, esize=3 bytes:

    a. f i nd t he t ot al number of el ement s

    b. f i nd t he addr ess of X[ 0] [ 2] [ 5] [ 1] [ 2]

  • 7/25/2019 Data Structures IT

    14/43

    FAP:UC-BCF 14

    Arrays

    3. Consider the following declaration:

    t ypedef st r uct {

    i nt A;

    char B[ 10] ;

    f l oat C;

    char D;

    }r ect ype;

    t ypedef r ect ype mat r i x[ 121] [ 4] [ 5] ;

    mat r i x A1;

    a. comput e t he addr ess of el ement A1[ 120] [ 3] [ 3] gi ven t hebase addr ess at 2000.

    b. Assume t hat we do not know t he si ze of RECTYPE i nnumber of byt es but we know t hat t he addr ess of

    A1[ 20] [ 2] [ 3] i s 2160. Gi ve t he si ze of RECTYPE i nbyt es. Assume t he base addr ess i s at 2000.

  • 7/25/2019 Data Structures IT

    15/43

    FAP:UC-BCF 15

    Stacks

    An ordered list in which all insertions and deletions aremade at one end called the TOP.

    LIFO (Last In First Out)A B C D E F

    TOP

    G

    A B C D E F G

    TOP

  • 7/25/2019 Data Structures IT

    16/43

    FAP:UC-BCF 16

    Stacks

    Operations: Create(top) Create an empty stack

    Push(Stack, top, item) Inserts an element item into the stack

    Pop(Stack, top, item) Removes the top element of the stack

    and stores the value in item S_top(Stack, top) Returns the top element of the stack

    Empty(top) Determines whether the stack is empty or not.

    Stack full: Top = n-1 Stack empty: Top = -1

  • 7/25/2019 Data Structures IT

    17/43

    FAP:UC-BCF 17

    Stacks

    Representation: One-dimensional array

    Singly linked-list

    TOP

    A B C D E F

    A B

    TOP

    C D

  • 7/25/2019 Data Structures IT

    18/43

    FAP:UC-BCF 18

    Stacks

    Declaration

    #def i ne n

    t ypedef el ement t ype;

    t ypedef el ement t ype St ack[ n] ;

    Example:

    Processing of procedure calls and their terminations

  • 7/25/2019 Data Structures IT

    19/43

    FAP:UC-BCF 19

    Stacks

    Procedures for Stack Operations:

    voi d cr eat e( i nt *t op)

    {*t op = - 1; }

    voi d push( st ack S; i nt *t op; el ement t ype i t em){

    i f ( t op==n- 1)

    st ackf ul l ;

    el se {*t op++;

    S[ *t op] = i t em;

    }

    }

  • 7/25/2019 Data Structures IT

    20/43

    FAP:UC-BCF 20

    Stacks

    voi d pop( st ack S; i nt *t op; el ement t ype *i t em)

    { i f ( t op==1) st ackempt y;

    el se{ *i t em = S[ t op] ;

    * top- - ;}

    }el ement t ype s_t op( st ack S; i nt t op)

    { i f ( t op==- 1) er r or _rout i ne;

    el se r et ur n S[ t op] ;

    }

    i nt empt y( i nt t op)

    { i f ( t op == 1) r et urn 1;

    el se r et ur n 0;

    }

  • 7/25/2019 Data Structures IT

    21/43

    FAP:UC-BCF 21

    Stacks

    voi d mai n( )

    { cr eat e( &t op1) ; cr eat e( &t op2) ;s_empt y = empt y( t op1) ;

    pr i nt f ( %d, s_empt y) ;

    push( s1, &t op1, 16) ;

    s_empt y = empt y( t op1) ;

    pr i nt f ( %d, s_empt y) ;

    push( s1, &t op1, 10) ; push( s1, &t op1, 9) ;

    push( s1, &t op1, 8) ; push( s1, &t op1, 7) ;j =s_t op( s1, t op1) ;

    pr i nt f ( Top i s %d, j ) ;

    pr i nt f ( %d\ n, t op1) ;

  • 7/25/2019 Data Structures IT

    22/43

    FAP:UC-BCF 22

    Stacks

    s_empt y = empt y( t op2) ;

    pr i nt f ( %d, s_empt y) ;push( s2, &t op2, 10) ; push( s2, &t op2, 9) ;

    push( s2, &t op2, 8) ; push( s2, &t op2, 7) ;

    push( s2, &t op2, 12) ; push( s2, &t op2, 4) ;

    pop( s1, &t op1, &i t em) ;

    pop( s2, &t op2, &i t em) ;

    j =s_t op( s2, t op2) ; pr i nt f ( Top i s %d, j ) ;

    pr i nt f ( %d\ n, t op2) ;}

  • 7/25/2019 Data Structures IT

    23/43

    FAP:UC-BCF 23

    Evaluation of Expressions

    Expression is made up of operands, operators anddelimiters.

    Operands can be any legal variable names or constantsin programming languages.

    Operations are described by operators:Basic arithmetic operators: + - * /

    Unary operators: - +

    Relational operators: ==, >, =,

  • 7/25/2019 Data Structures IT

    24/43

    FAP:UC-BCF 24

    Evaluation of Expressions

    Our concern: how the expressions are evaluated

    The compiler accepts expressions and produce correct result byreworking the expressions into postfix form. Other forms include

    infix and prefix.

    Prefix :

    Postfix : Infix :

  • 7/25/2019 Data Structures IT

    25/43

    FAP:UC-BCF 25

    Evaluation of Expressions

    Expression: A + B

    Prefix : +AB

    Postfix: AB+

    Expression: (A+B*C)/D

    Prefix : /+A*BCD

    Postfix: ABC*+D/

  • 7/25/2019 Data Structures IT

    26/43

    FAP:UC-BCF 26

    Conversion from Infix to Postfix

    using Stacks

    IN-Stack Priority (ISP) The priority of the operator asan element of the stack.

    IN-Coming Priority (ICP) The priority of the operatoras current token.

    SYMBOL ISP ICP

    ) -- --^ 3 4

    *, / 2 2

    +,- 1 1

    ( 0 4

  • 7/25/2019 Data Structures IT

    27/43

    FAP:UC-BCF 27

    Conversion from Infix to Postfix

    using Stacks

    Rule:

    Operators are taken out of the stack (POP) as long astheir ISP is greater than or equal to the ICP of the newoperator.

    Note: ISP and ICP of # sign = -1

  • 7/25/2019 Data Structures IT

    28/43

    FAP:UC-BCF 28

    Conversion from Infix to Postfix

    using Stacks

    Algorithm:voi d POSTFI X( expr essi on E)

    t oken x, y;

    st ack[ 0] = # ; t op = 0;x = next t oken( E) ; / / t akes t he f i r st t oken and r emove i t

    f r om t he or i gi nal expr essi on.

    whi l e x ! = #

    { i f x i s an oper and pr i nt f ( x) ;el se i f x == ) {/ / unst ack unt i l (

    whi l e st ack[ t op] ! = (

    { pop( y) ; pr i nt f ( y) ; }

    pop( y) ; / / del et e (

    }

  • 7/25/2019 Data Structures IT

    29/43

    FAP:UC-BCF 29

    Conversion from Infix to Postfix

    using Stacks

    el se{ whi l e i sp[ st ack[ t op] ] >= i cp[ x]

    { pop( y) ; pr i nt f ( y) ; }push( x) ;

    }

    x = next t oken( E) ;

    }

    i f ( ! empt y( st ack) )

    { whi l e st ack[ t op] ! = #

    { pop( y) ;pr i nt f ( y) ;

    }

    }

  • 7/25/2019 Data Structures IT

    30/43

    FAP:UC-BCF 30

    Queues

    An ordered list which all insertions take place at oneend, called the REAR, while all deletions take place atthe other end, called the FRONT.

    FIFO (First-In-First-Out)

    Elements are processed in the same order as they werereceived. The first element inserted in the queue will bethe first one to be removed.

  • 7/25/2019 Data Structures IT

    31/43

    FAP:UC-BCF 31

    Queues

    Conventions for FRONT and REAR: Front is always 1 less than the actual front of the queue.

    Rear always points to the last element in the queue.

    Initial value: Front = Rear = -1

    Operations: Createq(Front, Rear) creates an empty queue

    Insert(Queue, Rear, Item) inserts the element item to the rear

    of the queue. Delete(Queue, Front, Rear, Item) removes the front element

    from the queue and assigns is to variable item.

    Qfront(Queue, Front, Rear) returns the front element of thequeue

  • 7/25/2019 Data Structures IT

    32/43

    FAP:UC-BCF 32

    Queues

    Qempty(Front, Rear) determines if the queue is empty or not

    Returns 1 if true

    Otherwise return 0

    Front = Rear means queue is empty.

  • 7/25/2019 Data Structures IT

    33/43

    FAP:UC-BCF 33

    Queues

    Representation

    One-dimensional Array

    Singly linked-list

    Rear

    A B C D E F G

    A B C D

    Front

    Rear Front

  • 7/25/2019 Data Structures IT

    34/43

    FAP:UC-BCF 34

    Queues

    Declaration:

    #def i ne n

    t ypedef el ement t ype;

    t ypedef el ement t ype Queue[ n] ;t ypedef i nt FR

    FR f r ont , r ear ;

    Queue Q;

  • 7/25/2019 Data Structures IT

    35/43

    FAP:UC-BCF 35

    Queues

    Example:Processing of customers transactions (i.e. cashiers, bank-related)

    Procedures for Queue Operations:

    voi d cr eat eq( FR *f r ont , FR *r ear )

    { *f r ont = *r ear = - 1 }

    voi d i nser t ( queue Q, FR *r ear , el ement t ype i t em){ i f ( r ear == n- 1) queuef ul l ;

    el se { ( *r ear ) ++;

    Q[ *r ear ] = i t em; }

    }

  • 7/25/2019 Data Structures IT

    36/43

    FAP:UC-BCF 36

    Queues

    voi d del et e( queue Q, FR *f r ont , FR *r ear , el ement t ype

    *i t em){ i f ( *f r ont == r ear ) queueempt y;

    el se { ( *f r ont ) ++;

    i t em = Q[ f r ont ] ; }

    }

    el ement t ype qf r ont ( queue Q, FR f r ont , FR r ear )

    { i f ( f r ont == r ear ) er r or r out i ne;

    el se r et ur n( Q[ f r ont + 1] )}

    i nt quempt y( )

    { i f ( f r ont == r ear ) r et ur n 1;

    el se r et ur n 0;

    }

  • 7/25/2019 Data Structures IT

    37/43

    FAP:UC-BCF 37

    Queues

    Notes:

    QUEUEFULL signal does not necessary imply thatthere are N elements in the queue.

    To solve this, move the entire queue to the left so thatthe first element is again at Q[0] and front = -1.

  • 7/25/2019 Data Structures IT

    38/43

    FAP:UC-BCF 38

    Circular Queues

    n- 1

    0

    1

    23

    . .

    . .

    4

  • 7/25/2019 Data Structures IT

    39/43

    FAP:UC-BCF 39

    Circular Queues

    voi d i nser t ( queue *Q, FR f r ont , FR *r ear , el ement t ype

    i t em){ i f ( f r ont == ( r ear +1) %n) queuef ul l ;

    el se { *r ear = ( *r ear +1) %n;

    Q[ *r ear ] = i t em; }

    }

    voi d del et e( queue Q, FR *f r ont , FR r ear , el ement t ype

    *i t em){ i f ( f r ont == r ear ) t hen cqueueempt y;

    el se { *f r ont = ( *f r ont +1) %n;

    *i t em = Q[ f r ont ] ; }

    }

  • 7/25/2019 Data Structures IT

    40/43

    FAP:UC-BCF 40

    Circular Queues

    el ement t ype cqf r ont ( queue Q, FR f r ont , FR r ear )

    { i f ( f r ont == rear ) er r or r out i ng;

    el se r et ur n( Q[ f r ont +1] %n) ;

    }

    i nt cquempt y( )

    { i f ( f r ont == r ear ) r et ur n 1;

    el se r et ur n 0;

    }

  • 7/25/2019 Data Structures IT

    41/43

    FAP:UC-BCF 41

    Exercises

    Convert the following infix expressions to postfix and

    prefix:

    1. A+B*C/ D

    2. A/ B C+D*E- A*C

    3. ( A+B) *D+E/ ( F+A*D) +C

    4. A+B*D E F G/ H J *K- L5. ! ( A&&! ( BD) ) | | ( C

  • 7/25/2019 Data Structures IT

    42/43

    FAP:UC-BCF 42

    Exercises

    Convert the following prefix expressions to infix:

    6. - +- ABC*D EFG

    7. +- ABC+D- EF

    8. | | | | &&ABC!

  • 7/25/2019 Data Structures IT

    43/43

    FAP:UC-BCF 43

    Exercises

    Convert the following infix expressions to postfix

    using stack:12. B+C ( E+F*G H) / ( K- L/ M) +N

    13. B+C/ D ( E+F*G H) +Z

    14. A+B- C*D*( E+F- G*H I J ) +L/ M+( N*O/ P+

    ( Q R S T) ) +U