0% found this document useful (0 votes)
34 views

Lab 6

The document summarizes the algorithm for converting an infix notation mathematical expression to postfix notation. It describes the steps of the algorithm which include scanning the infix expression from left to right, pushing operands and opening parentheses to a stack, and applying precedence rules to determine when to push or pop operators from the stack. It also provides examples of C++ functions and data structures that could be used to implement the algorithm, including an input function, conversion function, and push and pop functions for a stack.

Uploaded by

Jawad Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lab 6

The document summarizes the algorithm for converting an infix notation mathematical expression to postfix notation. It describes the steps of the algorithm which include scanning the infix expression from left to right, pushing operands and opening parentheses to a stack, and applying precedence rules to determine when to push or pop operators from the stack. It also provides examples of C++ functions and data structures that could be used to implement the algorithm, including an input function, conversion function, and push and pop functions for a stack.

Uploaded by

Jawad Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structure (and Algorithm)

Tanveer Ahmed Siddiqui

Department of Computer Science


COMSATS University, Islamabad
Outline

LAB 06
STACK and its Applications

Department of Computer Science


PUSH

ALGORITHM FOR PUSH OPERATION


Algorithm PUSH_A(ITEM)
Input: The new ITEM to be pushed onto it.
Output: A stack with newly pushed ITEM at the TOP position
Data Structure: An Array A with TOP as the indicator (pointer)
// For pushing the element on stack first we check the condition of overflow then push the elements
Steps:
1. IF TOP ≥ (SIZE -1) THEN
1.PRINT “STACK IS FULL”
2. Else
1. TOP = TOP + 1
2. A[TOP] = ITEM
3. EndIf
4. Stop
C++ function for PUSH Operation
void push( )
{
int pushed_item ;
if(top == (size -1))
cout<< " Stack Overflow"<<endl;
else
{
cout<< " Enter the item to be pushed in stack:";
cin>>pushed_item;
top = top +1;
stack_arr[top] = pushed_item;
Department
} of Computer Science 3
} /* End of Push() */
POP
ALGORITHM FOR POP OPERATION
Algorithm POP_A( )
Input: A stack with element.
Output: Remove an ITEM from TOP of the stack if it is not empty
Data Structure: An Array A with TOP as the indicator (pointer)
// For Pop the element from stack first we check the condition of underflow
then remove the elements
Steps:
1. IF TOP = -1 THEN
1.PRINT “STACK IS EMPTY”
2. Else
1. ITEM = A[TOP] TOP + 1
2. TOP =TOP - 1
3. EndIf
4. Stop
C++ function for POP Operation
Void pop()
{
if(top == -1)
cout<< “Stack under flow”;
else
{
cout<< “ Poped element is : Stack_arr[top]
top = top -1
}Department of Computer Science 4
}
Algorithm for Infix to Postfix conversion

Step 1.  Scan/read the infix expression from left to right

Step 2. If scan character is an operand, output it.

Step 3. If scan character is opening parenthesis, push it on stack.

Step 4. If scan character is an operator, then


i) If stack is empty, push operator on stack.
ii) If the top of stack is opening parenthesis, push operator on stack
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it, repeat step 4

Department of Computer Science


Algorithm for Infix to Postfix conversion

Step 5.   If scan character is a closing parenthesis, pop operators from


stack and output them until an opening parenthesis is encountered.
pop and discard the opening parenthesis.

Step 6.   If there is more input go to step 1

Step 7.   If there is no more input, pop the remaining operators and


add to output.

Department of Computer Science


How to Implement this
algorithm

Department of Computer Science


Required data/data structure and its type

 Input:
 An array of character to hold the infix expression
 char infix_expression[100];
 Stack:
 An array of character to hold operations
 char stack[20];
 Character Variable:
 To hold the individual character from infix expression
 char ch;
 Integer variable
 To point the top of the stack
 int top = -1;

Department of Computer Science


Function to implement algorithm

 Function that get input(infix expression from


user
 void input();
 Function that convert infix expression to postfix
expression
 void convert();
 Function that push operation onto stack
 void push();
 Function that remove item from stack
 void pop();

Department of Computer Science


Input ( ) function

Department of Computer Science


convert ( ) function

Department of Computer Science


convert ( ) function

Step 1.  Scan/read the infix expression from left to right


for(int i= 0; infix_expression[i]!='\0';i++)
{
ch =infix_expression[i];

Step 2. If scan character is an operand, output it.


cout<<ch<<" ";
Step 3. If scan character is opening parenthesis, push it on stack.

if(ch=='('||ch=='*'||ch=='/'||ch=='+'||ch=='-'||ch==')')
{
if(ch=='(')
push();

Department of Computer Science


Algorithm for Infix to Postfix conversion

Step 4. If scan character is an operator, then


i) If stack is empty, push operator on stack.
if(top==-1)
push();
ii) If the top of stack is opening parenthesis, push operator on stack

if(stack[top]=='(‘)
push();
iii) If it has higher priority than the top of stack, push operator on stack.
scan character Top of Stack Action
* + push
* + push
/ - push
/ - push

if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/’))
push();
Department of Computer Science
Algorithm for Infix to Postfix conversion

Step 4. If scan character is an operator, then


i) If stack is empty, push operator on stack.
if(top==-1)
push();
ii) If the top of stack is opening parenthesis, push operator on stack

if(stack[top]=='(‘)
push();
iii) If it has higher priority than the top of stack, push operator on stack.
if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/’))
push();
iv) Else pop the operator from the stack and output it, repeat step 4
else
{
pop();
push();
}
Department of Computer Science
Algorithm for Infix to Postfix conversion

Step 5.   If scan character is a closing parenthesis, pop operators from


stack and output them until an opening parenthesis is encountered.
pop and discard the opening parenthesis.

Step 6.   If there is more input go to step 1


} // end of for loop
Step 7.   If there is no more input, pop the remaining operators and add
to output. pop();
Department of Computer Science

You might also like