CB_STACKS

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

STACKS

• It’s a linear data structure


• LIFO- Last In First Out
• E.g.
– Undo operation in word document
5 Main Functions
• Empty
• Size
• Top
• Push
• Pop
Header file

#include<stack>
1. Define the name and type of data

stack<int>numbersStack;

Type of data you NAME OF THE STACK


want to store
EMPTY FUNCTION
if(numbers.Stack.empty())
cout<<“Stack is empty”;
else
cout<<“Stack is not empty”;
SIZE FUNCTION
• To print the size of the stack
• numbersStack.size()
• Code
cout<<“Stack size is:”<<numbersStack.size();
Push Function

• numbersStack.push(1);

Value you want to enter


Pop Function

numbers.Stack(pop);
Printing the elements of Stack
• void printStackElements(Stack<int> stack)
{
while(!stack.empty())
{
cout<<Stack.top()<<endl;
stack.pop();
}
}
Invoking the function

printStackElements(numbersStack);
POLISH NOTATIONS
• INFIX EXPRESSION

• PREFIX EXPRESSION

• POSTFIX EXPRESSION
PRECEDENCE OF OPERATORS
• PARANTHESIS
• EXPONENT
• MULTIPLICATION AND DIVISION
• ADDITION AND SUBSTRACTION
Polish Notation
• Infix Notation: Operator symbol is placed between
the two operands.

Example:
(5 * 3) + 2 & 5 * (3 + 2)
Polish Notation
 Polish Notation: The Operator Symbol is placed before
its two operands.

• Also Known as Prefix Notation.

• Named after the Polish Mathematician Jan L..

• The order in which the operations are to be performed is


completely determined by the positions of operators and
operands in the expression.
Examples
• (A + B) * C = ___________

• A + (B * C) = ___________

• (A + B) / (C - D) = ____________
Reverse Polish Notation
 Reverse Polish Notation: The Operator Symbol is placed after
its two operands.

Example: A B+, C D*, P Q/ etc.

• Also known as Postfix Notation.


• Consider the following arithmetic
expression and convert it into postfix
notation and then solve the postfix
notation using stack:-

5*(6+2) – 12/4

You might also like