queue & stack

Download Queue & stack

Post on 01-Jan-2016

68 views

Category:

Documents

3 download

Embed Size (px)

DESCRIPTION

作業一解答下星期一也會上線. Queue & stack. Michael Tsai 2010/10/8. 作業二今天會放上網站. 各位同學寫作業一辛苦了 . 請發表對這次作業的 感想. 今日菜單. 複雜的 KMP 例子 上次沒有講完的 sparse matrix Stack Queue 計算機 迷宮. Failure function. 另外一個定義 (recursive). 朋友三 : 矩陣. 怎麼存起來 ? 用 二 維 陣列 , a[row][col] 來存 加法乘法都很容易. 很鬆的矩陣. 浪費 很多空間在 存 T_T - PowerPoint PPT Presentation

TRANSCRIPT

Queue & stack

Queue & stackMichael Tsai2010/10/8

....KMPsparse matrixStackQueue

2

Failure function3abcabdabcabcfabcabdabcabcf-1-1-101-1012342: 456index012345678Row600011245Col603512302value81522-15113-69128, transpose

row & col? (row)7index012345678Row603512302Col600011245value81522-15113-69128

O(columns+elements)transpose?

, O(columns+elements)=O(columns+columns*rows)=O(columns*rows)

? program 2.9 (p.78)8, ? (column)Transpose

9row & colabb10program 2.102.11 (p.81), Time complexity = O(??), 11O(rowsA*colsA*colsB)11StackStack?Ordered List?: Pop: Push, ??? 10(Top)(Bottom)1241054104103410Answer: 12Stack(First-In-Last-Out), ? ?:Stack(Push)(Pop)??13: function? activation recordstack framereturn address: functionprevious frame pointer: stack framestack()local variables.recursive call?Stack overflow?Previous frame pointerReturn addressPrevious frame pointerReturn addressLocal variablesPrevious frame pointerReturn addressspfp1414implementstack?: Arrayarray, ?indeximplementoperations?PushPop??15?int stack[MAX_SIZE];MAX_SIZE, . T_Tstack=(int*)malloc(sizeof(int)*MAX_SIZE);stack?: realloc?realloc:realloc?realloc?16 void *realloc(void *ptr, size_t size); 16realloccapacity stackrealloc(stack, 2*capacity*sizeof(int));capacity*=2;: push n ?17181920QueueQueue?Ordered ListStack?(Rear), (Front): Delete: Add, ??? 5410(Front)(Rear)21Queue, First-In-First-Out (FIFO), ? ?:Queue(Add)(Delete)??22Job schedulingpriorityjob FIFO:job, .: , queue, queuejob: FIFO stack?2323, implement queue?arrayarray(index)! !?120-5120-5312024: circular queuequeueelementarray??(?)20-50-5320200-550-532525, ?realloc?realloc: 50-5326: stackarray: array, arraystack, implement?stack? push & pop?: , nstack, array?stack??27:28: : 1+2*3-5/(4+5)/5, .? T_T291+2*3-5/(4+5)/5:Operand 1, 2, 3, 5, 4, 5, etc.Operator + * - / (,)1: (left-to-right associativity)2:infix (operatoroperand)operator, , 30: postfix31operatoroperands 2+3*4 2 3 4 * +a*b+5 ?(1+2)*7 ?a*b/c ?(a/(b-c+d))*(e-a)*c?a/b-c+d*?e-a*c?p131 Figure 3.1331Postfix?stack! : 6 2 / 3 4 2 * +6, 6stack (stack: 6)2, 2stack (stack: 6 2)/, operands (62), 6/2, stack (stack: 3)3, 3stack (stack: 3 3)-, operands(33), 3-3, stack (stack: 0).()32: infix to postfix: infix expressionpostfix expression?: ()1. expression2. operatoroperand, 3. ? ()33stack:1. expression2. operand3. operatorstackoperatoroperator (precedence)operator, operatorstackoperator, stackoperator, stackoperatorstackoperator?, stack, FILO, 34precedence=34 a+b*cpostfix , ?35page 135 Figure 3.1535, stack: a*(b+c) , stack(top)aa, *a, *(ab, *(ab, *(+abc, *(+abc+*361: ?stack operator:stackstackoperatorstack37Q: stack, operator?A: stackoperatorQ: expression, stackoperator?A: operator38a+(b*c/(d-f)+e) (? :P) page 137program 3.15 infix prefix: prefix: a*b+c +*abc 39: 0, 1. .: (0,0)(7,7) ? (): stack4001234567001111011100000001211111110310000001401111111500011010601010101701101110, ?, , (), , : ()stack: 41Q: , stack?A: , (row, col)()Q: ? ()A: , 42algorithm(row_start, col_start, ) stackwhile(stack) {stack(row, col, dir)while(dir) {(row, col)dir, (row_n, col_n, dir).(row_n, col_n), (row_n, col_n) {(row_n, col_n)(row, col, dir)stackrow=row_n; col=col_n; dir=;}}}4344~, ()45Michael