c5 stack queue

Download C5 Stack Queue

Post on 08-Sep-2015

5 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Stack, Queue

TRANSCRIPT

  • Chng 5: NGN XP HNG I(Stack - Queue)*

    Chng 5: Ngn xp Hng i

    Ni dungNgn xpHng i*Ngn xp (Stack)Khi nim StackCc thao tc trn StackHin thc Stackng dng ca StackHng i

    Chng 5: Ngn xp Hng i

    Stack - Khi nimStack 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*

    Chng 5: Ngn xp Hng i

    Stack Cc thao tcStack h tr 2 thao tc chnh:Push: Thao tc thm 1 i tng vo StackPop: Thao tc ly 1 i tng ra khi StackV d:5 3 2 - - 4*

    Chng 5: Ngn xp Hng i

    Stack Cc thao tc*Stack cng h tr mt s thao tc khc:isEmpty(): Kim tra xem Stack c rng khngTop(): 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)*Mng 1 chiuDanh sch LKKch thc stack khi qu thiu, lc qu thaCp pht ng!Push / Pop hi phc tpPush/Pop kh d dng

    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-1Phn 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;};*

    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 rngGi tr ca top s cho bit s phn t hin hnh c trong StackKhi 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*

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)*Khi to Stack:void Init (Stack &s){s.top = 0;}

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)Kim tra Stack rng hay khng:*int isEmpty(Stack s){if (s.top==0)return 1; // stack rngelsereturn 0;}

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng mng (Implementation of a Stack using Array)Kim tra Stack y hay khng:*int isFull(Stack s){if (s.top>=N)return 1;elsereturn 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*void Push (Stack &s, DataType x){if (!isFull(s)) // stack cha y {s.list[s.top]=x;s.top++;}}

    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*DataType Pop(Stack &s){ DataType x;if (!Empty(s)) // stack khc rng{s.top--;x = s.list[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 quTuy 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*

    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:

    *struct Node{DataType data;Node *pNext;};struct Stack{Node *top;};

    Chng 5: Ngn xp Hng i

    Hin thc Stack dng DSLK(Implementation of a Stack using Linked List)Khi to Stack:*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: *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:

    *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

    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:*DataType Pop (Stack &t){if (t.top==NULL){coutpNext = NULL;x = p->data;delete p;return x;}Ly v xa phn t u danh sch

    Chng 5: Ngn xp Hng i

    Stack - ng dngStack thch hp lu tr cc loi d liu m trnh t truy xut ngc vi trnh t lu trMt 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 tcLu 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*

    Chng 5: Ngn xp Hng i

    Stack - ng dngV 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 yxy thuc (l2, r2) ngc liBc 4.Nu phn hoch (l2, r2) c nhiu hn 1 phn t th thc hin: Ct (l2, r2) vo StackNu (l1, r1) c nhiu hn 1 phn t th thc hin: l = l1r = r1Quay ln bc 2Ngc liLy (l, r) ra khi Stack, nu Stack khc rng th quay ln bc 2, ngc li th dng*

    Chng 5: Ngn xp Hng i

    *Stack - ng dng572 1 28 2 0 14 2 0 7 2 1 3 2 1 1 2 1 0 57 = 1110012V d: 57 = ???2Bi 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

  • Lng gi biu thc RPN*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 trn3. 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 * 162 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 tr1. 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 cngca Stack.*

  • 2*((3+4)-(5-6))*Example: 2 3 4 + 5 6 - - *Push 2Push 3Push 4Read + Pop 4, Pop 3, Push 7Push 5Push 6Read -Pop 6, Pop 5, Push -1Read - Pop -1, Pop 7, Push 8Read *Pop 8, Pop 2, Push 163 + 4 = 75 - 6 = -17 - -1 = 82 * 8 = 16

    Chng 5: Ngn xp Hng i

    Chuyn infix thnh postfix1. Khi to Stack rng (cha cc php ton).2. Lp cho n khi kt thc biu thc:c 01 phn t ca biu thc (01 phn t c th l hng, bin,php ton, ) hay ( ).Nu phn t l:2.1 (: a vo Stack.2.2): ly cc phn t ca Stack ra cho n khi gp ( trong Stack.

    *

    Chng 5: Ngn xp Hng i

    Chuyn infix thnh postfix2.3 Mt php ton: + - * /Nu Stack rng: a vo Stack.Nu Stack khc rng v php ton c u tin cao hn phn t u Stack: a vo Stack.Nu Stack khc rng v php ton c u tin thp hn hocbng phn t u Stack: - ly phn t t Stack ra; - sau lp li vic so snh vi phn t u Stack.*

    Chng 5: Ngn xp Hng i

    Chuyn infix thnh postfix

    2.4 Hng hoc bin: a vo kt qu.3. Ly ht tt c cc phn t ca Stack ra.

    *

    Chng 5: Ngn xp Hng i

    u tin+ , _1*, /2^3

    *

  • *Example:Push (OutputDisplay APush +Display BPush *Display CRead ) Pop *, Display *, Pop +, Display +, Pop ( Push /Push (Display D Push -Push (Display EPush -Display FRead )Pop -, Display -, Pop (Read ) Pop -, Display -, Pop (Pop /, Display / (A+B*C)/(D-(E-F))AABCABABC*ABC*+ABC*+DABC*+DEABC*+DEFABC*+DEF-ABC*+DEF--(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))ABC*+DEF--/(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))(A+B*C)/(D-(E-F))

    Chng 5: Ngn xp Hng i

    V d A + (B*C - (D/E^F) * G) * H

    S=[];

    KQ=*

    Chng 5: Ngn xp Hng i

    V d A + (B*C - (D/E^F) * G) * H

    *S=[];KQ=

    S=[+];KQ=A

    A + (B*C - (D/E^F) * G) * H A + (B*C - (D/E^F) * G) * H

    Chng 5: Ngn xp Hng i

    V d*S=[+];KQ=A

    A + (B*C - (D/E^F) * G) * H A + (B*C - (D/E^F) * G) * H A + (B*C - (D/E^F) * G) * H A + (B*C - (D/E^F) * G) * H A + (B*C - (D/E^F) * G) * HS=[+(];S=[+(*];KQ=AB

    KQ=ABC

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HS=[+(*];KQ=ABC

    S=[+(];KQ=ABC*

    S=[+(-];

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*

    S=[+(-];S=[+(-(];

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*

    S=[+(-(];KQ=ABC*D

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*D

    S=[+(-(];S=[+(-(/];

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*D

    S=[+(-(/];KQ=ABC*DE

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*DE

    S=[+(-(/];S=[+(-(/^];

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*DE

    S=[+(-(/^];KQ=ABC*DEF

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HKQ=ABC*DEF

    S=[+(-(/^];S=[+(-(/];KQ=ABC*DEF^/

    S=[+(-(];S=[+(-];KQ=ABC*DEF^

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HS=[+(-];KQ=ABC*DEF^/

    S=[+(-*];

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HS=[+(-*];KQ=ABC*DEF^/

    KQ=ABC*DEF^/G

    Chng 5: Ngn xp Hng i

    V d* A + (B*C - (D/E^F) * G) * HS=[+(-*];KQ=ABC*DEF^/G

    S=[+(-];KQ=ABC*DEF^/G*

    KQ=ABC*DEF^/G*-

    S=[+(];S=[+];

    Chng 5: Ngn