Stack Queue Lecturers Text 1 Boontee Kruatrachue Kritawan
Stack & Queue Lecturers : Text : รศ. ดร. บญธร เครอตราช 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 & Queue
Stack Lecturers : Text : รศ. ดร. บญธร เครอตราช 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 & Queue
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 รศ. ดร. บญธร เครอตราช 3 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Implementations 1. Array Implementation 2. Linked Stack รศ. ดร. บญธร เครอตราช 4 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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? รศ. ดร. บญธร เครอตราช 5 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Top With array, can we do stack operations, push, pop, . . . ? Where can we push? 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. int top; What type of data can be top? 3 2 1 0 รศ. ดร. บญธร เครอตราช 6 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Data Structure typedef char T; #define SIZE 6 //constant SIZE T item[SIZE]; int top; item รศ. ดร. บญธร เครอตราช 7 A B C 0 1 2 รศ. กฤตวน ศรบรณ 5 4 3 top= 2 1 0 top = 2 3 C B A 4 5 KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Operations รศ. ดร. บญธร เครอตราช 8 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
push( ) What if the array is full ? Check Overflow ++ top; push D item[top] = i; item[++top] = i; top = 3 D top = 2 C 1 B 0 A รศ. ดร. บญธร เครอตราช 9 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
pop( ) What if the array is empty ? pop top=3 D 2 Check Underflow int old. Top = top; top --; return item[old. Top]; D old. Top=3 D C top=2 C 1 B 0 A รศ. ดร. บญธร เครอตราช 10 รศ. กฤตวน ศรบรณ return item[top--]; Need to erase D? No. Only change top. KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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 รศ. ดร. บญธร เครอตราช 11 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
init( ) Before pushing, a stack is empty. 2 test : push(A) 1 0 ++ top; top = -1 2 1 top = 0 A item[top] = ‘A’; รศ. ดร. บญธร เครอตราช 12 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
is. Full( ) What happens when stack full? top = SIZE top = 3 D 2 C 1 B 0 A รศ. ดร. บญธร เครอตราช 13 รศ. กฤตวน ศรบรณ return (top==SIZE); KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Implementations 1. Array Implementation 2. Linked Stack รศ. ดร. บญธร เครอตราช 14 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Linked Stack top Check if it support every operations. รศ. ดร. บญธร เครอตราช 15 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Queue Lecturers : Text : รศ. ดร. บญธร เครอตราช 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
Queue แถวคอย front / head rear/tail en. Queue de. Queue FIFO List First. In. First. Out • An ordered collection of items. There are 2 ends, head (front) and tail (rear). • Items are inserted to the head (front). => en. Queue • Items are deleted from the tail (rear). => de. Queue รศ. ดร. บญธร เครอตราช 17 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Queue Implementations 1. Linked Queue 2. Array Implementation รศ. ดร. บญธร เครอตราช 18 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Linked Queue front rear How do they link? Support every operations ? front rear Every operations ? รศ. ดร. บญธร เครอตราช 19 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Queue Implementations 1. Linked Queue 2. Array Implementation รศ. ดร. บญธร เครอตราช 20 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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. รศ. ดร. บญธร เครอตราช 21 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Array Implementation #define SIZE 5 0 1 f=2 C r++ does not work ! E 4=r D 3 &r) void increase(int &i) if (i (r == == (SIZE -1)) ri == 0; 0; else r++; i++; Also f++ does not work ! รศ. ดร. บญธร เครอตราช 22 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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. Also can’t for is. Full( ). รศ. ดร. บญธร เครอตราช 23 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
is. Full( ) 0 1 2 3 4 0 A B C D E f 1 2 B C A 3 rf f f =1 r= E 4 D 2 4 A F B C D E r f =0 1 2 r r =0 B C F E 4 D 3 3 is. Full( ), also can’t just check r>f ? or f>r ? รศ. ดร. บญธร เครอตราช 24 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
is. Empty(), is. Full( ) ? 0 1 2 3 4 A B C D E f 1 2 0 B C A 2 3 0 r =1 r= E 4 D 3 4 r f =0 1 4 f =2 3 is. Empty() & is. Full( ), has the same condition ie. front is next to rear. How can we seperate is. Empty() and is. Full()? รศ. ดร. บญธร เครอตราช 25 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
is. Empty( ) / is. Full( ) Solution #define MAX 50 typedef int T; struct queue{ T data[MAX]; int front, rear; }; รศ. ดร. บญธร เครอตราช 26 int count; en. Q count++ de. Q count-- init count = 0 is. Full count == SIZE is. Empty count == 0 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Queue Operations 1. en. Q 2. is. Full 3. de. Q 4. is. Empty 5. init รศ. ดร. บญธร เครอตราช 27 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Algorithms รศ. ดร. บญธร เครอตราช 28 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช 29 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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) } รศ. ดร. บญธร เครอตราช 30 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Parenthesis Matching [ (a+b)*{ (d+e)-3}] Match ? Count : [ { ( ] } ) ? No. We can do it since we were young. How ? รศ. ดร. บญธร เครอตราช 31 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Parenthesis Matching Simulate what you do. random How to program this algorithm ? 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. else goto 1 รศ. ดร. บญธร เครอตราช 32 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
){( ) }] Clear out what are irrelevant. Parenthesis matching’s nature is stack. How ? The close parenthesis matches the previous open parenthesis. รศ. ดร. บญธร เครอตราช 33 รศ. กฤตวน ศรบรณ ){( [( [( [ (a+b)*{ (d+e)-3}] ) }] Parenthesis Matching KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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 ! รศ. ดร. บญธร เครอตราช 34 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Warnier-Orr Diagram Design Tool [ (a+b)*{ (d+e)-3}] Init empty stack s error=false read ch Scan (not EOF && not error) ch = non_paren ch = open paren + s. push(ch) s. empty() ch = close paren Paren matching + s. empty() error =true(no match-open-paren) open = s. pop() match(open, ch) notmatch(open, ch) error + s. empty() รศ. ดร. บญธร เครอตราช 35 (missmatch) no match-open-paren / missmatch s. empty() รศ. กฤตวน ศรบรณ error = true MATCH ( open paren exceed [ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Warnier-Orr Diagram Design Tool Init empty stack s error=false read ch 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() s. empty() notmatch(open, ch) error + no match-open-paren / missmatch s. empty() error + s. empty() รศ. ดร. บญธร เครอตราช 36 รศ. กฤตวน ศรบรณ error = true (missmatch) MATCH open paren exceed KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช 37 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Evaluate Postfix Notation Infix Form Prefix Form a+b*c +ab Postfix Form (Polish notation) ab+ +a*bc abc*+ รศ. ดร. บญธร เครอตราช 38 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Evaluate Postfix Notation 6523+8*-3+* =? input: 6523 Push : 6, 5, 2, 3 3 2 5 6 S input: + o 2 <- pop#1 o 1 <- pop#2 5 push 5 + 5 6 S What ‘s next ? รศ. ดร. บญธร เครอตราช 39 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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 รศ. ดร. บญธร -32 -35+3 เครอตราช 40 6 รศ. กฤตวน ศรบรณ pop#2 → 6 push 6 * -32 - 192 KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช 41 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Infix to Postfix Conversion aa*b+c * b+ c ===> ab*c+ output stack รศ. ดร. บญธร เครอตราช 42 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Infix to Postfix Conversion input: a*b+c a*b+c รศ. ดร. บญธร เครอตราช 43 a*b+c ---- > stack: * * + + รศ. กฤตวน ศรบรณ ab*c+ output: a a ab ab*c+ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Infix to Postfix Conversion input: stack: a+b*c + + * + a+b*c รศ. ดร. บญธร เครอตราช 44 รศ. กฤตวน ศรบรณ a+b*c ---- > abc*+ output: a a ab ab abc*+ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Infix to Postfix Conversion input: a+b*c-d a+b*c-d รศ. ดร. บญธร เครอตราช 45 stack: + + * + - รศ. กฤตวน ศรบรณ a+b*c-d output: a a ab => abc*+d- ab abc*+d- KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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 รศ. ดร. บญธร เครอตราช 46 + * + ( / ( - รศ. กฤตวน ศรบรณ abc*+de/f+g*ab abc*+de KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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 รศ. ดร. บญธร เครอตราช 47 / ( + ( * - รศ. กฤตวน ศรบรณ abc*+de/f+g abc*+de/f+g*KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Stack Applications • Parenthesis Matching • Evaluate Postfix Expression • Infix to Postfix Conversion (Reverse Polish Notation) • Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช 48 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Pass : by Value, 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 } Pass Address & int i = 5; by Reference (C++). 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 รศ. ดร. บญธร เครอตราช 49 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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); รศ. ดร. บญธร เครอตราช 50 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Lab Questions รศ. ดร. บญธร เครอตราช 51 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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; t is int รศ. ดร. บญธร เครอตราช 52 รศ. กฤตวน ศรบรณ int *s; int t; int *u; q p 59 i 59 59 8 KMITL 01076249 Data Structures & Algorithms : Stack & Queue
Lab : Scramble List รศ. ดร. บญธร เครอตราช 53 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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++ รศ. ดร. บญธร เครอตราช 54 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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. รศ. ดร. บญธร เครอตราช 55 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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. รศ. ดร. บญธร เครอตราช 56 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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); รศ. ดร. บญธร เครอตราช 57 รศ. กฤตวน ศรบรณ i 369 c A 369 Append EOF KMITL 01076249 Data Structures & Algorithms : Stack & Queue
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, . . . ); รศ. ดร. บญธร เครอตราช 58 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack & Queue