งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 1 Stack & Queue Lecturers : Boontee Kruatrachue.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 1 Stack & Queue Lecturers : Boontee Kruatrachue."— ใบสำเนางานนำเสนอ:

1 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 1 Stack & Queue Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

2 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 2 Stack Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

3 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 3 Stack An ordered collection of items. One end is called a top of the stack. Items are inserted onto the top of the stack. => push Items are deleted from the top of the stack. => pop top pop push LIFO List ( LastInFirstOut )

4 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 4 Stack Implementations 1.Array Implementation 2.Linked Stack

5 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 5 Array Implementation How can we make a stack? What is it? An ordered collection of items. An array. 210210 What data type can we use? 1.What can keep many items? 2.What can make items in ordered? Now, we have place for items of a stack. Next, can stack operations be done?

6 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 6 Stack Top With array, can we do stack operations, push, pop,...? Where can we push? Why? At index 3. 210210 210210 3 We push on the top. int top; What type of data can be top? Where is the top element in each picture?At index 2,3. We need a data structure for top.

7 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 7 Stack Data Structure typedef char T; #define SIZE 6 //constant SIZE T item[SIZE]; int top; itemABC 012345 CBACBA top= top = 2 543210543210

8 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 8 Stack Operations

9 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 9 push( ) CBACBA top = 2 1 0 DCBADCBA 210210 top = 3 ++ top; item[top] = i; push D item[++top] = i; What if the array is full ? Check Overflow

10 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 10 pop( ) DCBADCBA 210210 top=3 DCBADCBA 3 2 1 0 pop D int oldTop = top; top --; return item[oldTop]; return item[top--]; oldTop= top= What if the array is empty ? Check Underflow Need to erase D? No. Only change top. No. Only change top.

11 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 11 isEmpty( ) CBACBA top = 2 1 0 C B A 210210 top = -1 return (top==-1); Try pop until empty What happens when stack empty? top =

12 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 12 init( ) ++ top; item[top] = ‘A’; top = 210210 test : push(A) A Before pushing, a stack is empty. 210210 top = -1

13 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 13 isFull( ) top = SIZE What happens when stack full? top = 3 2 1 0 DCBADCBA return (top==SIZE);

14 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 14 Stack Implementations 1.Array Implementation 2.Linked Stack

15 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 15 top Check if it support every operations. Linked Stack

16 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 16 Queue Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

17 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 17 Queue An ordered collection of items. There are 2 ends, head (front) and tail (rear). Items are inserted to the head (front). => enQueue Items are deleted from the tail (rear). => deQueue แถวคอ ย FIFO List FirstInFirstOut FIFO List FirstInFirstOut front / head rear/tail enQueue deQueue

18 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 18 Queue Implementations 1.Linked Queue 2.Array Implementation

19 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 19 front rear Support every operations ? Linked Queue frontrear front rear How do they link? Every operations ?

20 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 20 Queue Implementations 1.Linked Queue 2.Array Implementation

21 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 21 C D E f r 0 1 2 3 4 Array Implementation Not full. But cannot enqueue ! A B No place left at the rear side ! Can we enqueue at the other side ? Think as an array is a circle. Straight array C D E 1 0 f = 2 3 4 = r Circular array

22 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 22 Array Implementation r++ does not work ! C D E 1 0 f = 2 3 4 = r#define SIZE 5 if (r == (SIZE -1)) r = 0; else r++; void increase(int &r) Also f++ does not work ! if (i == (SIZE -1)) i = 0; else i++; void increase(int &i)

23 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 23 isEmpty( ), isFull( ) 1 0 3 4 2 r = f = 1 0 2 3 r r = f = f 0 1 2 3 4 A B r f f r f f isEmpty( ), can’t just check r>f ? or f>r ? It’s circular. Also can’t for isFull( ). 0 1 2 3 4 E

24 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 24 isEmpty( ) / isFull( ) Solution #define MAX 50 typedef int T; struct queue{ T data[MAX]; int front, rear; int count; }; enQ deQ init isFull isEmpty count++ count-- count = 0 count == SIZE count == 0

25 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 25 Queue Operations 1.enQ 2.isFull 3.deQ 4.isEmpty 5.init

26 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 26 Algorithms

27 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 27 Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

