c6 stack queue

Download C6 stack queue

Post on 08-Apr-2017

186 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1

    Chng 5:

    NGN XP HNG I

    (Stack - Queue)

    1

    Chng 5: Ngn xp Hng i

    Ni dung

    Ngn xp

    Hng i

    2

    Ngn xp (Stack)

    Khi nim Stack

    Cc thao tc trn Stack

    Hin thc Stack

    ng dng ca Stack

    Hng i

    Chng 5: Ngn xp Hng i

    Stack - Khi nim

    Stack l mt danh sch m cc i tng c thm vo v

    ly ra ch mt u ca danh sch (A stack is simply a list of

    elements with insertions and deletions permitted at one end)

    V th, vic thm mt i tng vo Stack hoc ly mt i

    tng ra khi Stack c thc hin theo c ch LIFO (Last In

    First Out - Vo sau ra trc)

    Cc i tng c th c thm vo Stack bt k lc no

    nhng ch c i tng thm vo sau cng mi c php

    ly ra khi Stack

    3

  • 2

    Chng 5: Ngn xp Hng i

    Stack Cc thao tc

    Stack h tr 2 thao tc chnh:

    Push: Thao tc thm 1 i tng vo Stack

    Pop: Thao tc ly 1 i tng ra khi Stack

    V d:

    5 3 2 - - 4

    4

    Chng 5: Ngn xp Hng i

    Stack Cc thao tc

    5

    Stack cng h tr mt s thao tc khc:

    isEmpty(): Kim tra xem Stack c rng khng

    Top(): Tr v gi tr ca phn t nm u Stack m khng

    hy n khi Stack. Nu Stack rng th li s xy ra

    Chng 5: Ngn xp Hng i

    Stack Hin thc Stack (Implementation of a Stack)

    6

    Mng 1 chiu Danh sch LK

    Kch thc stack

    khi qu thiu, lc

    qu tha

    Cp pht

    ng!

    Push / Pop hi

    phc tp

    Push/Pop

    kh d

    dng

  • 3

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    C th to mt Stack bng cch khai bo mt mng 1 chiu

    vi kch thc ti a l N (v d: N =1000)

    Stack c th cha ti a N phn t nh s t 0 n N-1

    Phn t nm nh Stack s c ch s l top (lc trong

    Stack ang cha top+1 phn t)

    Nh vy, khai bo mt Stack, ta cn mt mng 1 chiu list,

    v 1 bin s nguyn top cho bit ch s ca nh Stack:

    struct Stack {

    DataType list[N];

    int top;

    };

    7

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Lnh top = 0 s to ra mt Stack S rng

    Gi tr ca top s cho bit s phn t hin hnh c trong Stack

    Khi ci t bng mng 1 chiu, Stack b gii hn kch thc

    nn cn xy dng thm mt thao tc ph cho Stack:

    isFull(): Kim tra xem Stack c y cha, v khi Stack y,

    vic gi n hm Push() s pht sinh ra li

    8

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    9

    Khi to Stack:

    void Init (Stack &s)

    {

    s.top = 0;

    }

  • 4

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Kim tra Stack rng hay khng: 10

    int isEmpty(Stack s)

    {

    if (s.top==0)

    return 1; // stack rng

    else

    return 0;

    }

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Kim tra Stack y hay khng:

    11

    int isFull(Stack s)

    {

    if (s.top>=N)

    return 1;

    else

    return 0;

    }

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Thm mt phn t x vo Stack

    12

    void Push (Stack &s, DataType x)

    {

    if (!isFull(s)) // stack cha y

    {

    s.list[s.top]=x;

    s.top++;

    }

    }

  • 5

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Trch thng tin v hu phn t nh Stack

    13

    DataType Pop(Stack &s)

    {

    DataType x;

    if (!Empty(s)) // stack khc rng

    {

    x = s.list[s.top];

    s.top--;

    }

    return x;

    }

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)

    Nhn xt:

    Cc thao tc trn u lm vic vi chi ph O(1)

    Vic ci t Stack thng qua mng mt chiu n gin v

    kh hiu qu

    Tuy nhin, hn ch ln nht ca phng n ci t ny l

    gii hn v kch thc ca Stack (N)

    Gi tr ca N c th qu nh so vi nhu cu thc t hoc qu ln s

    lm lng ph b nh

    14

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK (Implementation of a Stack using Linked List)

    C th to mt Stack bng cch s dng mt danh sch lin

    kt n (DSLK)

    Khai bo cc cu trc:

    15

    struct Node

    {

    DataType data;

    Node *pNext;

    };

    struct Stack

    {

    Node *top;

    };

  • 6

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK (Implementation of a Stack using Linked List)

    Khi to Stack:

    16

    void Init(Stack &t)

    {

    t.top = NULL;

    }

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK (Implementation of a Stack using Linked List)

    Kim tra xem Stack c rng khng:

    17

    int isEmpty (Stack t)

    {

    return t.top == NULL ? 1 : 0;

    }

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK (Implementation of a Stack using Linked List)

    Thm mt phn t x vo Stack:

    18

    void Push (Stack &t, DataType x)

    {

    Node *p = new Node;

    if (p==NULL) { coutpNext= NULL;

    if (t.top==NULL) // if (Empty(l))

    t.top = p;

    else{

    p->pNext = t.top;

    t.top = p;

    }

    }

    Thm phn t vo u danh sch

  • 7

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK (Implementation of a Stack Using Linked List)

    Trch thng tin v hy phn t nh Stack:

    19

    DataType Pop (Stack &t)

    {

    if (t.top==NULL){

    coutpNext;

    x = p->data;

    delete p;

    return x;

    }

    Ly v xa phn t u danh sch

    Chng 5: Ngn xp Hng i

    Stack - ng dng

    Stack thch hp lu tr cc loi d liu m trnh t truy xut

    ngc vi trnh t lu tr

    Mt s ng dng ca Stack:

    Trong trnh bin dch (thng dch), khi thc hin cc th tc,

    Stack c s dng lu mi trng ca cc th tc

    Lu d liu khi gii mt s bi ton ca l thuyt th (nh tm

    ng i)

    Kh qui

    ng dng trong cc bi ton tnh ton biu thc

    20

    Chng 5: Ngn xp Hng i

    Stack - ng dng

    V d: th tc Quick_Sort dng Stack kh qui:

    Bc 1. l=1; r=n;

    Bc 2. Chn phn t gia x=a[(l+r) / 2]

    Bc 3. Phn hoch (l, r) thnh (l1, r1) v (l2, r2) bng cch xt:

    y thuc (l1, r1) nu yx

    y thuc (l2, r2) ngc li

    Bc 4. Nu phn hoch (l2, r2) c nhiu hn 1 phn t th thc hin:

    Ct (l2, r2) vo Stack

    Nu (l1, r1) c nhiu hn 1 phn t th thc hin:

    l = l1

    r = r1

    Quay ln bc 2

    Ngc li

    Ly (l, r) ra khi Stack, nu Stack khc rng th quay ln bc 2, ngc li

    th dng

    21

  • 8

    Chng 5: Ngn xp Hng i

    22

    Stack - ng dng

    57 2

    1 28 2

    0 14 2

    0 7 2

    1 3 2

    1 1 2

    1 0

    57 = 1110012

    V d: 57 = ???2

    Bi tp: i s t c s 10 sang c s x

    Chng 5: Ngn xp Hng i

    void main()

    {

    Stack s;

    int coso, so, sodu;

    Init(s);

    // Nhp s cn chuyn vo so

    // Nhp c s cn chuyn vo coso

    while (so != 0)

    {

    sodu = so % coso;

    Push (s, sodu); // push so du vao stack

    so = so/coso;

    }

    cout

  • 9

    Chng 5: Ngn xp Hng i

    RPN

    25

    Infix : ton t vit gia ton hng

    Postfix (RPN): ton t vit sau ton hng

    Prefix : ton t vit trc ton hng

    Examples:

    INFIX RPN (POSTFIX) PREFIX

    A + B

    A * B + C

    A * (B + C)

    A - (B - (C - D))

    A - B - C - D

    A B + + A B

    A B * C +

    A B C + *

    A B C D - - -

    A B - C - D -

    + * A B C

    * A + B C

    - A - B - C D

    - - - A B C D

    Lng gi biu thc RPN

    26

    K thut gch di:

    1. Duyt t tri sang phi ca biu thc cho n khi gp ton t.

    2. Gch di 2 ton hng ngay trc ton t v kt hp chng

    bng ton t trn

    3. Lp i lp li cho n ht biu thc.

    V d 2*((3+4)-(5-6))

    2 3 4 + 5 6 - - *

    2 8 * 2 8 * 16

    2 7 5 6 - - *

    2 7 5 6 - - * 2 7 -1 - *

    2 7 -1 - *

    2 3 4 + 5 6 - - *

    Chng 5: Ngn xp Hng i

    Thut ton tnh gi tr

    1. Khi to Stack rng (cha hng hoc bin).

    2. Lp cho n khi kt thc biu thc:

    c 01 phn t ca biu thc (hng, bin, php ton).

    Nu phn t l hng hay bin: a vo Stack.

    Ngc li:

    Ly ra 02 phn t ca Stack.

    p dng php ton cho 02 phn t va ly ra.

    a kt qu vo Stack.

    3. Gi tr ca biu thc chnh l phn t cui cng

    ca Stack.

    27

  • 10

    2*((3+4)-(5-6))

    28

    Example: 2 3 4 + 5 6 - - *

    Push 2

    Push 3

    Push 4

    Read +

    Pop 4, Pop 3,

    Push 7

    Push 5

    Push 6

    Read -

    Pop 6, Pop 5,

    Push -1

    Read -

    Pop -1, Pop 7,

    Push 8

    Read *

    Pop 8, Pop 2,

    Push 16

    2

    3

    4

    2

    7

    5

    6

    2

    7

    -1

    2

    8

    16

    3 + 4 = 7

    5 - 6 = -1

    7 - -1 = 8

    2 * 8 = 16

    Chng 5: Ngn xp Hng i

    Chuyn infix thnh postfix

    1. Khi to Stack rng (cha cc