SlideShare a Scribd company logo
Chapter Five
Stack
1
• A simple data structure, in which insertion and
deletion occur at the same end, is termed a
stack.
• It is a LIFO (Last In First Out) structure.
• The operations of insertion and deletion are
called PUSH and POP respectively.
– Push - push (put) item onto stack
– Pop - pop (get) item from stack
2
3
TOS=>
4
1
3
6
Our Purpose:
• To develop a stack implementation that does not
tie us to a particular data type or to a particular
implementation.
Implementation:
• Stacks can be implemented both as an array
(contiguous list) and as a linked list.
• We want a set of operations that will work with
either type of implementation: i.e. the method of
implementation is hidden and can be changed
without affecting the programs that use them.
4
The Basic Operations:
Push()
{ if there is room {
put an item on the top of the stack }
else
give an error message
}
Pop()
{ if stack not empty {
return the value of the top item
remove the top item from the stack }
else {
give an error message }
}
CreateStack()
{ remove existing items from the stack
initialise the stack to empty
}
5
Array Implementation of Stacks: The PUSH operation
• Here, as you might have noticed, addition of an
element is known as the PUSH operation.
• So, if an array is given to you, which is supposed to act
as a STACK, you know that it has to be a STATIC Stack;
• i.e sdata will overflow if you cross the upper limit of
the array. So, keep this in mind.
Algorithm:
• Step-1: Increment the Stack TOP by 1. Check whether it
is always less than the Upper Limit of the stack.
If it is less than the Upper Limit go to step-2 else
report -"Stack Overflow“
• Step-2: Put the new element at the position pointed by
the TOP
6
static int stack[UPPERLIMIT];
int top= -1; /*stack is empty*/
..
main()
{
..
push(item);
..
}
push(int item)
{
top = top + 1;
if(top < UPPERLIMIT)
stack[top] = item; /*step-1 & 2*/
else
cout<<"Stack Overflow";
}
• Note:- In array implementation, we have taken TOP = -1 to signify
the empty stack, as this simplifies the implementation.
7
Array Implementation of Stacks: the POP operation
• POP is the synonym for delete when it comes to
Stack.
• So, if you're taking an array as the stack,
remember that you'll return an error message,
"Stack underflow", if an attempt is made to Pop
an item from an empty Stack. OK.
Algorithm
• Step-1: If the Stack is empty then give the alert
"Stack underflow" and quit; or else go to step-2
• Step-2:
a) Hold the value for the element pointed by the TOP
b) Put a NULL value instead
c) Decrement the TOP by 1
8
Implementation:
static int stack[UPPPERLIMIT];
int top=-1;
....
main()
{
poped_val = pop();
..
}
9
int pop()
{ int del_val = 0;
if(top == -1)
cout<<"Stack underflow"; /*step-1*/
else
{
del_val = stack[top]; /*step-2*/
stack[top] = NULL;
top = top -1; }
return(del_val);
}
• Note: - Step-2:(b) signifies that the respective element
has been deleted
Linked List Implementation of Stacks: the PUSH
operation
• It’s very similar to the insertion operation in a
dynamic singly linked list.
• The only difference is that here you'll add the
new element only at the end of the list, which
means addition can happen only from the
TOP.
• Since a dynamic list is used for the stack, the
Stack is also dynamic, means it has no prior
upper limit set.
• So, we don't have to check for the Overflow
condition at all!
10
• In Step [1] we create the new element to be
pushed to the Stack.
• In Step [2] the TOP most element is made to
point to our newly created element.
• In Step [3] the TOP is moved and made to point
to the last element in the stack, which is our
newly added element
11
Algorithm
• Step-1: If the Stack is empty go to step-2 or
else go to step-3
• Step-2: Create the new element and make
your "stack" and "top" pointers point to it and
quit.
• Step-3: Create the new element and make the
last (top most) element of the stack to point
to it
• Step-4: Make that new element your TOP
most element by making the "top" pointer
point to it.
12
Implementation:
struct node{
int item;
struct node *next;
}
struct node *stack = NULL;
/*stack is initially empty*/
struct node *top = stack;
main()
{
..
..
push(item);
..
} 13
push(int item)
{ node *newnode;
if(stack == NULL) /*step-1*/
{
newnode = new node;//step-2
newnode -> item = item;
newnode -> next = NULL;
stack = newnode;
top = stack;
}
else
{
newnode = new node;//step-3
newnode -> item = item;
newnode -> next = NULL;
top ->next = newnode;
top = newnode; /*step-4*/
}
}
Linked List Implementation of Stacks: the POP
Operation
• very similar to the deletion operation in any
Linked List,
– but you can only delete from the end of the list and
– only one at a time;
– and that makes it a stack.
• Here, we'll have a list pointer, "target", which
will be pointing to the last but one element in
the List (stack).
• Every time we POP, the TOP most element will
be deleted and "target" will be made as the TOP
most element.
14
• In step[1] we got the "target" pointing to the last but
one node.
• In step[2] we freed the TOP most element.
• In step[3] we made the "target" node as our TOP most
element.
• Supposing you have only one element left in the Stack,
then we won't make use of "target" rather we'll take
help of our "bottom" pointer. See how...
15
Algorithm:
Step-1: If the Stack is empty then give an alert
message "Stack Underflow" and quit; or else
proceed
Step-2: If there is only one element left go to step-
3 or else step-4
Step-3: Free that element and make the "stack",
"top" and "bottom" pointers point to NULL and
quit
Step-4: Make "target" point to just one element
before the TOP; free the TOP most element;
make "target" as your TOP most element
16
• Implementation:
struct node
{
int nodeval;
struct node *next;
}
struct node *stack = NULL;
/*stack is initially empty*/
struct node *top = stack;
main()
{
int newvalue, delval;
push(newvalue);
delval = pop(); /*POP returns
the deleted value from the
stack*/
}
17
int pop( )
{
int pop_val = 0;
struct node *target = stack;
if(stack == NULL) /*step-1*/
cout<<"Stack Underflow";
else
{
if(top == bottom) /*step-2*/
{
pop_val = top -> nodeval; /*step-3*/
delete top;
stack = NULL;
top = bottom = stack;
}
else /*step-4*/
{
while(target->next != top)
target = target ->next;
pop_val = top->nodeval;
delete top;
top = target;
top ->next = NULL;
}
}
return(pop_val);
}
Applications of Stacks
Evaluation of Algebraic Expressions
e.g. 4 + 5 * 5
• simple calculator: 45
• scientific calculator: 29 (correct)
Question:
• Can we develop a method of evaluating arithmetic
expressions without having to ‘look ahead’ or ‘look
back’? ie consider the quadratic formula:
x = (-b+(b^2-4*a*c)^0.5)/(2*a)
• where ^ is the power operator, or, as you may
remember it :
18
• In it’s current form we cannot solve the
formula without considering the ordering of
the parentheses. i.e. we solve the innermost
parenthesis first and then work outwards also
considering operator precedence.
• Although we do this naturally, consider
developing an algorithm to do the same . . . . .
. possible but complex and inefficient.
• Instead . . . .
19
Re-expressing the Expression
• Computers solve arithmetic expressions by restructuring them so the
order of each calculation is embedded in the expression.
• Once converted an expression can then be solved in one pass.
Types of Expression
• The normal (or human) way of expressing mathematical expressions
is called infix form, e.g. 4+5*5.
• However, there are other ways of representing the same expression,
either by writing all operators before their operands or after them,
e.g.: 4 5 5 * +
+ 4 * 5 5
• This method is called Polish Notation (because this method was
discovered by the Polish mathematician Jan Lukasiewicz).
• When the operators are written before their operands, it is called the
prefix form
e.g. + 4 * 5 5
• When the operators come after their operands, it is called postfix
form (suffix form or reverse polish notation)
e.g. 4 5 5 * +
20
The valuable aspect of RPN (Reverse Polish Notation or postfix )
• Parentheses are unnecessary
• Easy for a computer (compiler) to evaluate an arithmetic expression
Postfix (Reverse Polish Notation)
• Postfix notation arises from the concept of post-order traversal of
an expression tree (see Weiss p. 93 - this concept will be covered
when we look at trees).
• For now, consider postfix notation as a way of redistributing
operators in an expression so that their operation is delayed until
the correct time.
• Consider again the quadratic formula:
x = (-b+(b^2-4*a*c)^0.5)/(2*a)
• In postfix form the formula becomes:
x b @ b 2 ^ 4 a * c * - 0.5 ^ + 2 a * / =
• where @ represents the unary - operator.
• Notice the order of the operands remain the same but the operands
are redistributed in a non-obvious way (an algorithm to convert infix
to postfix can be derived).
21
Purpose
• The reason for using postfix notation is that a
fairly simple algorithm exists to evaluate such
expressions based on using a stack.
Postfix Evaluation
• Consider the postfix expression :
6 5 2 3 + 8 * + 3 + *
22
Algorithm
initialise stack to empty;
while (not end of postfix expression) {
get next postfix item;
if(item is value)
push it onto the stack;
else if(item is binary operator) {
pop the stack to x;
pop the stack to y;
perform y operator x;
push the results onto the stack; }
else if (item is unary operator) {
pop the stack to x;
perform operator(x);
push the results onto the stack }}
• The single value on the stack is the desired result.
23
• Binary operators: +, -, *, /, etc.,
• Unary operators: unary minus, square root, sin,
cos, exp, etc.,
• So for 6 5 2 3 + 8 * + 3 + *
• the first item is a value (6) so it is pushed onto the
stack
• the next item is a value (5) so it is pushed onto
the stack
• the next item is a value (2) so it is pushed onto
the stack
• the next item is a value (3) so it is pushed onto
the stack
24
• and the stack becomes
• the remaining items are now: + 8 * + 3 + *
• So next a '+' is read (a binary operator),
• so 3 and 2 are popped from the stack and
their sum '5' is pushed onto the stack:
25
3
2
5
6
• Next 8 is pushed and the next item is the operator *:
• Next the operator + followed by 3:
• Next is operator +, so 3 and 45 are popped and
45+3=48 is pushed
• Next is operator *, so 48 and 6 are popped, and
6*48=288 is pushed
• Now there are no more items and there is a single
value on the stack, representing the final answer 288.
• Note the answer was found with a single traversal of
the postfix expression, with the stack being used as a
kind of memory storing values that are waiting for their
operands.
26
Infix to Postfix (RPN) Conversion
• Of course postfix notation is of little use unless
there is an easy method to convert standard
(infix) expressions to postfix.
• Again a simple algorithm exists that uses a
stack:
27
Algorithm
initialise stack and postfix output to empty;
while(not end of infix expression) {
get next infix item
if(item is value)
append item to pfix o/p
else if(item == ‘(‘)
push item onto stack
else if(item == ‘)’) {
pop stack to x
while(x != ‘(‘)
app.x to pfix o/p & pop stack to x }
else {
while(precedence(stack top) >= precedence(item))
pop stack to x & app.x to pfix o/p }
push item onto stack }
while(stack not empty)
pop stack to x and append x to pfix o/p
28
• Operator Precedence (for this algorithm):
– 4 : ‘(‘ - only popped if a matching ‘)’ is found
– 3 : All unary operators
– 2 : / *
– 1 : + -
• The algorithm immediately passes values
(operands) to the postfix expression, but
remembers (saves) operators on the stack
until their right-hand operands are fully
translated.
29
• Eg., consider the infix expression
a+b*c+(d*e+f)*g
30
Stack Output
TOS=>
+ ab
TOS=>
*
+ abc
TOS=>
+ abc*+
Function Calls
• When a function is called, arguments (including the return
address) have to be passed to the called function.
• If these arguments are stored in a fixed memory area then
the function cannot be called recursively since the 1st
return address would be overwritten by the 2nd return
address before the first was used:
10 call function abc(); /* retadrs = 11 */
11 continue;
...90 function abc;
91 code;
92 if (expression)
93 call function abc(); /* retadrs = 94 */
94 code
95 return /* to retadrs
31
*/ A stack allows a new instance of retadrs for each
call to the function.
Recursive calls on the function are limited only by
the extent of the stack.
10 call function abc(); /* retadrs1 = 11 */
11 continue;
...90 function abc;
91 code;
92 if (expression)
93 call function abc(); /* retadrs2 = 94 */
94 code
95 return /* to retadrsn */
32
Assignment and Exercise
End!
33

More Related Content

PPTX
Stack and its applications
PPTX
Stack and its Applications : Data Structures ADT
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
PDF
Chapter 4 stack
PPTX
My lecture stack_queue_operation
PPSX
Stacks fundamentals
PPT
7 stacksqueues
PPTX
Stack in Sata Structure
Stack and its applications
Stack and its Applications : Data Structures ADT
Programming in java - Concepts- Operators- Control statements-Expressions
Chapter 4 stack
My lecture stack_queue_operation
Stacks fundamentals
7 stacksqueues
Stack in Sata Structure

What's hot (20)

PDF
stack
PPTX
Application of Stack - Yadraj Meena
DOCX
Stack - Operations and Applications
DOCX
(674335607) cs2309 java-lab-manual
PPT
Data Structure Lecture 2
PDF
Lecture 01 variables scripts and operations
PPT
Stacks
PDF
stacks and queues class 12 in c++
PPT
Unit 3 stack
PPTX
Stack converted
PPT
Stack linked list
PDF
New Functional Features of Java 8
PPTX
Java 8 presentation
PPT
(Ai lisp)
PDF
PPTX
ScalaDays 2013 Keynote Speech by Martin Odersky
stack
Application of Stack - Yadraj Meena
Stack - Operations and Applications
(674335607) cs2309 java-lab-manual
Data Structure Lecture 2
Lecture 01 variables scripts and operations
Stacks
stacks and queues class 12 in c++
Unit 3 stack
Stack converted
Stack linked list
New Functional Features of Java 8
Java 8 presentation
(Ai lisp)
ScalaDays 2013 Keynote Speech by Martin Odersky
Ad

Viewers also liked (14)

PPT
Chapter Seven(1)
PPT
Chapter Seven(2)
PDF
Run time storage
PPTX
Access to non local names
PPTX
Wordpress for Business Seminar_CD
PPT
Chapter Eight(3)
DOCX
Compiler Design Material
PDF
Seminar_Report_hadoop
PPSX
Introduction of wordpress, Wordpress Training Ahmedabad, Wordpress Class Ahme...
PPT
WordPress Complete Tutorial
PDF
WordPress SEO & Optimisation
KEY
Intro To Hadoop
PPTX
Big Data & Hadoop Tutorial
PPT
Seminar Presentation Hadoop
Chapter Seven(1)
Chapter Seven(2)
Run time storage
Access to non local names
Wordpress for Business Seminar_CD
Chapter Eight(3)
Compiler Design Material
Seminar_Report_hadoop
Introduction of wordpress, Wordpress Training Ahmedabad, Wordpress Class Ahme...
WordPress Complete Tutorial
WordPress SEO & Optimisation
Intro To Hadoop
Big Data & Hadoop Tutorial
Seminar Presentation Hadoop
Ad

Similar to Chapter 5-stack.pptx (20)

PPTX
Introduction to information about Data Structure.pptx
PPTX
The presentation on stack data structure
PPT
Stacks
PPT
Data structure lecture7
PPT
week 7,8,10,11 alll files included from .ppt
PPT
Stack in Data Structure
PPTX
DS UNIT 2 PPT.pptx stack queue representations
PPTX
Stack.pptx
PDF
Chapter 5 Stack and Queue.pdf
PPTX
DS-UNIT 3 FINAL.pptx
PPTX
Stack and its operations, Queue and its operations
PPTX
Stack and Queue.pptx university exam preparation
PPTX
Stacks and queues using aaray line .pptx
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
Data Structures Stack and Queue Data Structures
PPT
Stack data structures with definition and code
PDF
Data structure lab manual
PPTX
Stack_Overview_Implementation_WithVode.pptx
PDF
Stacks-and-Queues.pdf
PDF
Introduction to information about Data Structure.pptx
The presentation on stack data structure
Stacks
Data structure lecture7
week 7,8,10,11 alll files included from .ppt
Stack in Data Structure
DS UNIT 2 PPT.pptx stack queue representations
Stack.pptx
Chapter 5 Stack and Queue.pdf
DS-UNIT 3 FINAL.pptx
Stack and its operations, Queue and its operations
Stack and Queue.pptx university exam preparation
Stacks and queues using aaray line .pptx
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Data Structures Stack and Queue Data Structures
Stack data structures with definition and code
Data structure lab manual
Stack_Overview_Implementation_WithVode.pptx
Stacks-and-Queues.pdf

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
Project quality management in manufacturing
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Geodesy 1.pptx...............................................
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CYBER-CRIMES AND SECURITY A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Project quality management in manufacturing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Embodied AI: Ushering in the Next Era of Intelligent Systems
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Internet of Things (IOT) - A guide to understanding
Geodesy 1.pptx...............................................
573137875-Attendance-Management-System-original
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Sustainable Sites - Green Building Construction
CH1 Production IntroductoryConcepts.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx

Chapter 5-stack.pptx

  • 2. • A simple data structure, in which insertion and deletion occur at the same end, is termed a stack. • It is a LIFO (Last In First Out) structure. • The operations of insertion and deletion are called PUSH and POP respectively. – Push - push (put) item onto stack – Pop - pop (get) item from stack 2
  • 4. Our Purpose: • To develop a stack implementation that does not tie us to a particular data type or to a particular implementation. Implementation: • Stacks can be implemented both as an array (contiguous list) and as a linked list. • We want a set of operations that will work with either type of implementation: i.e. the method of implementation is hidden and can be changed without affecting the programs that use them. 4
  • 5. The Basic Operations: Push() { if there is room { put an item on the top of the stack } else give an error message } Pop() { if stack not empty { return the value of the top item remove the top item from the stack } else { give an error message } } CreateStack() { remove existing items from the stack initialise the stack to empty } 5
  • 6. Array Implementation of Stacks: The PUSH operation • Here, as you might have noticed, addition of an element is known as the PUSH operation. • So, if an array is given to you, which is supposed to act as a STACK, you know that it has to be a STATIC Stack; • i.e sdata will overflow if you cross the upper limit of the array. So, keep this in mind. Algorithm: • Step-1: Increment the Stack TOP by 1. Check whether it is always less than the Upper Limit of the stack. If it is less than the Upper Limit go to step-2 else report -"Stack Overflow“ • Step-2: Put the new element at the position pointed by the TOP 6
  • 7. static int stack[UPPERLIMIT]; int top= -1; /*stack is empty*/ .. main() { .. push(item); .. } push(int item) { top = top + 1; if(top < UPPERLIMIT) stack[top] = item; /*step-1 & 2*/ else cout<<"Stack Overflow"; } • Note:- In array implementation, we have taken TOP = -1 to signify the empty stack, as this simplifies the implementation. 7
  • 8. Array Implementation of Stacks: the POP operation • POP is the synonym for delete when it comes to Stack. • So, if you're taking an array as the stack, remember that you'll return an error message, "Stack underflow", if an attempt is made to Pop an item from an empty Stack. OK. Algorithm • Step-1: If the Stack is empty then give the alert "Stack underflow" and quit; or else go to step-2 • Step-2: a) Hold the value for the element pointed by the TOP b) Put a NULL value instead c) Decrement the TOP by 1 8
  • 9. Implementation: static int stack[UPPPERLIMIT]; int top=-1; .... main() { poped_val = pop(); .. } 9 int pop() { int del_val = 0; if(top == -1) cout<<"Stack underflow"; /*step-1*/ else { del_val = stack[top]; /*step-2*/ stack[top] = NULL; top = top -1; } return(del_val); } • Note: - Step-2:(b) signifies that the respective element has been deleted
  • 10. Linked List Implementation of Stacks: the PUSH operation • It’s very similar to the insertion operation in a dynamic singly linked list. • The only difference is that here you'll add the new element only at the end of the list, which means addition can happen only from the TOP. • Since a dynamic list is used for the stack, the Stack is also dynamic, means it has no prior upper limit set. • So, we don't have to check for the Overflow condition at all! 10
  • 11. • In Step [1] we create the new element to be pushed to the Stack. • In Step [2] the TOP most element is made to point to our newly created element. • In Step [3] the TOP is moved and made to point to the last element in the stack, which is our newly added element 11
  • 12. Algorithm • Step-1: If the Stack is empty go to step-2 or else go to step-3 • Step-2: Create the new element and make your "stack" and "top" pointers point to it and quit. • Step-3: Create the new element and make the last (top most) element of the stack to point to it • Step-4: Make that new element your TOP most element by making the "top" pointer point to it. 12
  • 13. Implementation: struct node{ int item; struct node *next; } struct node *stack = NULL; /*stack is initially empty*/ struct node *top = stack; main() { .. .. push(item); .. } 13 push(int item) { node *newnode; if(stack == NULL) /*step-1*/ { newnode = new node;//step-2 newnode -> item = item; newnode -> next = NULL; stack = newnode; top = stack; } else { newnode = new node;//step-3 newnode -> item = item; newnode -> next = NULL; top ->next = newnode; top = newnode; /*step-4*/ } }
  • 14. Linked List Implementation of Stacks: the POP Operation • very similar to the deletion operation in any Linked List, – but you can only delete from the end of the list and – only one at a time; – and that makes it a stack. • Here, we'll have a list pointer, "target", which will be pointing to the last but one element in the List (stack). • Every time we POP, the TOP most element will be deleted and "target" will be made as the TOP most element. 14
  • 15. • In step[1] we got the "target" pointing to the last but one node. • In step[2] we freed the TOP most element. • In step[3] we made the "target" node as our TOP most element. • Supposing you have only one element left in the Stack, then we won't make use of "target" rather we'll take help of our "bottom" pointer. See how... 15
  • 16. Algorithm: Step-1: If the Stack is empty then give an alert message "Stack Underflow" and quit; or else proceed Step-2: If there is only one element left go to step- 3 or else step-4 Step-3: Free that element and make the "stack", "top" and "bottom" pointers point to NULL and quit Step-4: Make "target" point to just one element before the TOP; free the TOP most element; make "target" as your TOP most element 16
  • 17. • Implementation: struct node { int nodeval; struct node *next; } struct node *stack = NULL; /*stack is initially empty*/ struct node *top = stack; main() { int newvalue, delval; push(newvalue); delval = pop(); /*POP returns the deleted value from the stack*/ } 17 int pop( ) { int pop_val = 0; struct node *target = stack; if(stack == NULL) /*step-1*/ cout<<"Stack Underflow"; else { if(top == bottom) /*step-2*/ { pop_val = top -> nodeval; /*step-3*/ delete top; stack = NULL; top = bottom = stack; } else /*step-4*/ { while(target->next != top) target = target ->next; pop_val = top->nodeval; delete top; top = target; top ->next = NULL; } } return(pop_val); }
  • 18. Applications of Stacks Evaluation of Algebraic Expressions e.g. 4 + 5 * 5 • simple calculator: 45 • scientific calculator: 29 (correct) Question: • Can we develop a method of evaluating arithmetic expressions without having to ‘look ahead’ or ‘look back’? ie consider the quadratic formula: x = (-b+(b^2-4*a*c)^0.5)/(2*a) • where ^ is the power operator, or, as you may remember it : 18
  • 19. • In it’s current form we cannot solve the formula without considering the ordering of the parentheses. i.e. we solve the innermost parenthesis first and then work outwards also considering operator precedence. • Although we do this naturally, consider developing an algorithm to do the same . . . . . . possible but complex and inefficient. • Instead . . . . 19
  • 20. Re-expressing the Expression • Computers solve arithmetic expressions by restructuring them so the order of each calculation is embedded in the expression. • Once converted an expression can then be solved in one pass. Types of Expression • The normal (or human) way of expressing mathematical expressions is called infix form, e.g. 4+5*5. • However, there are other ways of representing the same expression, either by writing all operators before their operands or after them, e.g.: 4 5 5 * + + 4 * 5 5 • This method is called Polish Notation (because this method was discovered by the Polish mathematician Jan Lukasiewicz). • When the operators are written before their operands, it is called the prefix form e.g. + 4 * 5 5 • When the operators come after their operands, it is called postfix form (suffix form or reverse polish notation) e.g. 4 5 5 * + 20
  • 21. The valuable aspect of RPN (Reverse Polish Notation or postfix ) • Parentheses are unnecessary • Easy for a computer (compiler) to evaluate an arithmetic expression Postfix (Reverse Polish Notation) • Postfix notation arises from the concept of post-order traversal of an expression tree (see Weiss p. 93 - this concept will be covered when we look at trees). • For now, consider postfix notation as a way of redistributing operators in an expression so that their operation is delayed until the correct time. • Consider again the quadratic formula: x = (-b+(b^2-4*a*c)^0.5)/(2*a) • In postfix form the formula becomes: x b @ b 2 ^ 4 a * c * - 0.5 ^ + 2 a * / = • where @ represents the unary - operator. • Notice the order of the operands remain the same but the operands are redistributed in a non-obvious way (an algorithm to convert infix to postfix can be derived). 21
  • 22. Purpose • The reason for using postfix notation is that a fairly simple algorithm exists to evaluate such expressions based on using a stack. Postfix Evaluation • Consider the postfix expression : 6 5 2 3 + 8 * + 3 + * 22
  • 23. Algorithm initialise stack to empty; while (not end of postfix expression) { get next postfix item; if(item is value) push it onto the stack; else if(item is binary operator) { pop the stack to x; pop the stack to y; perform y operator x; push the results onto the stack; } else if (item is unary operator) { pop the stack to x; perform operator(x); push the results onto the stack }} • The single value on the stack is the desired result. 23
  • 24. • Binary operators: +, -, *, /, etc., • Unary operators: unary minus, square root, sin, cos, exp, etc., • So for 6 5 2 3 + 8 * + 3 + * • the first item is a value (6) so it is pushed onto the stack • the next item is a value (5) so it is pushed onto the stack • the next item is a value (2) so it is pushed onto the stack • the next item is a value (3) so it is pushed onto the stack 24
  • 25. • and the stack becomes • the remaining items are now: + 8 * + 3 + * • So next a '+' is read (a binary operator), • so 3 and 2 are popped from the stack and their sum '5' is pushed onto the stack: 25 3 2 5 6
  • 26. • Next 8 is pushed and the next item is the operator *: • Next the operator + followed by 3: • Next is operator +, so 3 and 45 are popped and 45+3=48 is pushed • Next is operator *, so 48 and 6 are popped, and 6*48=288 is pushed • Now there are no more items and there is a single value on the stack, representing the final answer 288. • Note the answer was found with a single traversal of the postfix expression, with the stack being used as a kind of memory storing values that are waiting for their operands. 26
  • 27. Infix to Postfix (RPN) Conversion • Of course postfix notation is of little use unless there is an easy method to convert standard (infix) expressions to postfix. • Again a simple algorithm exists that uses a stack: 27
  • 28. Algorithm initialise stack and postfix output to empty; while(not end of infix expression) { get next infix item if(item is value) append item to pfix o/p else if(item == ‘(‘) push item onto stack else if(item == ‘)’) { pop stack to x while(x != ‘(‘) app.x to pfix o/p & pop stack to x } else { while(precedence(stack top) >= precedence(item)) pop stack to x & app.x to pfix o/p } push item onto stack } while(stack not empty) pop stack to x and append x to pfix o/p 28
  • 29. • Operator Precedence (for this algorithm): – 4 : ‘(‘ - only popped if a matching ‘)’ is found – 3 : All unary operators – 2 : / * – 1 : + - • The algorithm immediately passes values (operands) to the postfix expression, but remembers (saves) operators on the stack until their right-hand operands are fully translated. 29
  • 30. • Eg., consider the infix expression a+b*c+(d*e+f)*g 30 Stack Output TOS=> + ab TOS=> * + abc TOS=> + abc*+
  • 31. Function Calls • When a function is called, arguments (including the return address) have to be passed to the called function. • If these arguments are stored in a fixed memory area then the function cannot be called recursively since the 1st return address would be overwritten by the 2nd return address before the first was used: 10 call function abc(); /* retadrs = 11 */ 11 continue; ...90 function abc; 91 code; 92 if (expression) 93 call function abc(); /* retadrs = 94 */ 94 code 95 return /* to retadrs 31
  • 32. */ A stack allows a new instance of retadrs for each call to the function. Recursive calls on the function are limited only by the extent of the stack. 10 call function abc(); /* retadrs1 = 11 */ 11 continue; ...90 function abc; 91 code; 92 if (expression) 93 call function abc(); /* retadrs2 = 94 */ 94 code 95 return /* to retadrsn */ 32