28 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 28 Parenthesis Matching Are the parenthesises correct? (a+b-c)*[d+e]/{f*(g+h)} [(a+b-c}*[d+e]/{f*(g+h)} (a+b-c *[d+e]/{f*(g+h) }

29 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 29 Parenthesis Matching Match ? Count : [ { ( ] } ) ? No. We can do it since we were young. How ? [ (a+b)*{ (d+e)-3}]

30 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 30 Parenthesis Matching Simulate what you do. [ (a+b)*{ (d+e)-3}] random 1.Random position (until found match?). 2.Scan out both ways. 3.if Left side = open paren & Right side = close paren. 4. If match : 5.Check out. 6.goto 2 7. else end process. 8.else goto 1 / / / / ↑ ↑ // / / How to program this algorithm ? What data structures are needed ? Difficult !

31 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 31 Parenthesis Matching [ ( ) { ( ) }] [ (a+b)*{ (d+e)-3}] Clear out what are irrelevant. Parenthesis matching’s nature is stack. How ? The close parenthesis matches the previous open parenthesis. [ ( ) { ( ) }]

32 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 32 Parenthesis Matching [ (a+b)*{ (d+e)-3}] Scan left to right. Open paren : push to a stack. Close paren : check if match ? with top in stack. match pop out. else done : not match ! ↑ ( { [ (

33 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 33 Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty() ch = non_paren match(open,ch) [ (a+b)*{ (d+e)-3}] ( [

34 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 34 Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty()

35 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 35 Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

36 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 36 Evaluate Postfix Notation Infix Form Prefix Form Postfix Form (Polish notation) a + b + a b a b + a + b * c + a * b ca b c * +

37 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 37 6 5 2 3 + 8 * - 3 + * = ? input: 6523 input: + Push : 6, 5, 2, 3 o2 <- pop#1 o1 <- pop#2 push S What ‘s next ? 6 5 2 3 + 5 Evaluate Postfix Notation 5 5 6 S

38 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 38 6 5 2 3 + 8 * - 3 + * = ? input: 6523 Push : 6 5 2 33 2 5 6 s input: + pop#1 → 3 pop#2 → 2 5 push 2+35 6 s input: 88 push 85 5 6 input: * pop#1 → 8 40 pop#2 → 5 5 push 5*86 input: - pop#1 → 40 pop#2 → 5 -35 push 5-406 input: 3 push 33 -35 6 input: + pop#1 → 3 pop#2 → -35 -32 push -35+36 input: * pop#1 → -32 pop#2 → 6 push 6 * -32 -192

39 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 39 Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

40 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 40 Infix to Postfix Conversion ab*c+a output stack b c+ * a*b+c ===>

41 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 41 Infix to Postfix Conversion a*b+c ---- > input: stack: output: a*b+ca a*b+c *a a*b+c *ab a*b+c +ab* a*b+c +ab*c a*b+cab*c+ ab*c+

42 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 42 Infix to Postfix Conversion a+b*c ---- > input: stack: output: a+b*ca a+b*c +a a+b*c +ab * * a+b*c +abc a+b*cabc*+ abc*+

43 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 43 input:stack: output: a+b*c-da a+b*c-d +a a+b*c-d +ab * * a+b*c-d +abc a+b*c-d -abc*+ a+b*c-d -abc*+d a+b*c-dabc*+d- abc*+d- a+b*c-d => Infix to Postfix Conversion

44 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 44 Infix to Postfix Conversion a+b*c-(d/e+f)*g +ab * a+b*c-(d/e+f)*g +abc a+b*c-(d/e+f)*g -abc*+ ( a+b*c-(d/e+f)*g -abc*+d / ( a+b*c-(d/e+f)*g -abc*+de abc*+de/f+g*- a+b*c-(d/e+f)*g =>

45 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 45 Infix to Postfix Conversion (cont.) a+b*c-(d/e+f)*g => / ( a+b*c-(d/e+f)*g -abc*+de + ( a+b*c-(d/e+f)*g -abc*+de/f a+b*c-(d/e+f)*g -abc*+de/f+ * a+b*c-(d/e+f)*g -abc*+de/f+g a+b*c-(d/e+f)*g -abc*+de/f+g*- abc*+de/f+g*-

46 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 46 Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

47 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 47 void sth(int i){ int x = 3; i = x; } void f() { int k = 8; sth(k); sth( 5); Add1(&k); Ad1(k); } 3 int i = 5; 53 //pass data k f( ) 8 sth( ) i 8 x 3 //pass address //pass by reference Ad1( ) Add1( ) p *p 3 Pass : by Value, Pass Address &. int *p = &k; void Add1(int *p) { (*p)++; } 9 10 void Ad1(int& ref){ ref++; } ref int& ref = k; pass by reference is tecnically the same as pass address but for writing convenience (no dereference) int& ref = k; ref is another name of k int i = k; int i = 5; by Reference (C++) Stack frame Stack top

48 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 48 Struct Stack void push (int item[], int *pTop, T d ){ (*pTop)++; item[*pTop] = d; } #define SIZE 6 typedef int T; #define SIZE 6 typedef int T; typedef struct stack { T item[SIZE]; int top; } stack; void push (stack *sp, T d){ if (!isFull(*sp)) sp->item[++sp->top] = d; } int main(){ T item[SIZE], item2[SIZE]; int top, top2; init(&top); push( item, &top,5); int main() { stack s, s1; init(&s); push(&s, 5);

49 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 49 Lab Questions

50 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 50 Common Misunderstanding int *p = &i; int *p; p = &i; int *s, t, *u; int *s; int t; int *u; *p = &i;  t is int s and u are pointers int *q = p; int *q; q = p; q points to what p points to q p i 8 59

51 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 51 Lab : Scramble List

52 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 52 Bottom Up h Lift it. Take the bottom up. Take the bottom up. I I love you very much C++ Where ? h2 t2 t1 Try to print h2. Opps!

53 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 53 h1 4 4 5 5 6 6 7 7 8 8 9 9 t1 h2 t2 Riffle Shuffle Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 1 1 2 2 3 3 h1 4 4 5 5 6 6 7 7 8 8 9 9 t1 h2 t2 1 1 2 2 3 3 Lift up to 2 packets.

54 รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 54 h1 4 4 5 5 6 6 7 7 8 8 9 9 t1 1 1 2 2 3 3 h2 t2 h1 4 4 5 5 6 6 7 7 8 8 9 9 t1 1 1 2 2 3 3 h2 t2 Lift up to 2 packets. Riffle Shuffle Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet.


ดาวน์โหลด ppt รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL 01076249 Data Structures & Algorithms : Stack & Queue 1 Stack & Queue Lecturers : Boontee Kruatrachue.

งานนำเสนอที่คล้ายกัน


Ads by Google