Abstract Data Type - Sekolah Tinggi Teknik Surabaya

Download Report

Transcript Abstract Data Type - Sekolah Tinggi Teknik Surabaya

1
Lecture 5
Stack
Sandy Ardianto & Erick Pranata
© Sekolah Tinggi Teknik Surabaya
» 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
» 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
3
© Sekolah Tinggi Teknik Surabaya
» 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
» 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.
© Sekolah Tinggi Teknik Surabaya
5
» In most high level languages, a stack
can easily implemented either through
an array or a linked list.
6
© Sekolah Tinggi Teknik Surabaya
» 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
public class STACK {
const int STACK_SIZE = 1000;
public int top;
public int[] items(STACK_SIZE);
8
© Sekolah Tinggi Teknik Surabaya
public STACK() {
top = -1;
}
9
© Sekolah Tinggi Teknik Surabaya
public bool IsEmpty() {
return s.top == -1;
}
public bool IsFull() {
return s.top == STACKSIZE – 1;
}
10
© Sekolah Tinggi Teknik Surabaya
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
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
Peek() {
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
» 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
» 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
© Sekolah Tinggi Teknik Surabaya
15
static 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
» 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
© Sekolah Tinggi Teknik Surabaya
17
string 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);
}
}
© Sekolah Tinggi Teknik Surabaya
18
» 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
» There are three notations of
expressions: infix, prefix, and postfix.
Infix
:1+5*3+2
Prefix : + + 1 * 5 3 2
Postfix : 1 5 3 * + 2 +
1 + (2 + 3)
+1+23
123++
» Infix can contain parentheses, but prefix
and postfix cannot (never need
parentheses).
© Sekolah Tinggi Teknik Surabaya
20
» 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
© Sekolah Tinggi Teknik Surabaya
» 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
» 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
78
0
24
© Sekolah Tinggi Teknik Surabaya
» 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
2 ) 156
2 ) 78
2 ) 39
2 ) 19
2) 9
2) 4
2) 2
2) 1
0
0
0
1
1
1
0
0
1
Pop all elements from the
stack to get a sequence of
binary digits.
10011100
26
© Sekolah Tinggi Teknik Surabaya
» 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