27
Data Structure Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

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

Embed Size (px)

Citation preview

Data Structure

Lecture 5Stack

Sandy Ardianto & Erick Pranata© 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.

Stack

© Sekolah Tinggi Teknik Surabaya

2

» 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

Push and Pop Operations

© Sekolah Tinggi Teknik Surabaya

3

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

Other Operations of Stack» 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

Stack Implementation» In most high level languages, a stack can

easily implemented either through an array or a linked list.

6

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Array» 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

Stack Implementation Using Arraypublic class STACK {const int STACK_SIZE = 1000;

public int top; public int[] items(STACK_SIZE);

8

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Arraypublic STACK() { top = -1;}

9

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Arraypublic bool IsEmpty() {return s.top == -1;

}

public bool IsFull() {return s.top == STACKSIZE – 1;

}

10

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Arraypublic void Push(int x) {if (IsFull(s)) { throw

new Exception("stack overflow");} else { top = top + 1; items(top) = x;}

}

11

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Arraypublic int Pop() { if (IsEmpty()) { throw

new Exception("stack underflow");} else { int data = items(top); top = top – 1; return data;}

}

12

© Sekolah Tinggi Teknik Surabaya

Stack Implementation Using Arraypublic 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()

The Stack<T> Class» The stack data structure

˃ Elements are from the same type T˃ T can be any type, e.g. Stack<int> ˃ 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

The Stack<T> Class» 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

Stack<T> – Examplestatic void Main(){ Stack<string> stack = new Stack<string>(); 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

Stack<T> – Matching Brackets» 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

Stack<T> – Matching Bracketsstring expression = "1 + (2 - (2+3) * 4 / (3+1)) * 5";Stack<int> stack = new Stack<int>();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

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

Expression Notations» There are three notations of

expressions: infix, prefix, and postfix.

Infix : 1 + 5 * 3 + 2 1 + (2 + 3)Prefix : + + 1 * 5 3 2 + 1 + 2 3Postfix : 1 5 3 * + 2 + 1 2 3 + +

» Infix can contain parentheses, but prefix and postfix cannot (never need parentheses).

20

© Sekolah Tinggi Teknik Surabaya

Calculation of postfix» 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

© Sekolah Tinggi Teknik Surabaya

22

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

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

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

Stack Applications2 ) 156 02 ) 78 02 ) 39 12 ) 19 12 ) 9 12 ) 4 02 ) 2 02 ) 1 1 0

26

© Sekolah Tinggi Teknik Surabaya

Pop all elements from the stack to get a sequence of binary digits.

10011100

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