第三章 堆疊與佇列的基本應用

54
written by Wei-ShenLai 1 第第第 第第第第第第第第第第 3-1 第第第第 (Stack) 3-2 第第第第第第 3-3 第第 (queue) 第第第 3-4 第第第第第第第第第第第第第 3-5 第第第第第第第第第第第第第 3-6 第第第第第第第第第第第第第

Upload: abraham-saunders

Post on 31-Dec-2015

83 views

Category:

Documents


0 download

DESCRIPTION

第三章 堆疊與佇列的基本應用. 3-1 簡介堆疊 (Stack) 3-2 迷宮問題研究 3-3 佇列 (queue) 的介紹 3-4 算術運算式表示法的求值計算 3-5 中序表示法轉換為前序表示法 3-6 前序式與後序式轉換成中序式. 3-1  簡介堆疊 (Stack). 堆疊 ( S t a c k ) 是一群相同資料型態的組合,所有的動作均在堆疊頂端進行,具 「後進先出」 ( L a s t In , F i r st Out : LIFO) 的特性。在電腦中的應用相當廣泛,例如遞迴呼叫、副程式的呼叫。 - PowerPoint PPT Presentation

TRANSCRIPT

  • 3-1 (Stack)3-2 3-3 (queue)3-4 3-5 3-6

  • 3-1(Stack)( S t a c k ) ( L a s t In F i r st Out LIFO)

  • 3-1-1(Abstract Data TypeADT)(LIFO,Last In First Out)( op e r a t i o n )CREATEPUSH POP EMPTYtruefalseFULLtruefalse

  • 3-1-2(ADT)(linked list)

  • 3-1-2S ( 1 : n ) S ( 1 ) S ( i ) i t o p CREATECREATE(S)declare S(1:n)top 0ENDTOPTOP(S)if top=0 then "empty"else S(top)ENDPUSH PUSH(itme,S,top,n)if top n then call Stack-Full(S)top top+1S(top) itemEND/ / i t em n / /

  • 3-1-2POP POP(item,S)if top=0 then call Stack-empty(S)item S(top)top top-1EndEMPTYStack-empty(S)if top=0 then trueelse falseEndFULLStack-Full(S)if top=n then trueelse falseEnd

  • Ch03_01.c

    card[0]card[51]card[i]=i1

    C0top-1

  • Ch03_01.c

    30

    1/26

  • Ch03_01.c

  • Ch03_01.c

    top+1top

    top>=MAX-1(51)

  • 3-1-2 (inorder)(preorder)(CPU)(interrupt handling)( D F S ) ( s t ack computer )( p o p ) ( p u s h ) ( ) (Compiler Syntax Processing)

  • 3-1-23 . 1 . 1 AABBAC()

  • 3-1-23 . 1 . 2 1,2,3,...,nn=3 1 2 3 3,2,1 (1)n=3(2)n=6325641154236154623n=5 32154 (3)Snn()

  • 3-1-2(1) n=3123,132,213,231,321312(2) 325641154263 154623 n=5 32154 1542635423 154623 5423(3)

  • 3-2MAZE[row][col ]MAZE[i][j] =1 [i][j] =0 [i][j]MAZE[1][1]MAZE[m][n]1 0 x 1 2

  • 3-2MAZE[x][y]

    struct list{int x,y;struct list* next;}2 p u s hp o p

  • 3-2

    listxynext

    listxynext

    listxynext

    NULL

    path

    typedefnodelistlistnode

  • 3-2

    po

  • 3-2

    stack

    listxynext

    listxynext

    listxynext

    NULL

    stack

    listxynext

    malloc

    listxynext

    listxynext

    listxynext

    NULL

    stack

    stack

    free

  • 3-3(queue) ( ADT) (rear)(front)First In, First Out()

  • 3-3-1( F I F O ) f r o n t r e a r Q( 1 : n ) ( Q(0: n - 1 ) 0 Q f r on t 0 f r on t -1r ea r 0 r e a r - 1 )

  • 3-3(queue) Q CREATE(Q)declare Q(1:n)front 0;rear 0end Q ADDQ(item,Q,n,rear)if rear=n then call Queue-Full(Q);rear rear+1Q(rear) itemend DeleteQ(item,Q,front,rear)if front=rear then call Queue-Empty(Q);front front+1item Q(front)end

  • 3-3(queue) Front(Q)if front=rear then "empty"else Q(front+1);end queue-Empty(Q)if front=rear then trueelse falseend

  • Ch03_03.c#include #include #include #define MAX 20 /* */void main(){int front,rear,val,queue[MAX]={0}; char ch;front=rear=-1;while(rearfront){ front++; printf("\n[]: [%d]\n",queue[front]); queue[front]=0; }else{ printf("\n[]\n");exit(0);}break;default:printf("\n");break;}}if(rear==MAX-1) printf("[]\n");printf("\n[]:");if (front>=rear){printf(" \n");printf("[]\n");}else{while (rear>front){front++; printf("[%d]\t",queue[front]);}printf("\n");}}

  • 3-3(queue) f r o n t , r e a r r e a r=n

    Circular queue

  • 3-3-2(circular queue) Q(0:n-1)Q(0)Q(n-1)frontrearfrontrear-1front=rearrear (rear+1) mod nfront (front+1) mod nrear+1=frontn-1tag=0tag=1n

  • 3-3-2(circular queue) CREATE(Q)//Q declare Q(0:n-1)front 0;rear 0;endADDQ(item,Q,n,rear)//Q rear (rear+1) mod nif rear=front then call Queue-Full(Q);Q(rear) itemendDeleteQ(item,Q,front,rear)//if front=rear then call Queue-Empty(Q);front (front+1) mod nitem Q(front)end

  • 3-3-2(circular queue) Front(Q)//if front=rear then "empty"else Q(front+1);endqueue-Empty(Q)//if front=rear then trueelse falseendqueue-Full(Q)//if front=(rear+1) mod n then trueelse falseend

  • 3-3-3(priority queue) FIFO( ) (priority)(Highest Priority Out First: HPOF)( SARS ) C P U

  • 3-3-4(double-ended queue:deque)(Deque)

    2

    front2

    rear2

    2

    front1

    1

    3

    4

    6

    7

    5

    rear1

  • 3 . 3 . 3 (deque)1,2,3,4,5,6,75174236()5[1]74[2][3][6][5]1[7]4236517[4]23[6]

    51723465[1]7[2][3][4]6[5]1[7]2346

    1

    2

    3

    4

    6

    7

    5

    5174236

  • 3-3-5(BFS)(simulation)(event)CPU(Job Scheduling) (buffer)( )

  • 3-3-53.3.6(multi stack)(multiqueue)()(1) S(1:n)mB(i)iT(i)iT(i)=B(i)T(i)=B(i)=int[n/m]*(i-1) 1 i m

    (push)(pop)Procedure push(i,x) if T(i)=B(i+1) then call Stack_Full(i)T(i) T(i)+1S(T(i))x endProcedure pop(i,x)if T(i)=B(i) then call Stack_Empty(i)x S(T(i))T(i) T(i)-1end

    B(1)T(1)

    B(3)T(3)

    B(i)T(i)

    B(m)T(m)

    B(2)T(2)

    1

    [n/m]

    2*[n/m]

    (i-1)*[n/m]

    (m-1)*[n/m]

  • 3-4(6*2+5*9)/3(infix notation)(infix)

    2+3 3*5 8 - 2 (prefix)

    2+3 +23 2*3+4*5 +*23*45 (postfix)

    2+3 23+ 2*3+4*5 23*45+

  • 3-4-1

  • 3-4-12+3*4+5

  • 3-4-2+*23*45

  • 3-4-3 23*45*+

  • 3-5 (1)(infix)(prefix) (2)(infix)(postfix) P3-53

  • 3-52*3+4*5

    2*3+4*5

    2*3+4*5

    2*3+4*5

    2*3+4*5

  • 3 . 5 . 1 6+2*9/3+4*2 8

  • 3 . 5 . 3 (1) A/BC+D*EA*C(2) (A+B)*D+E/(F+A*D)+C(3) A B C(4) A B+C()

  • ISP(In Stack Priority)ICP(I n Coming Priority) (1 ) ' ) ' ' ( ' ' ) ' ISP>ICP (2 ) ISP>=ICP ' ( ' ')' '('

  • A B*(C+D)/E

  • A B*(C+D)/E

  • 3-6 3-6-13-6-2

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

    -++6/*293*458

    -++6/*293*458

    [- {+(+6)[/(*2)9]3}(*4) 5]8

  • 3-6-1 ABC /DE*+AC*-

    ABC/DE*+AC*-

    /

    A/BC/+D*E-A*C

  • 3-6-2( ) ( < > ) < 2 >

  • +-*/ABCD//EF+GH+-*/ABCD//EF+GH

  • AB+C*DE-FG+*-AB+C*DE-FG+*-