You are on page 1of 69

Algorithm and Data Structures 2

Third Slide, Stack


Thomas Anung Basuki and Nirma Handewi Informatics Dept., Unpar

ASD 2 - Stack p. 1

What is a Stack?
a data structure whose elements can only be accessed from the top

ASD 2 - Stack p. 2

What is a Stack?
a data structure whose elements can only be accessed from the top insertion and deletion of its top element

ASD 2 - Stack p. 2

What is a Stack?
a data structure whose elements can only be accessed from the top insertion and deletion of its top element LIFO: Last In First Out

ASD 2 - Stack p. 2

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack

ASD 2 - Stack p. 3

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat

ASD 2 - Stack p. 3

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish

ASD 2 - Stack p. 3

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it

ASD 2 - Stack p. 3

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it put it on top of the clean dishes stack

ASD 2 - Stack p. 3

Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it put it on top of the clean dishes stack until no more dirty dishes

ASD 2 - Stack p. 3

Stack as a Data Structure


Push(e): insert e into the top of stack

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty Top(): getting the value of the topmost element of a stack

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty Top(): getting the value of the topmost element of a stack Internally a stack should have

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty Top(): getting the value of the topmost element of a stack Internally a stack should have a list of elements

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty Top(): getting the value of the topmost element of a stack Internally a stack should have a list of elements pointer to the top of stack

ASD 2 - Stack p. 4

Stack as a Data Structure


Push(e): insert e into the top of stack Pop(): taking the topmost element of a stack out isEmpty(): checking whether the stack is empty Top(): getting the value of the topmost element of a stack Internally a stack should have a list of elements pointer to the top of stack Stack overow: a condition in which the number of elements pushed into a stack is more than its capacity
ASD 2 - Stack p. 4

Operations on Stack
isEmpty() = true

ASD 2 - Stack p. 5

Operations on Stack
isEmpty() = true false Push(0)

ASD 2 - Stack p. 5

Operations on Stack
isEmpty() = true false Push(0) Push(1) 1 0

ASD 2 - Stack p. 5

Operations on Stack
isEmpty() = true false
ll Y 1  $ 2 @

Push(0) Push(1) Pop() = 1

ASD 2 - Stack p. 5

Operations on Stack
isEmpty() = true true false
l l Y 1  $ 2 @ ll Y 0  $ 2 @

Push(0) Push(1) Pop() = 1 Pop() = 0

ASD 2 - Stack p. 5

Stack Implementation
Java has provided stack class

ASD 2 - Stack p. 6

Stack Implementation
Java has provided stack class We can still implement our own stack for special purpose

ASD 2 - Stack p. 6

Stack Implementation
Java has provided stack class We can still implement our own stack for special purpose linked list vs. array

ASD 2 - Stack p. 6

An Array-based Implementation of Stack


class Stack{ private int NMax, N; private int content[]; public Stack(); public Stack(int); public bool isEmpty(); public void Push(int); public int Pop(); public int Top(); public bool isFull(); }

ASD 2 - Stack p. 7

Example 1: Reversal
sequentially read all characters of a string and push them to a stack

ASD 2 - Stack p. 8

Example 1: Reversal
sequentially read all characters of a string and push them to a stack pop the stack and output the character until the stack is empty

ASD 2 - Stack p. 8

Example 1: Reversal
sequentially read all characters of a string and push them to a stack pop the stack and output the character until the stack is empty e.g. checking palindrome

ASD 2 - Stack p. 8

Example 2: Matching delimiters


Compilers need to check matching delimiters such as [ and ], { and }, ( and )

ASD 2 - Stack p. 9

Example 2: Matching delimiters


Compilers need to check matching delimiters such as [ and ], { and }, ( and ) These delimiters may be nested e.g. if ((i < n) && (a[i] != x)) {a[i]++;}

ASD 2 - Stack p. 9

Example 2: Matching delimiters


Compilers need to check matching delimiters such as [ and ], { and }, ( and ) These delimiters may be nested e.g. if ((i < n) && (a[i] != x)) {a[i]++;}
Output error message if non-matching delimiters are found e.g. if (a[i < x)}

ASD 2 - Stack p. 9

Example 2: Matching delimiters


Compilers need to check matching delimiters such as [ and ], { and }, ( and ) These delimiters may be nested e.g. if ((i < n) && (a[i] != x)) {a[i]++;}
Output error message if non-matching delimiters are found e.g. if (a[i < x)}

