Topic 5 - Stack
Topic 5 - Stack
Topic 5 : Stack
Stack Definition
Stack Operations
Stack Application
Stack Implementation
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
Check if parentheses match, and output pairs (u,v) such that
the left parenthesis at position u is matched with the right
parenthesis at position v
(2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
(a+b))*((c+d)
(0,4)
Error: right parenthesis at 5 has no matching left parenthesis
(8,12)
Error: left parenthesis at 7 has no matching right parenthesis
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
2
1
0
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 (2,6) (1,13)
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
15
0 (2,6) (1,13)
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
21
0 (2,6) (1,13) (15,19)
0 1 2 6 13 15 19 21 25 27 31 32 34 38
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
import java.util.Stack;
Push
top
public void push(int elem) {
newStack
if (!isFull())
newStack[top++] = elem;
else
System.out.println("Full Stack");
}
May use
StackOverflowException
top
newStack
Compiled & edited by: Zahid Zainal
Stack Operations (cont.)
Pop
top
public int pop() {
newStack
if (!isEmpty())
return(newStack[--top]);
else {
System.out.println("Empty Stack");
return 0;
}
}
May use
StackUnderflowException
top
newStack
Compiled & edited by: Zahid Zainal
Testing
Queue
Concept
Application
Implementation