Lecture 5 Stack Eka, Erick, Reddy © Sekolah Tinggi Teknik Surabaya 1.

Slides:



Advertisements
Presentasi serupa
STACK (Tumpukan) Tumpukan Koin Tumpukan Kotak.
Advertisements

1 Algoritma Bahasa Pemrograman dan Bab 1.1. Pengertian Algoritma.
STRUKTUR DATA PERTEMUAN 5
Lecture 2 Introduction to C# - Object Oriented Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 9 Single Linked List Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Function.
Modul-8 : Algoritma dan Struktur Data
Pertemuan Struktur Data *Pohon Ekspresi *
STACK (TUMPUKAN).
Struktur Organisasi Data 2
STRUKTUR DATA version STMIK AMIKOM YOGYAKARTA
STRUKTUR DATA (4) Array Stack(Tumpukkan) dan Queue (Antrian)
Struktur Data Stack.
Linear List Teknik Informatika Universitas Muhammadiyah Malang SP Algoritma & Struktur Data.
Dasar Pemrograman Java Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008.
Sistem – Sistem Bilangan, Operasi dan kode
Lecture 5 Nonblocking I/O and Multiplexing Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
ARRAY RUBY. PENDAHULUAN Ruby's arrays are untyped and mutable. The elements of an array need not all be of the same class, and they can be changed at.
Algorithm and Data Structures.
Linear Data Structures (Stack)
1 DATA STRUCTURE “ STACK” SHINTA P STMIK MDP APRIL 2011.
stack ==tumpukan== Tenia wahyuningrum st3 telkom purwokerto
STRUKTUR DATA (4) array stack dan queue
Struktur Data List Linear : Linked List (Single Linkedlist)
1 Diselesaikan Oleh KOMPUTER Langkah-langkah harus tersusun secara LOGIS dan Efisien agar dapat menyelesaikan tugas dengan benar dan efisien. ALGORITMA.
Dasar Pemrograman Java Pertemuan 2 Pemrograman Berbasis Obyek.
Matakuliah : T0534/Struktur Data Tahun : 2005 Versi : September 2005
METHOD, ARRAY DAN STRING
Apakah Stack itu ?. Apakah Stack itu ? Pengertian STACK Secara sederhana diartikan dengan : sebagai tumpukan dari benda sekumpulan data yang seolah-olah.
Pertemuan 7 stack jual [Valdo] Lunatik Chubby Stylus.
BAB 3 STACK (TUMPUKAN).
STACK.
Stack Pertemuan 11.
1 Pertemuan 15 Game Playing Matakuliah: T0264/Intelijensia Semu Tahun: Juli 2006 Versi: 2/1.
Mata kuliah :K0362/ Matematika Diskrit Tahun :2008
Bayu Priyambadha, S.Kom.  Classes, which are the "blueprints" for an object and are the actual code that defines the properties and methods.  Objects,
9.3 Geometric Sequences and Series. Objective To find specified terms and the common ratio in a geometric sequence. To find the partial sum of a geometric.
Binary Search Tree. Sebuah node di Binary Search Tree memiliki path yang unik dari root menurut aturan ordering – Sebuah Node, mempunyai subtree kiri.
OPERATOR DAN FUNGSI MATEMATIK. Operator  Assignment operator Assignment operator (operator pengerjaan) menggunakan simbol titik dua diikuti oleh tanda.
The Three Principle Cooking Methods Dry Heat Moist Heat Combination Method.
Pengantar Teknik Kompilasi
Universitas Budi Luhur
Binary Tree.
Binary Tree.
HTML BASIC (Contd…..) PERTEMUAN KEDUA.
07/11/2017 BARISAN DAN DERET KONSEP BARISAN DAN DERET 1.
Sapta Candra Miarsa,S.T.,M.T.
STACK (Tumpukan) Tumpukan Koin Tumpukan Kotak.
Struktur Data Stack Oleh Lutfi Budi Ilmawan
Abstract Data Type (ADT) and Stack Array
Dynamic Array and Linked List
STACK (Tumpukan) Tumpukan Koin Tumpukan Kotak.
Matakuliah : T0534/Struktur Data Tahun : 2005 Versi : September 2005
Konsep pemrograman LOOP
STACK Denny Agustiawan,M.pd
Fondasi Pemrograman & Struktur Data
Linear Data Structures (Stack)
STACK 6.3 & 7.3 NESTED LOOP.
Dasar-Dasar Pemrograman
Linear Data Structures (Array)
Parabola Parabola.
Pertemuan 24 Teknik Searching
Algorithms and Programming Searching
REAL NUMBERS EKSPONENT NUMBERS.
STRUKTUR DATA version STMIK AMIKOM YOGYAKARTA
STRUKTUR DATA Stack atau Tumpukan.
Master data Management
Linked List A group of data which is linked each other.
Draw a picture that shows where the knife, fork, spoon, and napkin are placed in a table setting.
Transcript presentasi:

Lecture 5 Stack Eka, Erick, Reddy © Sekolah Tinggi Teknik Surabaya 1

» In computer science, a stack is a last-in first- out (LIFO) data structure. » A stack can have any data type as an element, but is characterized by only two fundamental operations, the push and pop. © Sekolah Tinggi Teknik Surabaya 2

PUSH

© Sekolah Tinggi Teknik Surabaya POP

» The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. ˃Push is implemented by a procedure. » The pop operation removes an item from the top of the stack, and returns this value to the caller. ˃Pop is implemented by a function © Sekolah Tinggi Teknik Surabaya 5

» Stack was first proposed in 1955, and then patented in 1957, by the German Friedrich L. Bauer. » The same concept was developed independently, at around the same time, by the Australian Charles Leonard Hamblin. 6 © Sekolah Tinggi Teknik Surabaya

» In modern computer languages, the stack is usually implemented with more operations than just push and pop. » Some implementations have a function which returns the current length of the stack. » Another typical helper operation, peek, can return the current top element of the stack without removing it from the stack. 7 © Sekolah Tinggi Teknik Surabaya

» In most high level languages, a stack can easily implemented either through an array or a linked list. 8 © Sekolah Tinggi Teknik Surabaya

» The array implementation aims to create an array where the zero-offset position is the bottom. That is, array[0] is the bottom. » The program keeps track of which offset index corresponds to the top. This index changes as the stack shrinks and grows in size. 9 © Sekolah Tinggi Teknik Surabaya

public class STACK { const int STACK_SIZE = 1000; public int top; public int[] items(STACK_SIZE); // © Sekolah Tinggi Teknik Surabaya

public STACK() { top = -1; } 11 © Sekolah Tinggi Teknik Surabaya

public bool IsEmpty() { return top == -1; } public bool IsFull() { return top == STACK_SIZE – 1; } 12 © Sekolah Tinggi Teknik Surabaya

public void Push(int x) { if (IsFull()) { throw new Exception("stack overflow"); } else { top = top + 1; items(top) = x; } } 13 © Sekolah Tinggi Teknik Surabaya

public int Pop() { if (IsEmpty()) { throw new Exception("stack underflow"); } else { int data = items(top); top = top – 1; return data; } } 14 © Sekolah Tinggi Teknik Surabaya

public int Pop() { if (IsEmpty()) { throw new Exception("stack underflow"); } else { int data = items(top); top = top – 1; return data; } } 15 © Sekolah Tinggi Teknik Surabaya Peek()

» The stack data structure ˃Elements are from the same type T ˃T can be any type, e.g. Stack ˃Size is dynamically increased as needed » Basic functionality: ˃Push(T) – inserts elements to the stack ˃Pop() – removes and returns the top element from the stack 16 © Sekolah Tinggi Teknik Surabaya

» Basic functionality: ˃Peek() – returns the top element of the stack without removing it ˃Count – returns the number of elements ˃Clear() – removes all elements ˃Contains(T) – determines whether given element is in the stack ˃ToArray() – converts the stack to an array ˃TrimExcess() – sets the capacity to the actual number of elements 17 © Sekolah Tinggi Teknik Surabaya

static void Main() { Stack stack = new Stack (); stack.Push("1. Ivan"); stack.Push("2. Nikolay"); stack.Push("3. Maria"); stack.Push("4. George"); Console.WriteLine("Top = {0}", stack.Peek()); while (stack.Count > 0) { string personName = stack.Pop(); Console.WriteLine(personName); } 18 © Sekolah Tinggi Teknik Surabaya

» We are given an arithmetical expression with brackets that can be nested » Goal: extract all sub-expressions in brackets » Example: ˃1 + (2 - (2+3) * 4 / (3+1)) * 5 » Result: ˃(2+3) | (3+1) | (2 - (2+3) * 4 / (3+1)) » Algorithm: ˃For each '(' push its index in a stack ˃For each ')' pop the corresponding start index 19 © Sekolah Tinggi Teknik Surabaya

string expression = "1 + (2 - (2+3) * 4 / (3+1)) * 5"; Stack stack = new Stack (); for (int index = 0; index < expression.Length; index++) { char ch = expression[index]; if (ch == '(') { stack.Push(index); } else if (ch == ')') { int startIndex = stack.Pop(); int length = index - startIndex + 1; string contents = expression.Substring(startIndex, length); Console.WriteLine(contents); } 20 © Sekolah Tinggi Teknik Surabaya

» Calculator ˃Convert an expression (infix notation) into a postfix notation. ˃Calculate a postfix expression. ˃These two processes need a stack. 21 © Sekolah Tinggi Teknik Surabaya

» There are three notations of expressions: infix, prefix, and postfix. Infix: * (2 + 3) Prefix: * Postfix: * » Infix can contain parentheses, but prefix and postfix cannot (never need parentheses). 22 © Sekolah Tinggi Teknik Surabaya

» Push when encountering an operand. » Pop two operands and evaluate the value when encountering an operator, and then push the result. 23 © Sekolah Tinggi Teknik Surabaya

24 © Sekolah Tinggi Teknik Surabaya * / + 3 * 546 Hasil = 5.8 Jika bertemu operator, POP operand 2x, (4) dan (5). Operasikan kemudian PUSH hasilnya. Ternyata bertemu dengan operator lagi (+), maka POP 2x, yaitu (20) dan (3),. Operasikan dan PUSH hasilnya ke stack Setelah itu lanjutkan kembali dengan potongan karakter yang lain. +/ Kembali bertemu dengan operator (/), maka POP 2 operand, dan operasikan. + Terakhir, bertemu dengan (+), maka POP 2x, dan operasikan.

25 © Sekolah Tinggi Teknik Surabaya + Setelah bertemu operand (5), bertemu lagi dengan operand (4), maka POP hingga ketemu operator (4), (5), (*)  dioperasikan jadi 20, di PUSH ke stack lagi + 2 / + 3 * Ternyata lagi! Operand berjajar dengan operand! Maka POP hingga ketemu operator (20), (3), (+)  dioperasikan jadi 23, di PUSH ke stack lagi 23 Sudah tidak bertumpuk lagi operand dengan operand. Maka lanjutkan PUSH potongan yang berikutnya (6) 6 Ternyata bertumpuk lagi antara (23) dan (6). Maka POP hingga bertemu operator, yaitu (/) 3.8 (3.8) bertumpuk lagi dengan (2), maka POP hingga ketemu operator (+), operasikan, dan PUSH hasilnya ke stack / + 3 * 54 6 Hasil = 5.8

Postfix  “” PUSH(“$”) READ(karakter) DO IF (topOfStack < karakter) OR (topOfStack = karakter) THEN PUSH(karakter) READ(karakter) ELSE IF (topOfStack > karakter) THEN DO last  POP Postfix  Postfix + last WHILE topOfStack > last ELSE ERROR WHILE(topOfStack <> “$”) OR (karakter <> “$”) RETURN Postfix 26 © Sekolah Tinggi Teknik Surabaya

27 © Sekolah Tinggi Teknik Surabaya +-*/^digit()$ +>><<<<<>> ->><<<<<>> *>>>><<<>> />>>><<<>> ^>>>><<<>> Digit>>>>>Error >> (<<<<<<<=> )>>>>> >> $<<<<<<< Stack Char

28 © Sekolah Tinggi Teknik Surabaya StackCharInputPostfix 2 + (3 + 5 * 4) / 6 $ $ Adding “$” $ 2 + (3 + 5 * 4) / 6 $2 + (3 + 5 * 4) / 6 $ $22 + $ +( * 4) / 6 $ ( $ + (3 + 5 * 4) / 6 $ 3 $ + ( * 4) / 6 $2 3+ $ + ( +5 * 4) / 6 $ 5 $ + ( +5* 4) / 6 $2 3 5* $ + ( + * 4 ) / 6 $ 4 $ + ( 4) / 6 $ ** + ) $ + (/ 6 $ ) * + / $ + ( / 6 $ 6 $ +(/ 6 $ * + 6/+

» Conversion from a decimal (base-10) number system to another number system ˃Examples: from decimal to binary (base-2), from decimal to octal (base-8), and from decimal to hexadecimal (base-16). ˃Suppose to convert the decimal number 156 to binary (base-2). 29 © Sekolah Tinggi Teknik Surabaya

» Write the integer answer (quotient) under the long division symbol, and write the remainder (0 or 1) to the right of the dividend. This remainder must be pushed into a stack. 2 ) © Sekolah Tinggi Teknik Surabaya

» Continue downwards, dividing each new quotient by two and writing the remainders to the right of each dividend (pushing the remainders into a stack). Stop when the quotient is © Sekolah Tinggi Teknik Surabaya

2 ) ) ) ) ) ) ) ) © Sekolah Tinggi Teknik Surabaya Pop all elements from the stack to get a sequence of binary digits

» Write an source code which is used to convert infix to postfix notation. ˃ to: ˃Subject: SD3_XXXXXXXXX ˃Due: Friday, 26 September 2014 at 11:59 PM. » Inappropriate essay will be returned unmarked. 33 © Sekolah Tinggi Teknik Surabaya