Data Structure & Algorithms
Assignment 1
BS-SE-18
Section A
Name Waleed Qadeer
Roll no. 18094198-022
Submitted TO [Link] Fareed
Infix to Postfix
#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to convert Infix expression to postfix
string InfixToPostfix(string expression);
// Function to verify whether an operator has higher precedence over other
int HasHigherPrecedence(char operator1, char operator2);
// Function to verify whether a character is operator symbol or not.
bool IsOperator(char C);
// Function to verify whether a character is alphanumeric chanaracter (letter or numeric digit) or not.
bool IsOperand(char C);
int main()
string expression;
cout<<"Enter Infix Expression \n";
getline(cin,expression);
string postfix = InfixToPostfix(expression);
cout<<"Output = "<<postfix<<"\n";
// Function to evaluate Postfix expression and return output
string InfixToPostfix(string expression)
// Declaring a Stack from Standard template library in C++.
stack<char> S;
string postfix = ""; // Initialize postfix as empty string.
for(int i = 0;i< [Link]();i++) {
// Scanning each character from left.
// If character is a delimitter, move on.
if(expression[i] == ' ' || expression[i] == ',') continue;
// If character is operator, pop two elements from stack, perform operation and push the result back.
else if(IsOperator(expression[i]))
while(![Link]() && [Link]() != '(' && HasHigherPrecedence([Link](),expression[i]))
postfix+= [Link]();
[Link]();
[Link](expression[i]);
// Else if character is an operand
else if(IsOperand(expression[i]))
postfix +=expression[i];
else if (expression[i] == '(')
[Link](expression[i]);
else if(expression[i] == ')')
while(![Link]() && [Link]() != '(') {
postfix += [Link]();
[Link]();
[Link]();
while(![Link]()) {
postfix += [Link]();
[Link]();
return postfix;
// Function to verify whether a character is english letter or numeric digit.
// We are assuming in this solution that operand will be a single character
bool IsOperand(char C)
if(C >= '0' && C <= '9') return true;
if(C >= 'a' && C <= 'z') return true;
if(C >= 'A' && C <= 'Z') return true;
return false;
// Function to verify whether a character is operator symbol or not.
bool IsOperator(char C)
if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
return true;
return false;
// Function to verify whether an operator is right associative or not.
int IsRightAssociative(char op)
if(op == '$') return true;
return false;
// Function to get weight of an operator. An operator with higher weight will have higher precedence.
int GetOperatorWeight(char op)
int weight = -1;
switch(op)
{
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
case '$':
weight = 3;
return weight;
// Function to perform an operation and return output.
int HasHigherPrecedence(char op1, char op2)
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
// If operators have equal precedence, return true if they are left associative.
// return false, if right associative.
// if operator is left-associative, left one should be given priority.
if(op1Weight == op2Weight)
if(IsRightAssociative(op1)) return false;
else return true;
return op1Weight > op2Weight ? true: false;
Postfix to Prefix
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
#define flag '#'
using namespace std;
bool isOperator(char c)
if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^' || c=='$')
return true;
else
return false;
int main()
stack<char> stk;
char postfix[30], prefix[30];
int j=0,len;
cout<<"Input a postfix expression: ";
cin>>postfix;
len = strlen(postfix);
for(int i=len-1;i>=0;i--)
if(isOperator(postfix[i]))
[Link](postfix[i]);
else
prefix[j++] = postfix[i];
while(![Link]() && [Link]()==flag)
[Link]();
prefix[j++] = [Link]();
[Link]();
[Link](flag);
prefix[j] = 0;
reverse(prefix, prefix + len);
cout<<"The prefix expression is: "<<prefix;
return 0;
Prefix to Postfix
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <stdlib.h>
using namespace std;
bool isOperator(char symbol); //declaring function isOperator
int main()
ifstream myfile; //declaring streams
ofstream outfile;
[Link]("[Link]", ios::in); //opening streams
[Link]("[Link]", ios::out);
while (![Link]()) //while not end of input file
stack<char> operators;
stack<char> flags;
string line;
string outputLine;
getline(myfile, line); //read the next line of file and store into ‘line’
for(int i=0; i < [Link](); i ++) //reiterate through ‘line’
char symbol = line [i];
if( isOperator(symbol)) //if the symbol is an operator
[Link](symbol); //push operator on stack
[Link](0); //push associated flag on stack and set to off
}
if((symbol != ' ') && !isOperator(symbol)) //then it’s a operand
outputLine += symbol; //append to output string
//strcat(outputLine, symbol);
//[Link](symbol);
while([Link]()) //while top flag is ON on stack
outputLine += [Link]() ; //append the associated op
//strcat(outputLine, [Link]());
//[Link]([Link]());
[Link](); //remove operator from stack
[Link](); //remove flag from stack
[Link](); //set next flag to ON
[Link](1);
}//end of for
if(![Link]() || (![Link]()) )
outfile << "SOMETHING WENT WRONG. Prob incorrect input" << endl;
else
outfile << "Prefix: " << line << endl;
outfile << "Postfix: " << outputLine << endl;
} //end of while eof
[Link](); //close streams
[Link]();
}//end of main
bool isOperator(char symbol) //definition of isOperator
if( (symbol == '*') || (symbol == '+') || (symbol == '/') || (symbol == '-' ) )
return true;
else
return false;
Postfix to infix
#include<iostream>
#include<cstring>
using namespace std;
struct node
char data ;
node *next ;
};
class stack
private :
node *head;
public :
stack()
head = NULL;
void push(char a)
node * temp = new node();
temp ->data = a ;
temp -> next = head ;
head = temp ;
}
char pop()
node *temp = head ;
head = temp->next ;
char a = temp->data ;
delete temp;
return a;
char see_top()
if(is_empty())
return '0' ;
node * temp = head ;
return (temp->data);
int is_empty()
if(head == NULL)
return 1;
else
return 0 ;
};
int is_digit(char a)
if(a >= '0' && a<= '9')
return 1;
else
return 0;
}
int is_operand(char a)
switch(a)
case '+' :
case '-' :
case '*' :
case '/' : return 1;
default : return 0;
void reverse(char a[] , int l)
int i=0;
stack b ;
for(int j=0;j<l;j++)
[Link](a[j]);
while(!b.is_empty())
a[i++]=[Link]();
return ;
int main()
int i , l , j=0;
char exp[100] , infix[100];
stack s ;
cout << "Enter a postfix expression : ";
[Link](exp,100);
l=strlen(exp);
reverse(exp,l);
for(i=0;i<l;i++)
if(is_digit(exp[i]))
infix[j++] = exp[i];
if(i==l-1)
break ;
infix[j++] = [Link]() ;
else
[Link](exp[i]);
while(!s.is_empty())
infix[j++] =[Link]();
reverse(infix,l);
cout << "\nInfix is : " ;
for(i=0;i<l;i++)
cout << infix[i] ;
cout << endl;
return 0;
Recursion
1. #include <iostream>
2. using namespace std;
3.
4. int factorial(int);
5.
6. int main()
7. {
8. int n;
9. cout<<"Enter a number to find factorial: ";
10. cin >> n;
11. cout << "Factorial of " << n <<" = " << factorial(n);
12. return 0;
13. }
14.
15. int factorial(int n)
16. {
17. if (n > 1)
18. {
19. return n*factorial(n-1);
20. }
21. else
22. {
23. return 1;
24. }
25. }