สแตก และคิว (Stack & Queue)

Download สแตก และคิว  (Stack & Queue)

Post on 15-Jan-2016

51 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

(Stack & Queue). FIFO LIFO. F irst I n F isrt O ut : FIFO - PowerPoint PPT Presentation

TRANSCRIPT

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

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

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

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

  • (Top of the Stack) (Stack Full)

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

  • Push (Stack Empty)

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

  • stack array static stack

  • - 4 Push Pop

  • int Stack[4];

    Stack

  • Top Top 0

  • I10154060Top60401510

  • stack Link list Dynamic Pointer

  • 4 , Push, Pop

  • 3 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 (Balancing Symbol)

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

  • Factorial

  • Stack10001500

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

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

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

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

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

  • 1. Completely parnthesize th infix notation to specify the order of all operations2. Move each operator to the space held by its corressponding right parenthesis3. Remove all parenthesisA/B^C+D*E-A*C 1--> (((A/(B^C))+(D*E))-(A*C)) 2--> (((A(BC)^)/(DE)*)+(AC)*) - 3--> ABC^/DE*+AC*-

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

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

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

  • Stack PostfixA+B++AAABAB+

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

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

  • 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

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