Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Similar presentations


Presentation on theme: "Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."— Presentation transcript:

1 Lecture 5 Stack Sandy Ardianto & Erick Pranata © 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 » 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 3

4 » 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. 4 © Sekolah Tinggi Teknik Surabaya

5 » 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. 5 © Sekolah Tinggi Teknik Surabaya

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

7 » 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. 7 © Sekolah Tinggi Teknik Surabaya

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

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

10 public bool IsEmpty() { return s.top == -1; } public bool IsFull() { return s.top == STACKSIZE – 1; } 10 © Sekolah Tinggi Teknik Surabaya

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

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

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

14 » 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 14 © Sekolah Tinggi Teknik Surabaya

15 » 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 15 © Sekolah Tinggi Teknik Surabaya

16 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); } 16 © Sekolah Tinggi Teknik Surabaya

17 » 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 17 © Sekolah Tinggi Teknik Surabaya

18 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); } 18 © Sekolah Tinggi Teknik Surabaya

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

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

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

22 22

23 » 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). 23 © Sekolah Tinggi Teknik Surabaya

24 » 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 ) 156 0 78 24 © Sekolah Tinggi Teknik Surabaya

25 » 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 0. 25 © Sekolah Tinggi Teknik Surabaya

26 2 ) 156 0 2 ) 78 0 2 ) 39 1 2 ) 19 1 2 ) 9 1 2 ) 4 0 2 ) 2 0 2 ) 1 1 0 26 © Sekolah Tinggi Teknik Surabaya Pop all elements from the stack to get a sequence of binary digits. 10011100

27 » Write an essay describing source code which is used to calculate infix notation. ˃Submit the printed version @ my office. ˃Due: Friday, 11 April 2014 at 5 pm. » Inappropriate essay will be returned unmarked. 27 © Sekolah Tinggi Teknik Surabaya


Download ppt "Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."

Similar presentations


Ads by Google