บทที่ 4 สแตกและคิว (stack & queue)

Download บทที่  4 สแตกและคิว  (Stack & Queue)

If you can't read please download the document

Post on 14-Jan-2016

46 views

Category:

Documents

5 download

Embed Size (px)

DESCRIPTION

บทที่ 4 สแตกและคิว (Stack & Queue). FIFO และ LIFO. F irst I n F isrt O ut : FIFO หมายถึงข้อมูลที่ เข้ามา ในลิสต์ ก่อน จะถูก นำออก จากลิสต์เป็น ลำดับแรก ตัวอย่างได้แก่การยืนรอคิวเพื่อซื้อตั๋ว - PowerPoint PPT Presentation

TRANSCRIPT

  • 4 (Stack & Queue)

  • FIFO LIFO

    First In Fisrt Out : FIFO Last In First Out : LIFO

  • (stack) stack stack stack "" (Last In First Out : LIFO)

  • (Operation) Stack push pop top push pop (CREATE) stack (EMPTY) stack (FULL) stack stack (CLEAR)

  • (Push) (Top) (Pop) ()

  • (Push) (Top of the Stack) (Stack Full)

  • 01234Top901. Top = 02. Top = Top + 13. Stack[Top] =10154060StackStack Full

  • (Pop) Push (Stack Empty)

  • Top2. Top = Top - 11. Temp = Stack[Top]Stack Empty10154060

  • stack Array stack array static stack

  • - 4 Push Pop

  • int Stack[4];

    Stack

  • Top Top 0

  • I10154060Top60401510

  • stack Link List stack Link list Dynamic Pointer

  • 4 , Push, Pop

  • typedef struct node{ int data; struct node *Next;} node;node *Top;int info;nodeNext

  • 80^PTop1. Top = Null2. New(P)3. Data(p) = 804. Next(p) = Null5. Top = p

  • 90

    80^Top1. New(P)2. Data(P) = 903. Next(P) = Top4. Top = pP90Top

  • 80^Top1. New(P)2. Data(P) = 1203. Next(P) = Top4. Top = pP120Top90

  • 80^TopTempTop901. Temp=Top2. Top = Next(Top)

  • TopTempTop801. Temp=Top2. Top = Next(Top)^

  • Top = NULLTempTop1. Temp=Top2. Top = Next(Top)

  • Top = NULL1. Temp=TopStack Empty

  • 80^P120Top90PP1209080

  • stack stack (Balancing Symbol)

  • PROGRAM MAIN......CALL Sub1PRINT Q....END MAINPROCEDURE Sub1....CALL Sub2A:=A+B...END Sub1PROCEDURE Sub2...END Sub2

  • Factorial

  • Stack10001500

  • Factorial n! 0!= 11!= 12!= 2 x 13!= 3 x 2 x 1n!= n x (n-1) x (n-2) x x 2 x 1

  • 3!01234Top321Stack3! = 3 * 2!2! = 2 * 1!1! = 1 * 0!0! = 1PushPop1! = 1 * 12! = 2 * 13! = 3 * 2

  • (Balancing Symbol)

    Stack 1. - {,(,[ PUSH stack- },),] TOP stack - POP stack - error2. stack stack error

  • (Polish Notation) 2*3 *23 Prefix : * + 5 3 2Infix : (5+3)*2Postfix : 5 3 + 2 *

  • Algorithm Postfix

    () push stack() pop item stack 2 push stack

  • Algorithm Postfix

  • Infix PostfixAlgorithm Infix Postfix EXP Infix Stack stack NEXP Postfix1. ( Stack2. EXP 2.1 () NEXP2.2 ( push stack

  • Infix Postfix2.3 () - pop 2.3 NEXP - 2.3 push stack 2.4 ) - push NEXP (- push ( 3.

  • Stack** ()34* , /22+ , -11(03

  • A + B

    Stack PostfixA+B++AAABAB+

  • A + B * C

    Stack PostfixA+B*C+++ , *+ , *AAABABABCABC*+

  • (A + B) * C

    Stack Postfix(A+B)*C(((+(+ * *AAABAB+AB+AB+CAB+C*

  • ((10/2-2)*2+2)

  • 8*(4+3)-9/6

    Stack Postfix8*(4+3)-9 /6**(*(*(+*(+*--- /- /8888 48 48 4 38 4 3 +8 4 3 + *8 4 3 + * 98 4 3 + * 98 4 3 + * 9 68 4 3 + * 9 6 / -

  • 1. ((A+B)*C)-D2. A*C/D3. 10+15*20/64. 5*4+(8/2)5. 10/(2+3)+(5+6)*96. A*(B-C/D)+E

    *********************