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.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks Chapter 11.
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Chapter 3 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks.
Stacks, Queues & Deques CSC212.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS2468: Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone: TA (Assignment.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks (and Queues).
Stacks Stacks.
Stacks.
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
תגבור פרונטלי 6 ברוכים הבאים מבוא למדעי המחשב 2018
Stacks.
Chapter 5 Stacks and Queues 11/28/2018 Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues 1.
Stacks Abstract Data Types (ADTs) Stacks
Stacks.
CS2468: Data Structures and Data Management
Stacks.
Stacks CS-240 Dick Steflik.
Stacks.
Presentation transcript:

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

BABA DCBADCBA CBACBA DCBADCBA EDCBAEDCBA top A

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); }

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){} }

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); }

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

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

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

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

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

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

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

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