Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack? Menyimpan data dalam susunan tertentu Prinsip Stak: Last In First Out = LIFO atau: First In Last Out = FILO Data yang masuk terkahirlah yang dikeluarkan.

Similar presentations


Presentation on theme: "Stack? Menyimpan data dalam susunan tertentu Prinsip Stak: Last In First Out = LIFO atau: First In Last Out = FILO Data yang masuk terkahirlah yang dikeluarkan."— Presentation transcript:

1

2 Stack? Menyimpan data dalam susunan tertentu Prinsip Stak: Last In First Out = LIFO atau: First In Last Out = FILO Data yang masuk terkahirlah yang dikeluarkan terlebih dahulu Contohnya

3 BABA DCBADCBA CBACBA DCBADCBA EDCBAEDCBA top A

4 ADT Stack public interface Stack { public boolean kosong(); public boolean penuh(); public Object nilaiTop(); public Object pop(); public void push(Object o); } public interface Stack { public boolean kosong(); public boolean penuh(); public Object nilaiTop(); public Object pop(); public void push(Object o); }

5 ArrayStack public class ArrayStack implements Stack{ private Object[]data; private int top = -1; private int kapasitas = 0; public ArrayStack(int size){} public int size(){} public boolean kosong(){} public boolean penuh(){} public Object top(){} public Object pop(){} public void push(Object o){} }

6 ArrayStack public ArrayStack(int size){ kapasitas = size; data = new Object[kapasitas]; } public int size(){return top+1;} public boolean kosong(){return (top<0);} public boolean penuh(){ return (top+1 == kapasitas); }

7 ArrayStack public Object top(){ Object dt = null; if (!kosong()) dt = data[top]; return dt; }  Baca data di top tetapi data tetap ada di top

8 ArrayStack public Object pop(){ Object hasil = null; if (!kosong()) { hasil = data[top]; data[top] = null; top--; } return hasil; }  Data pada posisi top di hapus

9 ArrayStack public void push(Object o){ if (!penuh()){ this.data[++top] = o; }

10 Parentheses Matching An expression, i.e.,[(2+3)*x+5]*2. Each “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])} incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: ( Stacks10

11 Parentheses Matching Algorithm Algorithm ParenMatch( X, n ) : Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i = 0 to n - 1 do if X [ i ] is an opening grouping symbol then S. push (X [ i ] ) else if X [ i ] is a closing grouping symbol then if S. isEmpty () then return false { nothing to match with } if S. pop () does not match the type of X [ i ] then return false { wrong type } if S. isEmpty () then return true { every symbol matched } else return false { some symbols were never matched } Stacks11

12 Parentheses Matching Example 1 Input: () (() [()]) i X[i] OperationStackOutput 0(Push (( 1)Pop ( Test if ( and X[i] match? YES 2(Push (( 3( (( 4)Pop ( Test if ( and X[i] match? YES ( 5[Push [([ Stacks12

13 Parentheses Matching Example 1 Input: () (() [()]) i X[i] OperationStackOutput 6(Push (([( 7)Pop ( Test if ( and X[i] match? YES ([ 8]Pop [ Test if [ and X[i] match? YES ( 9)Pop ( Test if ( and X[i] match? YES Test if stack is Empty? YES TRUE Stacks13

14 Parentheses Matching Example 2 Input: ( () [] ]() i X[i] OperationStackOutput 0(Push (( 1( (( 2)Pop ( Test if ( and X[i] match? YES ( 3[Push [([ 4]Pop [ Test if [ and X[i] match? YES ( 5]Pop ( Test if ( and X[i] match ? NOFASLE Stacks14

15


Download ppt "Stack? Menyimpan data dalam susunan tertentu Prinsip Stak: Last In First Out = LIFO atau: First In Last Out = FILO Data yang masuk terkahirlah yang dikeluarkan."

Similar presentations


Ads by Google