ASD 2 - Stack p. 9

Example 3: Calculator
Binary vs. unary operators binary: +, -, *, /, unary: sin, cos, tan, ln, exp Operator precedence must be respected

ASD 2 - Stack p. 10

Inx Example 1
3*5+4

ASD 2 - Stack p. 11

Inx Example 1
3*5+4

ASD 2 - Stack p. 11

Inx Example 1
3*5+4

ASD 2 - Stack p. 11

Inx Example 1
3*5+4

5 3 *

ASD 2 - Stack p. 11

Inx Example 1
3*5+4 Precedence of + is lower than precedence of * 5 3 *

ASD 2 - Stack p. 11

Inx Example 1
3*5+4

15

ASD 2 - Stack p. 12

Inx Example 1
3*5+4

4 15 +

ASD 2 - Stack p. 12

Inx Example 1
19

ASD 2 - Stack p. 13

Inx Example 2
3+5*4

ASD 2 - Stack p. 14

Inx Example 2
3+5*4

ASD 2 - Stack p. 14

Inx Example 2
3+5*4

ASD 2 - Stack p. 14

Inx Example 2
3+5*4

5 3 +

ASD 2 - Stack p. 14

Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 5 3 +

ASD 2 - Stack p. 14

Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 5 3 * +

ASD 2 - Stack p. 14

Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 4 5 3 * +

ASD 2 - Stack p. 14

Inx Example 2

20 3 +

ASD 2 - Stack p. 15

Inx Example 2
23
20  $ 2 @ 3  $ 2 @

ASD 2 - Stack p. 15

Inx Example 3
3 * (5 + 4)

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4)

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4)

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4)

( 3 *

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4)

5 3

( *

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( 5 3 ( *

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( + 5 3 ( *

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( 4 5 3 + ( *

ASD 2 - Stack p. 16

Inx Example 3
3 * (5 + 4) Pop and operate until ( is found 4 5 3 + ( *

ASD 2 - Stack p. 17

Inx Example 3
3 * (5 + 4) Pop and operate until ( is found 4 9 5 3 + ( *

ASD 2 - Stack p. 17

Inx Example 3

9 3 *

ASD 2 - Stack p. 18

Inx Example 3
27
9  $ 2 @ 3  $ 2 @  $ 2 @ *  $ 2 @

ASD 2 - Stack p. 18

Example 4: Prex Notation


The common notation used for arithmetic operations is called inx notation, where the operator is between the two operands Alternative notation: prex notation, where the operator precedes the two operands also known as Polish notation, invented by Polish logician Jan Lukasiewicz in the 1920s Inx 1 * (3 + 5) 3 + 5 * 4 - (10 * 5) (1 * 3) / (4 - 50 / 5) Prex *1+35 - + 3 * 5 4 * 10 5 / * 1 3 - 4 / 50 5
ASD 2 - Stack p. 19

Postx Notation
Another alternative notation: postx notation, where the two operands precede the operator As also the case in prex notation, parantheses are not needed in postx notation also known as Reverse Polish notation, invented by Australian philosopher and computer scientist Charles Hamblin in the mid 1950s Inx 1 * (3 + 5) 3 + 5 * 4 - (10 * 5) (1 * 3) / (4 - 50 / 5) Postx 135+* 3 5 4 * + 10 5 * 1 3 * 4 50 5 / - /
ASD 2 - Stack p. 20

Exercises
1. If symbol * represents POP and other symbols represent PUSH, then write down the ouput of POP operations of this sequence: P*U*S*H*DA*N**POP***. 2. Insert symbol * into STACK in order to output the following sequences: (or show that the sequence is impossible to output) STACK KCATS TACKS TCKAS AKTCS KCSTA
ASD 2 - Stack p. 21

Exercises (2)
3. Reverse the content of a stack with the help of 2 additional stacks with the help of another stack and a few variables 4. Sort (output) the content of a stack with the help of 2 additional stacks with the help of another stack and a few variables

ASD 2 - Stack p. 22

Exercises (3)
5. Transform an inx expression to postx using a stack 6. Transform an inx expression to prex using two stacks 7. Dene functions (push, pop, top, isEmpty) for two stacks implemented in an array such that no collision may occur

ASD 2 - Stack p. 23

You might also like