Mr.
Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
I.A.U. of Shabestar
Computer Engineering Dep.
Data Structures
(Session 9: Stack)
http://www.iaushab.ac.ir/prof/challenger/DS/index.htm
Instructor and Lecturer: M.Challenger
[email protected]
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 1/31
Contents
• Definition & Specification
• ADT
• Implementation
• Applications
• Multi-Stack
• Railway Exchange
• Evaluating Algebraic Statement
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 2/31
1
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Definition & Specification
• Sorted list of data items which its Entrance and Exit is the same
• Top of stack is the only place to use stack
• Technique: LIFO (Last In First Out) or FILO (First In Last out)
• If D1 and D2 enters consequently then
D2 is on the TOP of D1 in stack (so, D1 is under)
• Sample: System Stack => To implement function calls => like Recursive
• Stack: Pile of data with only one open-side to use it
4 3
In Out
Top
2
1
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 3/31
Implementation
1. Static: Using static ADT like Array
2. Dynamic: Using Dynamic ADT like Linked-List
Note: having static & Dynamic implementation leads to call
stack as Semi-Dynamic ADT
Array Implementation of Stack:
• An array with N item to implement a Stack with length of N
• Type of array shows Stack storing item’s type
• A var. called Top (Top of Stack)
• Top points to a exact position of last item
• To use all N item in stack array let Top=-1
– It is L-1 for an array starting with L
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 4/31
2
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Stack ADT
Stack{
Type Stack [Max];
int Top = -1;
----------------------------------
stack Create (Type, Size);
bool Is_Empty();
bool Is_Full();
void Push (item);
item Pop();
item Top();
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 5/31
Implementation
• Init: Top= -1 // In C, In other lang. L-1
• Empty Top ==-1
• Full Top == N-1
• Push => When Is_Full() != True
• Pop => When Is_Empty() != True
• Top => When Is_Empty() != True
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 6/31
3
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Implementation: Is_Empty
0 1 2 3 4 5 6 7 8
Stack
Top = -1
bool Is_Empty (){
if (Top = = -1) return TRUE
else return FALSE
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 7/31
Implementation: Is_Full
0 1 2 3 4 5 6 7 8
A B C D E F G H I
Stack
Top = 8
bool Is_Full (){
if (Top = = N-1) return TRUE
else return FALSE
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 8/31
4
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Implementation: Push
0 1 2 3 4 5 6 7 8
A B C D E F
Stack
void Push (item){ Top = 5
if (Is_Full()) Stack_Full_Error();
else {
Top++;
Stack[Top]=item;
// Stack[++Top]=item;
}
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 9/31
Implementation: Pop
0 1 2 3 4 5 6 7 8
A B C D E F
Stack
Top = 5
item Pop (){
if (Is_Empty()) Stack_Empty_Error();
else {
T = Stack[Top];
Top--;
return T;
// return Stack[Top--];
}
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 10/31
5
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Implementation: Top
0 1 2 3 4 5 6 7 8
A B C D E F
Stack
Top = 5
item Top (){
if (Is_Empty()) Stack_Empty();
else
return Stack[Top];
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 11/31
Exercises 1
Do 5 assignments in Exercises 1 to write
several Stack ADT’s
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 12/31
6
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Multi-Stack Def.
• N stack in array with M item
• Top and Bottom should be saved for each stack
• T[ ] array keeps top of all stacks
• B[ ] array keeps bottom of all stacks
• T[i] shows top of stack i
• B[i] shows bottom of stack i
– first data of stack i is in next item of B[i]
• Init: B[i]=T[i]=[m/n](i-1) - 1
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 13/31
Multi-Stack: Structure
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
(M/N) (M/N) (M/N)
Stack 1 Stack 2 Stack N
T[1] T[2] T[3] T[N]
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 14/31
7
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Multi-Stack ADT
Multi-Stack{
Type Stack [Max];
int T[i] = B[i] = [M/N](i-1) -1;
----------------------------------
stack Create (Type, Size, Number);
bool Is_Empty(i);
bool Is_Full(i);
void Push (i, item);
item Pop(i);
item Top(i);
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 15/31
Multi-Stack: Precondition
• Init: B[i]=T[i]=[m/n](i-1) - 1
• Empty: T[i] = = B[i]
• Full: T[i] = = B[i+1]
• Push => When Is_Full(stack i) != True
• Pop => When Is_Empty(stack i) != True
• Top => When Is_Empty(stack i) != True
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 16/31
8
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Multi-Stack: Imp. of Is_Empty
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
(M/N) (M/N) (M/N)
Stack 1 Stack 2 Stack N
T[1] T[2] T[3] T[N]
bool Is_Empty(i){
if (T[i] = = B[i]) return TRUE;
else return FALSE
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 17/31
Multi-Stack: Imp. of Is_Full
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
A B C D 1 2
(M/N)
Stack N
T[1] T[2] T[3] T[N]
bool Is_Full(i){
if (T[i] = = B[i+1]) return TRUE;
else return FALSE
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 18/31
9
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Multi-Stack: Imp. of Push
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
A B 1 2
(M/N)
Stack N
T[1] T[2] T[3] T[N]
void Push (i, item){
if (Is_Full(i)) Stack_Full_Error(i);
else {
T[i]++;
Stack[T[i]] = item;
}
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 19/31
Multi-Stack: Imp. of Pop
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
A B 1 2
(M/N)
Stack N
T[1] T[2] T[3] T[N]
item Pop (i){
if (Is_Empty(i)) Stack_Empty_Error(i);
else { temp = Stack [T[i]];
T[i]--;
return temp;
}
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 20/31
10
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Multi-Stack: Imp. of Top
B[1] B[2] B[3] B[N]
0 1 2 3 4 5 6 7 M-1
A B 1 2
(M/N)
Stack N
T[1] T[2] T[3] T[N]
item Top (i){
if (Is_Empty(i)) Stack_Empty(i);
else return Stack [T[i]];
}
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 21/31
Miscellaneous
Stack Items No:
– Simple Stack: No= Top+1
– Multi-Stack: No= T[i]-B[i]
– All items in Multi-Stack: ∑(T[i]-B[i])
Applications:
• System Stack => Function call
• Recursion Problems
• Railway exchange
• Algebraic Statement Evaluation
• Problems like: Maze, …
• …
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 22/31
11
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Railway Exchange
3, 2, 4, 1, 5, … 1, 2, 3, 4, 5, …
Test for output: 4, 5, 3, 2, 1, …
Check output: 3, 1, 4, 5, 2, …
Exercise 2: Find a relation to impossible cases for a1, a2, a3, … input
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 23/31
Algebraic Statements’ Evaluation
• A*B => A, B are operands & The * is operator
• Algebraic Statements’ Presentation:
1. Infix (In=Between): operator Between Operands=>a+b
2. Postfix (Post = after): Operator after operands => ab+
3. Prefix (Pre=Before): Operator before Operands=>+ab
• Infix: Math, Human, but not Computer since priority
– Needs Parenthesis
• Post and Prefix: Used in Computer, but are not easy to
understand for human. They do not need Parenthesis.
• Pre is the most popular and is called
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 24/31
12
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Infix=>Prefix
1. Fully Parenthesis (clear priority)
2. Relate ( to Operators
3. Start from left to right
A. Replace ( with its related operator
B. Copy operands in its turn
C. Pass over ) and operators
Infix: a + b * ( c – d ) / 2
1) (a+((b*(c–d))/2))
2)
3) +a/*b-cd2
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 25/31
Infix=>Postfix
• Fully Parenthesis (clear priority)
• Relate ) to Operators
• Start from left to right
– A. Replace ) with its related operator
– B. Copy operands in its turn
– C. Pass over ( and operators
• Infix: a + b * ( c – d ) / 2
• 1) (a+((b*(c–d))/2))
• 2)
• 3) abcd-*2/+
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 26/31
13
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Postfix=>Infix (value)
1. Start from left to Right
– Push Operands
– For Operators, pop enough items b
=> (a*b)
a
– Calculate items & operation and push it again
*
2. At the end, the only item in the Stack is answer
=>
Ex: abcd-*2/+ (a+((b*(c–d))/2))
c d 2
c (c-d) (b*(c-d)) (b*(c-d)) ((b*(c-d))/2)
b b b b
a a a a a a a a
a b c d - * 2 / +
====>
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 27/31
Prefix=>Infix (value)
1. Start from Right to Left
– Push Operands
– For Operators, pop enough items b
=> (b*a)
– Calculate items & operation and push it again a
2. At the end, the only item in the Stack is answer *
=>
Ex: +a/*b-cd2
(a+((b*(c–d))/2))
c b
d d (c-d) (c-d) (b*(c-d)) a
2 2 2 2 2 2 ((b*(c-d))/2) ((b*(c-d))/2)
2 d c - b * / a +
====>
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 28/31
14
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Exercises 3
Using Stack:
Write 5 functions to do HW’s in Exercises 3
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 29/31
Next Agendas
• Linked-List & its applications
• Determining Next Sessions Presenter
• Reminding Next Sessions dues (Proj & HW)
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 30/31
15
Mr. Challenger’s Data-Structures Lecture-Notes
(Session 9: Stack & its applications)
Thank you!
Any Question?
12:15:11 AM, Sunday, November 26, 2006 <IAU of Shabestar, M.Challenger, Data Structures> Slide no.: 31/31
16