Stack Queue Lecturers Text Queue 1 Boontee Kruatrachue
Stack & Queue Lecturers : Text : รศ. ดร. บญธร Queue เครอตราช 1 Boontee Kruatrachue Kritawan Siriboon Room no. 913 Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Lecturers : Text : รศ. ดร. บญธร Queue เครอตราช 2 Boontee Kruatrachue Kritawan Siriboon Room no. 913 Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack push pop top LIFO List ( Last. In. First. Out ) • 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 รศ. ดร. บญธร Queue เครอตราช 3 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Implementations 1. Array Implementation 2. Linked Stack รศ. ดร. บญธร Queue เครอตราช 4 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Array Implementation How can we make a stack? What is it? An ordered collection of items. What data type can we use? 1. What can keep many items? 2. What can make items in ordered? An array. 2 1 0 Now, we have place for items of a stack. Next, can stack operations be done? รศ. ดร. บญธร Queue เครอตราช 5 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Top With array, can we do stack operations, push, pop, . . . ? Where can we push in the array ? Why? At index 3. We push on the top. We need a data structure for top. Where is the top element in each picture? At index 2, 3. What type of data can be top -> index ? int top; 3 2 1 0 รศ. ดร. บญธร Queue เครอตราช 6 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Data Structure typedef char T; #define SIZE 6 //constant SIZE T item[SIZE]; int top; item A B C 0 1 2 5 4 3 top= 2 1 0 3 C B A 4 5 top 2 รศ. ดร. บญธร Queue เครอตราช 7 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Operations รศ. ดร. บญธร Queue เครอตราช 8 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
push( ) What if the array is full ? pushing variable i push D Check Overflow ++ top; item[++top] = i; item[top] = i; top = 3 D top = 2 C 1 B 0 A item รศ. ดร. บญธร Queue เครอตราช 9 รศ. กฤตวน ศรบรณ KMITL item 01076249 Data Structures & Algorithms : Stack &
pop( ) What if the array is empty ? pop top=3 D 2 int old. Top = top; top --; return item[old. Top]; D old. Top=3 D C top=2 C 1 B 0 A รศ. ดร. บญธร Queue Check Underflow เครอตราช 10 รศ. กฤตวน ศรบรณ KMITL return item[top--]; Need to erase D? No. Only change top. 01076249 Data Structures & Algorithms : Stack &
is. Empty( ) What happens when stack empty? Try pop until empty return (top==-1); top = 2 C top =2 C 1 B 0 A top = -1 รศ. ดร. บญธร Queue เครอตราช 11 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
init( ) Before pushing, a stack is empty. 2 test : push(A) 1 0 ++ top; top = -1 2 1 top = 0 A item[top] = ‘A’; รศ. ดร. บญธร Queue เครอตราช 12 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
is. Full( ) What happens when stack full? top = SIZE top = 3 D 2 C 1 B 0 A รศ. ดร. บญธร Queue เครอตราช 13 รศ. กฤตวน ศรบรณ KMITL return (top==SIZE); 01076249 Data Structures & Algorithms : Stack &
Stack Implementations 1. Array Implementation 2. Linked Stack รศ. ดร. บญธร Queue เครอตราช 14 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Linked Stack top Check if it support every operations. pop ? รศ. ดร. บญธร Queue เครอตราช 15 รศ. กฤตวน ศรบรณ push ? KMITL 01076249 Data Structures & Algorithms : Stack &
Queue Lecturers : Text : รศ. ดร. บญธร Queue เครอตราช 16 Boontee Kruatrachue Kritawan Siriboon Room no. 913 Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Queue แถวคอย front / head rear/tail FIFO List First. In. First. Out en. Queue (insert) de. Queue (delete) • An ordered collection of items. There are 2 ends, head (front) and tail (rear). • Items are inserted to the tail (rear). => en. Queue • Items are deleted from the head (front). => de. Queue รศ. ดร. บญธร Queue เครอตราช 17 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Queue Implementations 1. Linked Queue 2. Array Implementation รศ. ดร. บญธร Queue เครอตราช 18 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Linked Queue front rear How do they link? Support every operations ? front rear en. Queue ? (insert) de. Queue ? (delete) Every operations ? รศ. ดร. บญธร Queue เครอตราช 19 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Queue Implementations 1. Linked Queue 2. Array Implementation รศ. ดร. บญธร Queue เครอตราช 20 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Array Implementation 0 1 2 3 0 1 4 A B C D E f r Straight array Not full. But cannot enqueue ! f=2 No place left at the rear side ! C E 4=r D 3 Circular array Can we enqueue at the other side ? Think as an array is a circle. รศ. ดร. บญธร Queue เครอตราช 21 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Array Implementation #define SIZE 5 0 1 f=2 C r++ does not work ! &r) void increase(int &i) E 4=r if (i (r == == (SIZE -1)) ri == 0; 0; else r++; i++; D 3 Also f++ does not work ! รศ. ดร. บญธร Queue เครอตราช 22 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
is. Empty( ) 0 1 A B 2 3 4 0 f r f f 1 2 3 r =1 1 E r f f 0 4 f =0 r= 4 4 2 f =2 3 3 is. Empty( ): can’t just check r>f ? or f>r ? It’s circular. Same as is. Full( ). รศ. ดร. บญธร Queue เครอตราช 23 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
is. Full( ) 0 1 2 3 4 0 A B C D E f 1 2 C 3 rf f f =0 B 2 4 A F B C D E r A 1 f =1 r= E 4 2 D r r =0 B C F E 4 D 3 3 is. Full( ), also can’t just check r>f ? or f>r ? รศ. ดร. บญธร Queue เครอตราช 24 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
is. Empty(), is. Full( ) ? 0 1 2 3 4 0 A B C D E f 1 2 f =0 C A 2 3 4 r f r B 1 0 r =1 r= E 4 4 f =2 D 3 3 is. Empty() & is. Full( ), has the same condition ie. increase(r) makes it equal front. How to check is. Empty() or is. Full()? รศ. ดร. บญธร Queue เครอตราช 25 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
is. Empty( ) / is. Full( ) Solution #define MAX 50 typedef int T; struct queue{ T data[MAX]; int front, rear; int count; }; รศ. ดร. บญธร Queue เครอตราช 26 en. Q count++ de. Q count-- init count = 0 is. Full count == SIZE is. Empty count == 0 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Queue Operations 1. en. Q 2. is. Full 3. de. Q 4. is. Empty 5. init รศ. ดร. บญธร Queue เครอตราช 27 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Algorithms รศ. ดร. บญธร Queue เครอตราช 28 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร Queue เครอตราช 29 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
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) } รศ. ดร. บญธร Queue เครอตราช 30 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Parenthesis Matching [ (a+b)*{ (d+e)-3}] Match ? Count : [ { ( ] } ) ? No. We can do it since we were young. How ? รศ. ดร. บญธร Queue เครอตราช 31 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Parenthesis Matching Simulate what you do. How to program this algorithm ? random What data structures are needed ? 1. 2. 3. 4. 5. 6. 7. 8. // / / /(d+e)-3}] / /[ /(a+b)*{ Difficult ! ↑ ↑ Random position (until found match? ). Scan out both ways. if Left side = open paren & Right side = close paren. If match : Check out. goto 2 else end process : Not match. else goto 1 รศ. ดร. บญธร Queue เครอตราช 32 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
){( ) }] Parenthesis matching’s nature is a stack. How ? The close parenthesis matches the previous open parenthesis. รศ. ดร. บญธร Queue เครอตราช 33 รศ. กฤตวน ศรบรณ KMITL ){( [( [( Clear out what are irrelevant. [ (a+b)*{ (d+e)-3}] ) }] Parenthesis Matching 01076249 Data Structures & Algorithms : Stack &
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 ! รศ. ดร. บญธร Queue เครอตราช 34 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Design Tools Code if (i == (SIZE -1)) i = 0; else i++; Pseudocode if (i is at the last item) i = 0; else i++; Warnier – Orr Diagram i is last item i=0 i is last item i++ + + Or if. . then A รศ. ดร. บญธร Queue เครอตราช 35 รศ. กฤตวน ศรบรณ KMITL not A 01076249 Data Structures & Algorithms : Stack &
Warnier-Orr Diagram Design Tool [ (a+b)*{ (d+e)-3}] Init empty stack s error=false read ch ch = non_paren Scan (not EOF && not error) ch = open paren + s. push(ch) s. empty() ch = close paren Paren matching + error =true(no match-open-paren) open = s. pop() match(open, ch) s. empty() notmatch(open, ch) error + + error + s. empty() รศ. ดร. บญธร Queue เครอตราช 36 (missmatch) no match-open-paren / missmatch s. empty() รศ. กฤตวน ศรบรณ error = true MATCH ( MISSMATCH open paren exceed KMITL [ 01076249 Data Structures & Algorithms : Stack &
Warnier-Orr Diagram Design Tool Init empty stack s error=false Scan (until EOF || error) read ch ch = open paren + s. push(ch) s. empty() ch = close paren Paren matching + error =true(no match-open-paren) open = s. pop() s. empty() notmatch(open, ch) error + + no match-open-paren / missmatch s. empty() error + s. empty() รศ. ดร. บญธร Queue เครอตราช 37 รศ. กฤตวน ศรบรณ error = true (missmatch) MATCH MISSMATCH open paren exceed KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร Queue เครอตราช 38 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Evaluate Postfix Notation Infix Form Prefix Form a+b*c +ab Postfix Form (Polish notation) ab+ +a*bc abc*+ รศ. ดร. บญธร Queue เครอตราช 39 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Evaluate Postfix Notation’s nature is a stack. 6523+8*-3+* =? input: 6523 Push : 6, 5, 2, 3 3 2 5 6 input: + o 2 <- pop#1 o 1 <- pop#2 5 push 5 S + 5 What ‘s next ? รศ. ดร. บญธร Queue เครอตราช 40 รศ. กฤตวน ศรบรณ KMITL 6 S 01076249 Data Structures & Algorithms : Stack &
6523+8*-3+* =? input: 6523 input: + Push : 6 5 2 3 3 pop#1 → 3 2 pop#2 → 2 5 5 push 2+3 5 6 6 s input: 8 s 8 input: * push 5 pop#1 → 8 40 5 pop#2 → 5 5 6 push 5*8 6 8 input: - input: 3 pop#1 → 40 push 3 3 pop#2 → 5 -35 push 5 -40 6 6 input: + input: * pop#1 → 3 pop#1 → -32 pop#2 → -35 push รศ. ดร. บญธร Queue -32 -35+3 เครอตราช 41 6 รศ. กฤตวน ศรบรณ KMITL pop#2 → 6 push 6 * -32 - 192 01076249 Data Structures & Algorithms : Stack &
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร Queue เครอตราช 42 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion aa*b+c * b+ c ab*c+ ===> Notice : output: operands’ order is the same as input’s. output stack รศ. ดร. บญธร Queue เครอตราช 43 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion input: a*b+c a*b+c รศ. ดร. บญธร Queue เครอตราช 44 a*b+c ---- > stack: * * + + รศ. กฤตวน ศรบรณ KMITL ab*c+ output: a a ab ab*c+ 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion input: stack: a+b*c a+b*c Queue เครอตราช 45 รศ. กฤตวน ศรบรณ abc*+ output: a a ab + + * + a+b*c รศ. ดร. บญธร a+b*c ---- > ab abc*+ KMITL 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion input: a+b*c-d a+b*c-d รศ. ดร. บญธร Queue เครอตราช 46 stack: + + * + - รศ. กฤตวน ศรบรณ a+b*c-d output: a a ab => abc*+d- ab abc*+d- KMITL 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร Queue เครอตราช 47 + * + ( / ( - รศ. กฤตวน ศรบรณ abc*+de/f+g*ab abc*+de KMITL 01076249 Data Structures & Algorithms : Stack &
Infix to Postfix Conversion (cont. ) a+b*c-(d/e+f)*g => abc*+de/f+g*a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร Queue เครอตราช 48 / ( + ( * - รศ. กฤตวน ศรบรณ abc*+de/f+g abc*+de/f+g*KMITL 01076249 Data Structures & Algorithms : Stack &
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see more in recursion) รศ. ดร. บญธร Queue เครอตราช 49 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
int i = 5; Call Stack of Function int i = 5; void f() { int k = 8; sth(k); //pass data sth( 5); Add 1(&k); //pass address Ad 1(k); //pass by reference } int i = k; void sth(int i){ int x = 3; i = x; int *p = &k; } void Add 1(int *p) { (*p)++; } int& ref = k; void Ad 1(int& ref){ ref++; } 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 x 3 3 p 8 3 53 Stack top Stack frame Ad 1( Add 1( sth( )) ) i Stack top Stack frame f( ) ref*p k 8 9 10 รศ. ดร. บญธร Queue เครอตราช 50 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Struct Stack #define SIZE 6 typedef int T; typedef struct stack { T item[SIZE]; int top; } stack; #define SIZE 6 typedef int T; void push (stack *sp, T d){ void push (int item[], int *p. Top, T d ){ (*p. Top)++; if (!is. Full(*sp)) item[*p. Top] = d; sp->item[++sp->top] = d; { } int main(){ T item[SIZE], item 2[SIZE]; int top, top 2; init(&top); push( item, &top, 5); int main() { stack s, s 1; init(&s); push(&s, 5); รศ. ดร. บญธร Queue เครอตราช 51 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Lab Questions รศ. ดร. บญธร Queue เครอตราช 52 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Common Misunderstanding int *p = &i; int *p; p = &i; *p = &i; int *q = p; int *q; q = p; q points to what p points to s and u are pointers int *s, t, *u; int *s; int t; int *u; t is int รศ. ดร. บญธร Queue เครอตราช 53 รศ. กฤตวน ศรบรณ KMITL q p 59 i 59 59 8 01076249 Data Structures & Algorithms : Stack &
Lab : Scramble List รศ. ดร. บญธร Queue เครอตราช 54 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
h Lift it. t 1 Bottom Up I love Where ? h 2 Take the bottom up. you very t 2 Try to print h 2. Opps! much C++ รศ. ดร. บญธร Queue เครอตราช 55 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Riffle Shuffle h 1 t 1 4 1 5 2 6 3 h 2 h 1 t 2 4 1 5 2 6 3 h 2 t 2 7 7 8 8 • Riffle Shuffle each node of each packet from the top. 9 • put the rest at the back of the result packet. t 1 9 • Lift up to 2 packets. รศ. ดร. บญธร Queue เครอตราช 56 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
Riffle Shuffle h 1 4 1 5 2 6 4 1 5 2 t 2 6 3 7 7 t 1 h 2 8 t 1 8 9 9 t 2 3 • Riffle Shuffle each node of each packet from the top. • put the rest at the back of the result packet. Lift up to 2 packets. รศ. ดร. บญธร Queue เครอตราช 57 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &
FILE *fp = fopen("D: /zfile. txt", "w"); 369 fprintf(fp, "369"); fclose(fp); fp = fopen("D: /zfile. txt", "a"); 369 EOF fprintf(fp, "Append"); 369 Append EOF fclose(fp); fp = fopen("D: /zfile. txt", "r"); 369 Append EOF int i; fscanf(fp, "%d", &i); 369 Append EOF char c = fgetc(fp); รศ. ดร. บญธร Queue เครอตราช 58 รศ. กฤตวน ศรบรณ i 369 c A KMITL 369 Append EOF 01076249 Data Structures & Algorithms : Stack &
http: //www. tutorialspoint. com/cpr ogramming/c_file_io. htm FILE * fopen( const char * filename, const char * mode ); int fputc( int c, FILE *fp ); int fputs( const char *s, FILE *fp ); int fprintf(FILE *fp, const char *format, . . . ); int fgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp ); int fscanf(FILE *fp, const char *format, . . . ); รศ. ดร. บญธร Queue เครอตราช 59 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack &