Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

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

Presentasi serupa


Presentasi berjudul: "Lecture 5 Stack Eka, Erick, Reddy © Sekolah Tinggi Teknik Surabaya 1."— Transcript presentasi:

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

2 » 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

3 PUSH

4 © Sekolah Tinggi Teknik Surabaya POP

5 » 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

6 » 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

7 » 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

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

9 » 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

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

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

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

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

14 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

15 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()

16 » 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

17 » 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

18 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

19 » 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

20 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

22 » 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

23 » 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 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 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

26 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 27 © Sekolah Tinggi Teknik Surabaya +-*/^digit()$ +>><<<<<>> ->><<<<<>> *>>>><<<>> />>>><<<>> ^>>>><<<>> Digit>>>>>Error >> (<<<<<<<=> )>>>>> >> $<<<<<<< Stack Char

28 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/+

29 » 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

30 » 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

31 » 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

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

33 » 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


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

Presentasi serupa


Iklan oleh Google