Академический Документы
Профессиональный Документы
Культура Документы
st.push('(');
}
//Pop all the remaining elements from the stack
while(st.top() != 'N')
{
char c = st.top();
st.pop();
ns += c;
}
Output:
abcd^e-fgh*+^*+i-
Any equation in the form "5 + ((1 + 2) × 4) − 3" is called Infix expression. The postfix expression of
this infix notation will be: "5 1 2 + 4 × + 3 −". It is also known as Reverse Polish notation.
Algorithm:
Repeat above two steps till all the characters are scanned.
After all characters are scanned, there will be only one element in the stack, and this is the
result of given expression.
C++ Implementation:
The following program evaluates a given postfix string. The numbers in inputs must be space
separated:
/*
* Postfix Evaluation
* Language: C++
*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <string.h>
int main()
{
char exp[1000], buffer[15];
int i,op1, op2, len, j, x;
stack<int> s;
printf("Enter a Postfix Expression: ( e.g. 23 34 * )\n");
gets(exp);
len = strlen(exp);
j = 0;
for(i=0; i<len;i++){
else if(isOperator(exp[i])){
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(performOperation(op1, op2, exp[i]));
}
}
return 0;
}
Insertion sort
#include<iostream>
int main()
{
int i,j,n,temp,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j]; //moves element forward
j=j-1;
}
return 0;
}
Selection Sort
#include <iostream>
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
SelectionSort(arr, n);
return 0;
}
Binary Search
#include <iostream>
using namespace std;
int main()
{
int count, i, arr[30], num, first, last, middle;
cout<<"how many elements would you like to enter?:";
cin>>count;
}
else if(arr[middle] == num)
{
cout<<num<<" found in the array at the location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<num<<" not found in the array";
}
return 0;
}
Output:
Linear Search
#include<iostream>
int main()
int a[20],n,x,i,flag=0;
for(i=0;i<n;++i)
cin>>a[i];
cin>>x;
for(i=0;i<n;++i)
if(a[i]==x)
flag=1;
break;
if(flag)
else
return 0;
}
Output
How many elements?4
Enter elements of the array
5 9 12 4
Enter element to search:9
Element is found at position 2
Bubble Sort
#include<iostream>
int main()
int a[50],n,i,j,temp;
cin>>n;
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
Output
Heap Sort
// Driver program
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, n);
Quick Sort
#include <iostream>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
int a[50],n,i;
for(i=0;i<n;i++)
cin>>a[i];
quick_sort(a,0,n-1);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
int j;
if(l<u)
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
temp=a[i];
a[i]=a[j];
a[j]=temp;
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
Output
How many elements?6
Enter array elements:9 15 6 7 10 12
Array after sorting:6 7 9 10 12 15