1 / 19

Stack & Queue

Stack & Queue. Winter-Camp-2011 CSIE.NTNU. N.S.Lin@csie.ntnu@Taiwan. CC – 非商業性 – 相同方式. Stack. 一種串列式資料結構 LIFO (Last In First Out). Stack Sample. Make a Stack. -1. 0. 1. 2. 3. 4. 5. 6. 7. #define SIZE 8 int my_stack[SIZE]; int pointer = -1. if (pointer < 0) null.

calla
Download Presentation

Stack & Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack & Queue Winter-Camp-2011 CSIE.NTNU N.S.Lin@csie.ntnu@Taiwan CC– 非商業性 – 相同方式

  2. Stack • 一種串列式資料結構 • LIFO(Last In First Out)

  3. StackSample

  4. Make a Stack -1 0 1 2 3 4 5 6 7 #define SIZE 8 int my_stack[SIZE]; int pointer = -1 if (pointer < 0) null. if (pointer == SIZE) full.

  5. Queue • 一種串列式資料結構 • FIFO(First In First Out)

  6. QueueSample

  7. Make a Queue 0 1 2 3 4 5 6 7 if (front == rear) null. if (rear == SIZE) full.

  8. Circular Queue

  9. Stack Applications • LISP functions • Infix to Postfix

  10. LISP Functions

  11. LISP Functions ( ( () () ) ) oddp - 5 2 + * 2 3

  12. LISP Functions ( ( () () ) ) - 將左括號放入stack, 看到右括號就從stack拿出一個左括號 ( ( ( (

  13. Infix to Postfix • Infix : (1 + 2) * 3 + 4 • Postfix : 1 2 + 3 * 4 +

  14. Infix to Postfix • Usea Stack • Output the numbers • Push operators into the stack • If there are operators having higher(or equal) precedence, pop them. • When read ) , pop all operators until pop a ( .

  15. Infix to Postfix • Operator Precedence • Outside : (> * = / > + = - • Inside : * = / > + = - > (

  16. Infix to Postfix • ( 1 + 2 ) * 3 / 4 4 / 1 2 + 3 * + / ( *

  17. Queue Applications • BFS

  18. Test Yourself • 673 - Parentheses Balance • 271 - Simply Syntax • 727 - Equation

  19. Test Yourself Practice: Use a Circular Queue Sample Input: 100 200 -1 4 20 -1 -1 10 -1 0 Sample Output: 100 200 4 20

More Related