c-stacks & queues

Upload: soumik-sarkar

Post on 09-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 C-Stacks & Queues

    1/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur1

    Dynamic 2D arrays,Stacks, Queues

    P. P. Chakrabarti

  • 8/8/2019 C-Stacks & Queues

    2/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur2

    2D Arrays using arrays of pointers

    #define N 20

    #define M 10

    main()

    { char word[N], *w[M];

    int i, n;

    scanf("%d",&n);

    for (i=0; i

  • 8/8/2019 C-Stacks & Queues

    3/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur3

    w

    0

    1

    2

    3

    9

    How it will look like

    \0rakludneT

    \0varuoS

    \0nahK

    \0aidnI

  • 8/8/2019 C-Stacks & Queues

    4/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur4

    2D Arrays using a pointer to a pointer

    #define N 20

    main(){

    char word[N], **w; /* **w is a pointer to a pointer array */

    int i, n;

    scanf("%d",&n);

    w = (char **) malloc (n * sizeof(char *));

    for (i=0; i

  • 8/8/2019 C-Stacks & Queues

    5/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur5

    w

    0

    1

    2

    3

    4

    How this will look like

    \0aidnI

    \0aknaLirS

    \0ailartsuA

    \0ayneK

    N \0dnalaeZwe

  • 8/8/2019 C-Stacks & Queues

    6/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur6

    2D Array of n rows and constant columns:Single pointer + memory chunk

    int (*r)[COLS], rows,

    scanf("%d",&rows);

    r = (int (*)[COLS])malloc(rows*COLS*sizeof(int));

    r

    Dynamically allocated memory

    r[0][0]

  • 8/8/2019 C-Stacks & Queues

    7/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur7

    2D Arrays of n rows and 3 columns

    #define COLS 3

    main()

    { int (*r)[COLS], rows, i,j;

    scanf("%d",&rows);

    r = (int (*)[COLS])malloc(rows*COLS*sizeof(int));

    for (i=0;i

  • 8/8/2019 C-Stacks & Queues

    8/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur8

    The Stack ADTPush

    Pop

    A Stack is a Last-in-First-Out (LIFO) list with all

    or some of the following interface functions:

    create: makes a new stack (of a given size)

    dispose: destroys a stack

    make_empty: makes the stack empty

    push: inserts an element into the stack

    pop: removes the top element from the stack

    isempty: determines if the stack has no elements

    isfull: determines if the stack is full in case of abounded sized stack

    push is like inserting at the front of the list

    pop is like deleting from the front of the list

  • 8/8/2019 C-Stacks & Queues

    9/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur9

    Postfix Evaluation

    Input format: [code, value] sequence

    Example: 12*4+3 is coded in postfix in the formofab*c+ as 0 12 0 4 1 2 0 3 1 0 2

    For evaluation, we define a stack of element-type(say integers/float, etc).

    The overall algorithm is as follows:

    (a) Read next input;

    (b) If input is end then top of stack containsresult;

    (c) If input is operand, push value into stack;

    (d) If Input is operator, pop out (remove) thetop two elements from the stack, performthe operation and push the resultant valueinto the stack;

    End2

    Operator1

    Operand0

    MeaningCode

    /3

    *2

    -1

    +0

    OperationOp-code

  • 8/8/2019 C-Stacks & Queues

    10/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur10

    Postfix Evaluation: Example 1

    (12*4)+3 == 12,4,*,3,+

    Input: 0 12 0 4 1 2 0 3 1 0 2

    End2

    Operator1

    Operand0

    MeaningCode

    /3

    *2

    -1

    +0

    OperationOp-code

    12 12

    4

    48 48

    3

    51

  • 8/8/2019 C-Stacks & Queues

    11/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur11

    Postfix Evaluation: Example 2

    2+4*5+3*2 == 2,4,5,*,+,3,2,*,+

    2 2

    4

    2

    4

    5

    2

    20

    22 22

    3

    22

    3

    2

    22

    6

    28

  • 8/8/2019 C-Stacks & Queues

    12/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur12

    fib (5)

    fib (3) fib (4)

    fib (1)

    fib (2)fib (1) fib (2)

    fib (0)

    fib (3)

    fib (1)

    fib (1) fib (2)

    fib (0)

    fib (0) fib (1)

    Fibonacci recurrence:

    fib(n) = 1 if n =0 or 1;

    = fib(n 2) + fib(n 1)otherwise;

    Recursion can beimplemented as a stack

  • 8/8/2019 C-Stacks & Queues

    13/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur13

    Fibonacci recursion stack

    5 4

    3

    4

    21

    4

    2

    4

    10

    4

    1

    4 3

    2

    3

    10

    0 0 0 1 1 2 3 3 3

    31

    3 21

    2 10

    1

    4 5 5 6 6 7 8

  • 8/8/2019 C-Stacks & Queues

    14/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur14

    Tower of Hanoi

    A B C

  • 8/8/2019 C-Stacks & Queues

    15/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur15

    Tower of Hanoi

    A B C

  • 8/8/2019 C-Stacks & Queues

    16/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur16

    Tower of Hanoi

    A B C

  • 8/8/2019 C-Stacks & Queues

    17/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur17

    Tower of Hanoi

    A B C

  • 8/8/2019 C-Stacks & Queues

    18/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur18

    Towers of Hanoi function

    void towers (int n , char from, char to, char aux){/ * Base Condition */

    if (n==1) {printf (Disk 1 : % c -> % c \ n, from, to) ;return ;

    }/ * Recursive Condition */

    tow ers (n-1, from, aux, to) ;

    printf (Disk % d : % c -> % c\ n, n, from, to) ;tow ers (n-1, aux , to, from) ;

    }

  • 8/8/2019 C-Stacks & Queues

    19/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur19

    TOH recursion stack

    3,A,B,C 2,C,B,A

    A to B

    2,A,C,B

    1,A,B,C

    2,C,B,A

    A to B

    1,B,C,A

    A to CA to B

    2,C,B,A

    A to B

    1,B,C,A

    A to C

    2,C,B,A

    A to B

    1,B,C,A

    A to C

    2,C,B,A

    A to B

    1,B,C,A

    2,C,B,A

    A to B

    B to C

    2,C,B,A

    A to B

    2,C,B,A 1,A,B,C

    C to B

    1,C,A,B

  • 8/8/2019 C-Stacks & Queues

    20/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur20

    The Queue ADTEnqueue

    Dequeue

    A Stack is a First-in-First-Out (FIFO) list with thefollowing interface functions:

    create: makes a new queue of a given size in case

    of a bounded queue

    dispose: destroys a queue make_empty: makes queue empty

    enqueue: inserts an element at the rear

    dequeue: removes the element in front

    isempty: determines if the queue has no elements isfull: determines if the queue is full in case of a

    bounded sized stack

    REAR

    FRONT

  • 8/8/2019 C-Stacks & Queues

    21/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur21

    Possible Implementations

    Linear Arrays:(static/dynamicaly allocated)

    front rear

    Circular Arrays:(static/dynamicaly allocated)

    Can be implemented by a one-D

    array using modulus operations.

    frontrear

    Linked Lists: Use a linear

    linked list with insert_rear

    and delete_front operations

  • 8/8/2019 C-Stacks & Queues

    22/22

    27-03-03 P.P.Chakrabarti, IIT Kharagpur22

    Exercises Implement the Queue as an array. (Read

    the concept of a circular queue). Writedown the data definition and all theinterface functions.

    Implement the Queue as a linked list. Implement a Priority Queue which

    maintains the items in an order (ascending/

    descending) and has additional functionslike remove_max and remove_min.

    Maintain a Doctors appointment list