Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
by
Nor Bahiah Hj Ahmad
Software Engineering Department, FSKSM
bahiah@utm.my
Course Objectives
At the end of the lesson students are
expected to be able to:
Understand stack concepts and the
structure.
Understand operations that can be done
on stack.
Understand and know how to implement
stack using array and linked list.
2
SCSJ 2013
Introduction to Stack
Example of Stack
Reverse a string
Example: TOP , Reverse: POT
SCSJ 2013
5
SCSJ 2013
What Is Stack?
6
SCSJ 2013
7
SCSJ 2013
Stack implementation
Stack is an abstract data structure
Item can be Integer, Double, String, and
also can be any data type, such as
Employee, Student
How to implement a general stack for all
those types?
We can implement stack using array or
linked list.
8
SCSJ 2013
top = 2
SCSJ 2013
stack
empty
SCSJ 2013
stack
empty
11
12
SCSJ 2013
13
SCSJ 2013
2.
SCSJ 2013
SCSJ 2013
16
SCSJ 2013
before push()
after push()
18
SCSJ 2013
Before pop()
after pop()
20
SCSJ 2013
21
SCSJ 2013
else
return data[top];
} // end stackTop
22
SCSJ 2013
SCSJ 2013
SCSJ 2013
25
SCSJ 2013
27
SCSJ 2013
push() operations
2 conditions for inserting element in stack:
Insert to empty stack.
Insert item to non empty stack : stack with
value.
28
SCSJ 2013
29
SCSJ 2013
Push() operations
void stack::push(int newitem)
{ // create newnode
nodeStack *newnode;
newnode = new (nodeStack);
if( newnode == NULL)
cout << Cannot allocate memory << endl;
else // add to empty stack, or to front stack
{ newnode->data = newitem;
newnode->next = head;
head = newnode;
}// end if
} //end push operation
31
SCSJ 2013
Pop() operation
void stack::pop()
{ nodeStack *delnode;
if (isEmpty())
cout <<Sorry, Cannot pop item from
stack.Stack is still empty! <<endl;
else
{ delnode = head;
cout << Item to be popped from stack
is: << stackTop()<<endl;
head = delnode->next;
delete(delnode);
}// end else
} // end pop
33
SCSJ 2013
34
SCSJ 2013
createStack(),push(),pop()
stackTop(),isEmpty(),isFull()
35
SCSJ 2013
Next..Stack Application
SCSJ 2013
SCSJ 2013
40
SCSJ 2013
Push(()
Push(()
(
Pop()
0
Pop()
Push(()
Push(()
(
Pop()
Pop()
Pop()->fail
Algebraic expression
One of the compilers task is to evaluate
algebraic expression.
Example of assignment statement:
y=x+z*(w/x+z*(7+6))
Compiler must determine whether the right
expression is a syntactically legal algebraic
expression before evaluation can be done on
the expression.
3 algebraic expressions are :
Infix, prefix and postfix
43
SCSJ 2013
Infix Expression
Example 2:
A+B*C
To evaluate infix expression,
the following rules were applied:
1.
2.
3.
Precedence rules.
Association rules (associate from left to right)
Parentheses rules.
44
SCSJ 2013
45
SCSJ 2013
46
SCSJ 2013
STEP 2
47
SCSJ 2013
48
SCSJ 2013
STEP 2
49
SCSJ 2013
50
SCSJ 2013
51
SCSJ 2013
52
SCSJ 2013
53
SCSJ 2013
stack
postfix
54
SCSJ 2013
stack
postfix
55
SCSJ 2013
3.
4.
56
SCSJ 2013
57
SCSJ 2013
result
stack
58
SCSJ 2013
result
stack
59
SCSJ 2013
Conclusion and
The End..
SCSJ 2013
November 2012
bahiah@utm.my