Slides Stacks

Embed Size (px)

Citation preview

  • 8/12/2019 Slides Stacks

    1/54

  • 8/12/2019 Slides Stacks

    2/54

    Reading Assignment

    Secion 6.13: Array of Pointers

    From the book A Book on C

  • 8/12/2019 Slides Stacks

    3/54

  • 8/12/2019 Slides Stacks

    4/54

    Stack data structure.

    .

    .

    In theory, stack is open-ended.

    TOP: location where the topmost element

    is stored in stackInitially TOP has some special value

    say, TOP = EMPTY

    (EMPTY = -1)

    IS_EMPTY(): true iff TOP== EMPTY

    TOP

  • 8/12/2019 Slides Stacks

    5/54

    Stack data structure

    9

    6

    5

    .

    .

    .

    PUSH(5);

    PUSH(6);

    PUSH(9)

    TOP

  • 8/12/2019 Slides Stacks

    6/54

    Stack data structure

    6

    5

    .

    .

    .

    PUSH(5);

    PUSH(6);

    PUSH(9);

    POP();

    IS_ENPTY FALSETOP

  • 8/12/2019 Slides Stacks

    7/54

  • 8/12/2019 Slides Stacks

    8/54

  • 8/12/2019 Slides Stacks

    9/54

    Some implementations

    6

    5

    TOP: location where the next element will be

    stored in stack

    So, initially TOP=0;

    PUSH: push one element to stack

    if stack is not full

    POP: take out one element in stack

    provided stack is non-empty

    IS_EMPTY(): true iff stack is empty

    IS_FULL(): true iff stack is full

    TOP

  • 8/12/2019 Slides Stacks

    10/54

  • 8/12/2019 Slides Stacks

    11/54

    Arrays

    Benefits

    Faster access (constant time access)

    Easy to to declare and use

    All languages provide array data structures

    Faster searching (binary search) and sorting

    Problems

    May not be easy to declare very large arrays Size is fixed, can not be expanded

    Insertion/ deletion (in between) is costly

  • 8/12/2019 Slides Stacks

    12/54

    Implementation using Arrays

    int stack[100];

    int TOP,BOTTOM; // global variables

    void initialize_stack(){

    TOP = 0;

    }int push(int elem){

    if (!stackfull()){stack[TOP] = elem;

    TOP++;

    return(1);}

    return(-1)

    }

  • 8/12/2019 Slides Stacks

    13/54

    Implementation using Arrays

    int stack[100];int TOP,BOTTOM; // global variables

    void initialize_stack(){

    TOP = 0;

    }int pop( ){

    if (!stackempty()){

    TOP --;

    return(stack[TOP]);}

    return(-1)

    }

  • 8/12/2019 Slides Stacks

    14/54

    Implementation using Arrays

    int stack[100];

    int TOP,BOTTOM; // global variables

    void initialize_stack(){

    TOP = 0;

    }

    int stackempty( ){

    return (TOP == 0);

    }

    int stackfull(){

    return(TOP == 100);

    }

  • 8/12/2019 Slides Stacks

    15/54

    Prefix notation

    a + ( b - c) * d // infix notation

    ( a + ( ( bc )) * d ) )

    + a * - b c d // prefix notation

  • 8/12/2019 Slides Stacks

    16/54

    Postfix notation

    a + ( b - c) * d // infix notation

    ( a + ( ( bc )) * d ) )

    a b cd * + // postfix notation

  • 8/12/2019 Slides Stacks

    17/54

    Expression evaluation using a stack

    a b cd * + // let a = 5, b = 6, c = 7, d =8

    Read the expression from LR

    push if you see a variable

    when you see an operator, pop

    arguments, compute and push

  • 8/12/2019 Slides Stacks

    18/54

  • 8/12/2019 Slides Stacks

    19/54

  • 8/12/2019 Slides Stacks

    20/54

    Expression evaluation using a stack

    a b cd * + // let a = 5, b = 6, c = 7, d =8

    when you see an operator, pop

    arguments, compute and push

    push (a)

    push (b)

    push (c) // now see

    // pop two arguments, compute, and push

    -1

    a

  • 8/12/2019 Slides Stacks

    21/54

    Expression evaluation using a stack

    a b cd * + // let a = 5, b = 6, c = 7, d =8

    push d // see *

    compute (-1) * 8 and push to stack

    d

    -1

    a

  • 8/12/2019 Slides Stacks

    22/54

    Expression evaluation using a stack

    a b cd * + // let a = 5, b = 6, c = 7, d =8

    see +

    compute a + (-8)

    -8

    a

  • 8/12/2019 Slides Stacks

    23/54

    Expression evaluation using a stack

    a b cd * + // let a = 5, b = 6, c = 7, d =8

    -3

  • 8/12/2019 Slides Stacks

    24/54

    uses of a stack

    To show that a program statement is correct

    (recognizing program statements)

    When the program runs, there is a runtime stack to store

    function locals and arguments

    Expression evaluator

  • 8/12/2019 Slides Stacks

    25/54

    Algorithm: binary_search

    Input:

    An arrayA[0..max]of integers; elements are

    already sorted and in ascending order.

    A number num

    Output:

    Array index i, if num is present inA[0..max]

    -1if num is not in the array

  • 8/12/2019 Slides Stacks

    26/54

  • 8/12/2019 Slides Stacks

    27/54

    Algorithm: binary_search

    Step 0: if (max = 0) output(-1); goto Step 7Step 1: begin := 0; end := max;

    Step 2: if (begin > end) output (-1); goto Step 7

    Step 3: mid := (begin + end)/2

    Step 4: if A[mid] = num output (mid); go to Step 7

    Step 5: if (num > A[mid])begin := mid + 1;

    go to Step 2

    endif

    Step 6:if (num < A[mid])end := mid1

    go to step 2

    endif

    Step 7: STOP

  • 8/12/2019 Slides Stacks

    28/54

    Structures

  • 8/12/2019 Slides Stacks

    29/54

    Complex numbers.

    c1 = a + jb

    where j = square_root(-1);

    a is called the real part, b is called the imaginary part.

    c2 = c + jd

    c1 + c2 = (a+c) + j(b +d)

    c1c2 = (ac) + j(bd)

    c1*c2 = acbd + j(ad +bc)

  • 8/12/2019 Slides Stacks

    30/54

    Complex numbers.

    c1 = a + jb

    otherwise written as ( a , b )

    real part imaginary part

  • 8/12/2019 Slides Stacks

    31/54

    Implementing complex-numbers

    int c_real, c_imag, d_real, d_imag, x_real,x_imag;

    c_real = 10;

    c_imag = 20;

    d_real = 30;

    d_imag = 40

    x_real = c_real + d_real.

    .

  • 8/12/2019 Slides Stacks

    32/54

  • 8/12/2019 Slides Stacks

    33/54

    Simarly

    A persons Name

    Address

    Telephone number

    Occupation Or a students

    Name,

    Roll no

    Discipline Year of study

    Grades

  • 8/12/2019 Slides Stacks

    34/54

    . This is where come Structures

  • 8/12/2019 Slides Stacks

    35/54

    struct complex

    {

    int real;

    int imag;} ;

    struct complex c,d,r;

    c.real = 10; d.real = 30;

    c.imag = 20; d.imag = 40;

    e.real = c.real + d.real;e.imag = c.imag + d.imag;

  • 8/12/2019 Slides Stacks

    36/54

    struct complex

    {

    int real;

    int imag;} ;

    struct complex carray[40];

    carray[5].real = 10;

    carray[5].imag = 20;

  • 8/12/2019 Slides Stacks

    37/54

    struct complex

    {

    int real;

    int imag;

    } ;

    struct complex *a; // pointer to a structure

    struct complex b;

    a = &b;

    (*a).real = 10;

    (*a).imag = 20;

    // bracketing required; .Has higher precedence than *

  • 8/12/2019 Slides Stacks

    38/54

  • 8/12/2019 Slides Stacks

    39/54

    struct complex *a;

    struct complex b;

    xxxx

    b

    a

  • 8/12/2019 Slides Stacks

    40/54

  • 8/12/2019 Slides Stacks

    41/54

    struct complex *a;

    struct complex b;

    a = &b;

    a -> real = 10;10

    xx

    b

    a

  • 8/12/2019 Slides Stacks

    42/54

    struct complex *a;

    struct complex b;

    a = &b;

    a -> real = 10;

    a -> imag = 20;10

    20

    b

    a

  • 8/12/2019 Slides Stacks

    43/54

    Structure in heap

    struct complex *a;

    a = (struct complex *)

    calloc(1, sizeof(struct complex);

    a

  • 8/12/2019 Slides Stacks

    44/54

    Structure in heap

    struct complex *a;

    a = (struct complex *)

    calloc(1, sizeof(struct complex);

    a-> real = 10;

    a->imag = 20;

    a

  • 8/12/2019 Slides Stacks

    45/54

    Linked Lists

    struct list

    {

    int data;

    struct list *next;

    }

    data list

  • 8/12/2019 Slides Stacks

    46/54

    Linked Lists

    struct list

    {

    int data;

    struct list *next;

    }

    struct list a, b; data list

  • 8/12/2019 Slides Stacks

    47/54

    Linked Lists

    struct list{

    int data;

    struct list *next;

    }

    struct list a, b;

    a.data = 10;a.next = &b;

    b.data = 20;

    10 list

    20 list

  • 8/12/2019 Slides Stacks

    48/54

    Linked Lists

    struct list

    {

    int data;

    struct list *next;

    }

    struct list a, b;

    a.data = 10;

    a.next = &b;

    b.data = 20;

    b.next = NULL

    10 next

    20 next

  • 8/12/2019 Slides Stacks

    49/54

    Linked Lists

    struct list

    {

    int data;

    struct list *next;

    }

    struct list a, b;

    a.data = 10;

    a.next = &b;

    b.data = 20;

    b.next = NULL

    10

    20 0

  • 8/12/2019 Slides Stacks

    50/54

    struct list {

    int data;

    struct list *next;}

    struct list *a, *b, *c;

    a = (struct list *) malloc(sizeof (struct list));

    b = (struct list *) malloc(sizeof (struct list));c = (struct list *) malloc(sizeof (struct list));

  • 8/12/2019 Slides Stacks

    51/54

  • 8/12/2019 Slides Stacks

    52/54

    adata = 10;

    anext = b;

    anextdata = 20;

    anextnext = c;

    anextnextdata = 30;

    anextnextnext = NULL;

    10 20 30a b c

  • 8/12/2019 Slides Stacks

    53/54

    struct list *create_node(){

    struct list *nn;nn = (struct list *) malloc(sizeof(struct list));

    return(nn);

    }

  • 8/12/2019 Slides Stacks

    54/54

    struct list *make_list(){

    struct list *p,*q;

    int dd;

    q = create_node(); qnext = NULL:

    scanf(%d,&dd);

    while (dd != 9999){

    p = create_node();

    pnext = q;

    pdata = dd;

    q = p;

    scanf(%d,&dd);

    }

    return(q);

    }