You are on page 1of 89

DSA - Practical

1] A] Array using switch case:

#include <iostream>
using namespace std;
class ArrayDemo
{
public:
int arr[10];
int n;
void get();
void insert();
void display();
void Delete();
void search();
};
void ArrayDemo::get()
{
cout << "Enter the number of elements you want to give\n";
cin >> n;
cout << "\nEnter array element\n";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
}
void ArrayDemo::insert()
{
int num, loc;
cout << "\nEnter element for insert :";
cin >> num;
cout << "\nEnter the position:";
cin >> loc;
for (int i = n; i >= loc; i--)
{
arr[i] = arr[i - 1];
}
n++;
arr[loc ] = num;
ArrayDemo::display();
}
void ArrayDemo::Delete()
{
int item, k;
cout << "\nEnter the position you want to delete :";
cin >> k;
//k = k - 1;
item = arr[k];
for (int i = k; i <= n - 1; i++)
{
arr[i] = arr[i + 1];
}
n = n - 1;
ArrayDemo::display();
}
void ArrayDemo::search()
{
int ele;
cout << "\nEnter the element to be searched :";
cin >> ele;
for (int i = 0; i <= n; i++)
{
if (arr[i] == ele)
{
cout << "Element is found at position: " << i;
break;
}
else if (i == n)
{
cout << "\nElement is not found";
break;
}
}
}
void ArrayDemo::display()
{
for (int i = 0; i < n; i++)
{
cout << " " << arr[i];
}
}
int main()
{
int ch;
ArrayDemo aobj;
aobj.get();
do
{
cout << "\nEnter your choice" << endl;
cout << "\n1:insert...."
<< "\n"
<< "2:delete..."
<< "\n"
<< "3:search.."
<< "\n"
<< "4:Exit..";
cin >> ch;
switch (ch)
{
case 1:
aobj.insert();
break;
case 2:
aobj.Delete();
break;
case 3:
aobj.search();
break;
case 4:
cout << "Exit";
break;
}
} while (ch != 4);
return 0;
}
/*
OUTPUT:
Enter the number of elements you want to give
5

Enter array element


2
7
5
3
4

Enter your choice

1:insert....
2:delete...
3:search..
4:Exit..1

Enter element for insert :9

Enter the position:5


275349
Enter your choice

1:insert....
2:delete...
3:search..
4:Exit..2

Enter the position you want to delete :4


27539
Enter your choice

1:insert....
2:delete...
3:search..
4:Exit..3

Enter the element to be searched :7


Element is found at position: 1
Enter your choice

1:insert....
2:delete...
3:search..
4:Exit..
*/
B] Multidimensional Arrays ,Matrices:
#include <iostream>
using namespace std;
class matrices
{
int a[10][10],b[10][10],d[10][10],mul[10][10],r,c,i,j,k;
public:
void get();
void addition();
void subtraction();
void multilpication();
};
void matrices::get()
{
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
}void matrices::addition()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
d[i][j] = a[i][j] + b[i][j];
}
}
cout<<"Addition of two matrices are \n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<" "<<d[i][j];
}
cout<<"\n";
}
}
void matrices::subtraction()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)

d[i][j] = a[i][j] - b[i][j];

cout<<"Subtraction of two matrices are \n";

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

cout<<" "<<d[i][j];

cout<<"\n";

void matrices::multilpication()

cout<<"multiply of the matrix=\n";

for(i=0;i<r;i++)

for(j=0;j<c;j++)

mul[i][j]=0;

for(k=0;k<c;k++)

mul[i][j]+=a[i][k]*b[k][j];

cout<<"\n";

}
//this method used for printing the result

for(i=0;i<r;i++)

for(j=0;j<c;j++)

cout<<" "<<mul[i][j]<<" ";

cout<<"\n";

int main()

int ch;

matrices s;

while(1)

cout<<"\n 1. for Insert";

cout<<"\n 2. For addition of two matrices";

cout<<"\n 3. For subtraction of two matrices";

cout<<"\n 4. For multiplication of two matrices";

cout<<"\n 5. For Exit";

cout<<"\n Enter the Choice:";

cin>>ch;

switch(ch)

case 1:s.get();

break;
case 2:s.addition();
break;

case 3:s.subtraction();

break;

case 4:s.multilpication();

break;

case 5: exit(0);

s.get();

return 0;

/*

Output:

1. for Insert

2. For addition of two matrices

3. For subtraction of two matrices

4. For multiplication of two matrices

5. For Exit

Enter the Choice:1

enter the number of row=2

enter the number of column=2

enter the first matrix element:

enter the second matrix element=

4
2

1. for Insert

2. For addition of two matrices

3. For subtraction of two matrices

4. For multiplication of two matrices

5. For Exit

Enter the Choice:2

Addition of two matrices are

55

84

1. for Insert

2. For addition of two matrices

3. For subtraction of two matrices

4. For multiplication of two matrices

5. For Exit

Enter the Choice:3

Subtraction of two matrices are

-1 1

00

1. for Insert

2. For addition of two matrices

3. For subtraction of two matrices

4. For multiplication of two matrices

5. For Exit

Enter the Choice:4

multiply of the matrix=


18 10

20 12

1. for Insert

2. For addition of two matrices

3. For subtraction of two matrices

4. For multiplication of two matrices

5. For Exit

Enter the Choice:

*/

C] a] Linear Search:
#include <iostream>

using namespace std;

class ArrayDemo

public:

int arr[10];

int n,x,i;

void get();

void linear_search();

};

void ArrayDemo::get()

cout << "Enter the number of elements you want to give\n";

cin >> n;

cout << "\nEnter array element\n";

for (int i = 0; i < n; i++)

cin >> arr[i];


}

void ArrayDemo::linear_search()

cout << "\nEnter element to search: \n";

cin >> x;

for (i = 0; i < n; i++)

if (arr[i] == x)

break;

if (i < n)

cout << "\nElement found at index: "<<i;

else

cout << "Element not found";

int main()

ArrayDemo aobj;

aobj.get();

aobj.linear_search();

/*OUTPUT:

Enter the number of elements you want to give


5

Enter array element

12

32

43

54

21

Enter element to search:

32

Element found at index: 1*/


b] Binary Search

#include <iostream>

using namespace std;

class ArrayDemo

public:

int arr[10];

int n, key, low, high, mid;

void get();

void binary_search();

};

void ArrayDemo::get()

cout << "Enter the number of elements you want to give\n";

cin >> n;

cout << "\nEnter array element\n";

for (int i = 0; i < n; i++)

cin >> arr[i];

void ArrayDemo::binary_search()

cout << "Enter the value to find: \n";

cin >> key;

low = 0;

high = n - 1;

mid = (low + high) / 2;

while (low <= high)


{

if (arr[mid] < key)

low = mid + 1;

else if (arr[mid] == key)

cout << "Element found at location: " << mid;

break;

else

high = mid - 1;

mid = (low + high) / 2;

if (low > high)

cout << "Not found!!!";

int main()

ArrayDemo obj;

obj.get();

obj.binary_search();

/*OUTPUT:

Enter the number of elements you want to give

Enter array element

21

32

43

54

65
Enter the value to find:

43

Element found at location: 2*/

2] A] Bubble Sort

#include<iostream>

using namespace std;

class Bsort{

public:

int a[10];

int n;

void get();

void sort();

void display();

};

void Bsort::get()

cout<<"No of element yo want\n";

cin>>n;

cout<<"\n Enter array elements\n";

for(int i=0;i<n;i++)

cin>>a[i];

void Bsort::sort()

int temp;

for(int i=0;i<n;i++)

for(int j=0; j<n-i-1;j++)


{

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

void Bsort::display()

for(int i=0;i<n;i++)

cout<<a[i]<<" ";

int main()

Bsort obj;

obj.get();

obj.sort();

obj.display();

/*OUTPUT:

No of element yo want

Enter array elements

32
12

43

54

21

12 21 32 43 54*/

B] Selection Sort

#include<iostream>

using namespace std;

class Ssort{

public:

int a[10];

int n,min,loc,temp;

void get();

void sort();

void display();

};

void Ssort::get()

cout<<"No of element you want\n";

cin>>n;

cout<<"\n Enter array elements\n";

for(int i=0;i<n;i++)

cin>>a[i];

void Ssort::sort()

for(int i=0;i<n;i++)
{

min = a[i];

loc=i;

for(int j = i+1;j<n;j++)

if(min>a[j])

min = a[j];

loc =j;

temp =a[i];

a[i]=a[loc];

a[loc]=temp;

void Ssort::display()

for(int i=0;i<n;i++)

cout<<a[i]<<" ";

int main()

Ssort obj;

obj.get();
obj.sort();

obj.display();

/*OUTPUT:

No of element you want

Enter array elements

32

43

21

43

65

21 32 43 43 65*/
C] Insertion Sort

#include<iostream>

using namespace std;

class Isort{

public:

int a[10];

int n;

void get();

void sort();

void display();

};

void Isort::get()

cout<<"No of element you want\n";

cin>>n;

cout<<"\n Enter array elements\n";

for(int i=0;i<n;i++)

cin>>a[i];

void Isort::sort()

int temp,i,j;

for (i=1;i<=n-1;i++)

temp=a[i];

j=i-1;

while((temp<a[j]) && (j>=0))

{
a[j+1]=a[j];

j=j-1;

a[j+1]=temp;

void Isort::display()

for(int i=0;i<n;i++)

cout<<a[i]<<" ";

int main()

Isort obj;

obj.get();

obj.sort();

obj.display();

/*Output:

No of element you want

Enter array elements

21

32

43

21

43
21 21 32 43 43*/

D] Quick Sort

#include<iostream>
using namespace std;
class Quick
{
public:
int A[50],n;
void getdata();
void quicksort(int,int);
int Partition(int,int);
void swap(int &a,int &b);
void putdata();
};
void Quick :: getdata()
{
cout << "\n Enter the limit of the array : ";
cin >> n;
cout << "\n Enter the elements of the array : ";
for(int k=1;k<=n;k++)
{
cin >> A[k];
}
quicksort(1,n);
}
void Quick::quicksort(int p,int q)
{
if( p < q)
{
int j=q+1;
j=Partition(p,j);
quicksort(p,j-1);
quicksort(j+1,q);
}
}
int Quick::Partition(int m,int r)
{
int i;
int v=A[m];
i=m;
do
{
do
{
i++;
}while(A[i] <= v);
do
{
r--;
}
while(A[r] > v);
if(i<r)
{
swap(A[i],A[r]);
}
else
break;
}
while(1);
A[m]=A[r];
A[r]=v;
return(r);
}
void Quick::swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void Quick :: putdata()
{
cout<<"After Sorting ";
for(int k =1 ;k<=n;k++)
{
cout<<"\t"<<A[k];
}
}
int main()
{
Quick Q;
Q.getdata();
Q.putdata();
}
/*Output:
Enter the limit of the array : 5

Enter the elements of the array : 32


43
5
65
21
After Sorting 5 21 32 43 65*/

E] Merge Sort

#include<iostream>
using namespace std;
class number
{
int a[50],n,i;
public:
void get();
void mergesort(int low,int high);
void merge(int low,int mid,int high);
void put();
};
void number :: get()
{
cout<<"\n\n Enter the Number of Elements? :";
cin>>n;
cout << "\n Enter Your Elements : \n";
for (i=1; i<=n; i++)
{
cin >> a[i];
}
cout<<"\n\nYour array is:\n";
for (i=1; i<=n; i++)
{
cout<<a[i]<<"\t";
}
mergesort(1,n);
}
void number :: mergesort(int low,int high)
{
int mid;
if (low < high)
{
mid =(low + high) / 2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void number :: merge(int low,int mid,int high)
{
int h,i,j,k,b[5000];
h = low;
i = low;
j = mid+1;
while ((h <= mid) && (j <=high))
{
if(a[h] <= a[j])
{
b[i] = a[h];
h=h+1;
}
else
{
b[i] = a[j];
j=j+1;
}
i=i+1;
}
if (h > mid)
{
for (k=j; k<=high; k++)
{
b[i] = a[k];
i=i+1;
}
}
else
{
for (k=h; k<=mid; k++)
{
b[i] = a[k];
i=i+1;
}
}
for (k=low; k<=high; k++)
a[k] = b[k];
}
void number :: put()
{
cout<<"\n\nArray after Sorting :\n";
for (i=1; i<=n; i++)
{
cout<<a[i]<<"\t";
}
}
int main()
{
number a;
a.get();
a.put();
return(0);
}

/*Output:

Enter the Number of Elements? :5

Enter Your Elements :


21
33
54
65
23

Your array is:


21 33 54 70 23

Array after Sorting :


21 23 33 54 70*/

F] Radix Sort

#include <iostream>

using namespace std;

class Rsort

int arr[50];

int size;

public:

int getMax(int arr[],int size);

int CountingSort(int arr[],int size,int div);

int RadixSort(int arr[],int size);


int get();

};

int Rsort::getMax(int arr[],int size)

int max = arr[0];

for(int i=1;i<size;i++)

if(arr[i]>max)

max=arr[i];

return max;

int Rsort::CountingSort(int arr[],int size,int div)

int output[size];

int count[10]={0};

for(int i=0;i<size;i++)

count[(arr[i]/div)%10]++;

for(int i=1;i<10;i++)

count[i]+=count[i-1];

for(int i=size-1;i>=0;i--)

output[count[(arr[i]/div)%10]-1]=arr[i];

count[(arr[i]/div)%10]--;

for(int i=0;i<size;i++)

arr[i]=output[i];

int Rsort::RadixSort(int arr[],int size)

int m = getMax(arr,size);
for(int div=1;m/div>0;div*=10)

CountingSort(arr,size,div);

int Rsort::get()

int size;

cout<<"Enter the size of the array: "<<endl;

cin>>size;

int arr[size];

cout<<"Enter\t"<<size<<"integers in any order"<<endl;

for(int i=0;i<size;i++)

cin>>arr[i];

cout<<endl<<"Before Sorting"<<endl;

for(int i=0;i<size;i++)

cout<<arr[i]<<" ";

RadixSort(arr,size);

cout<<endl<<"After Sorting: "<<endl;

for(int i=0;i<size;i++)

cout<<arr[i]<<" ";

int main()

Rsort R;

R.get();
return 0;

/*Output:

Enter the size of the array:

Enter 5integers in any order

Before Sorting

23146

After Sorting:

1 2 3 4 6 */

5. Link List

A] Linear Linked List:

#include <iostream>

using namespace std;

class link

int info;

link *next;

public:

void insert();

void show();

void delet();

void reverse();

};

link *start=NULL, *temp, *move, *back;


int item;

void link::insert()

cout<<"\n Enter the item :";

cin>>item;

temp = new link;

temp->info= item;

temp->next=NULL;

if(start==NULL)

start=temp;

else

move=start;

while(move->next !=NULL)

move=move->next;

move->next=temp;

void link::show()

if(start==NULL)

cout<<"\nList is empty";

else

{
move=start;

while(move->next != NULL)

cout<<"\n"<<move->info;

move=move->next;

cout<<"\n"<<move->info;

void link::reverse()

int i, stack[10],top=-1;

if(start==NULL)

cout<<"\n List is empty:";

else

move=start;

while(move !=NULL)

top++;

stack[top]=move->info;

move=move->next;

cout<<"Link list reserve order: ";

for(i=top;i>=0;i--)

cout<<" "<<stack[i];

}
}

void link::delet()

if(start==NULL)

cout<<"\n List is empty :";

else

if(start->next==NULL)

item=start->info;

start=NULL;

else

move=start;

while(move->next!= NULL)

back=move;

move=move->next;

item=move->info;

back->next=NULL;

cout<<"\n Item is deleted :"<<item;

int main()
{

int ch;

link L1;

do

cout<<"\n 1 for Insert";

cout<<"\n 2 for Delete";

cout<<"\n 3 for Show";

cout<<"\n 4 for Reverse";

cout<<"\n 5 for Exit";

cout<<"\n Enter your choice :";

cin>>ch;

switch(ch)

case 1: L1.insert();

break;

case 2: L1.delet();

break;

case 3: L1.show();

break;

case 4: L1.reverse();

break;

case 5: exit(0);

} while(ch!=5);

/*Output:
1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :21

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :33

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :54

1 for Insert

2 for Delete

3 for Show
4 for Reverse

5 for Exit

Enter your choice :2

Item is deleted :54

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :3

21

33

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :4

Link list reserve order: 33 21

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :5*/


B] Circular Linked List:

#include <iostream>

using namespace std;

class clink

int info;

clink *next;

public:

void insert();

void show();

void delet();

};

clink *start=NULL, *temp, *move, *back;

int item;

void clink::insert()

cout<<"\n Enter the item :";

cin>>item;

temp = new clink;

temp->info= item;

temp->next=NULL;

if(start==NULL)

start=temp;

start->next=start;

else

move=start;
while(move->next !=start)

move=move->next;

temp->next=start;

move->next=temp;

void clink::show()

if(start==NULL)

cout<<"\nList is empty :";

else

move=start;

while(move->next != start)

cout<<"\n"<<move->info;

move=move->next;

cout<<"\n"<<move->info;

void clink::delet()

if(start==NULL)

cout<<"\n List is empty :";


}

else if(start->next==NULL)

cout<<"\n Deleted item is: "<<start->info;

start=NULL;

else

move=start;

while(move->next!= start)

back=move;

move=move->next;

cout<<"\n Deleted item is: "<<move->info;

back->next=start;

int main()

int ch;

clink C1;

do

cout<<"\n 1 for Insert";

cout<<"\n 2 for Delete";

cout<<"\n 3 for Show";

cout<<"\n 4 for Exit";

cout<<"\n Enter your choice :";


cin>>ch;

switch(ch)

case 1: C1.insert();

break;

case 2: C1.delet();

break;

case 3: C1.show();

break;

case 4: exit(0);

} while(ch!=4);

/*Output:

1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :1

Enter the item :21

1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :1

Enter the item :32


1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :1

Enter the item :25

1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :2

Deleted item is: 25

1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :3

21

32

1 for Insert

2 for Delete

3 for Show

4 for Exit

Enter your choice :4

Enter your choice4*/


C] Doubly Linked List:

#include <iostream>

using namespace std;

class dlink

int info;

dlink *left, *right;

public:

void insert();

void show();

void delet();

void reverse();

};

dlink *start=NULL, *end=NULL, *temp, *move, *back;

int item;

void dlink::insert()

cout<<"\n Enter the item :";

cin>>item;

temp = new dlink;

temp->info= item;

temp->left=NULL;

temp->right=NULL;

if(start==NULL)

start=temp;

end=temp;

else
{

move=start;

while(move->right !=NULL)

move=move->right;

move->right=temp;

temp->left=move;

end=temp;

void dlink::show()

if(start==NULL)

cout<<"\n List is empty :";

else

move=start;

while(move->right != NULL)

cout<<"\n"<<move->info;

move=move->right;

cout<<"\n"<<move->info;

void dlink::reverse()

{
if(start==NULL)

cout<<"\n List is empty :";

else

move=end;

while(move->left !=NULL)

cout<<"\n"<<move->info;

move=move->left;

cout<<" "<<move->info;

void dlink::delet()

if(start==NULL)

cout<<"\n List is empty";

else

if(start==end)

cout<<"\nDeleted item is: "<<start->info;

start=NULL;

end=NULL;

else
{

move=start;

while(move->right!= NULL)

back=move;

move=move->right;

item=move->info;

end=move->left;

back->right=NULL;

cout<<"\n Item is deleted :"<<item;

int main()

int ch;

dlink d1;

do

cout<<"\n 1 for Insert";

cout<<"\n 2 for Delete";

cout<<"\n 3 for Show";

cout<<"\n 4 for Reverse";

cout<<"\n 5 for Exit";

cout<<"\n Enter your choice :";

cin>>ch;

switch(ch)

case 1: d1.insert();
break;

case 2: d1.delet();

break;

case 3: d1.show();

break;

case 4: d1.reverse();

break;

case 5: exit(0);

} while(ch!=5);

/*Output:

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :21

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :32


1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :1

Enter the item :45

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :2

Item is deleted :45

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :3

21

32

1 for Insert

2 for Delete

3 for Show
4 for Reverse

5 for Exit

Enter your choice :4

32 21

1 for Insert

2 for Delete

3 for Show

4 for Reverse

5 for Exit

Enter your choice :5*/

3]A] Stack

#include <iostream>

using namespace std;

class Stack

int a[4],top;

public:

Stack()

top=-1;

void push();

void pop();

void show();

};

void Stack::push()

if(top==3)

{
cout<<"\n Overflow :";

else

cout<<"\n Enter item: ";

top=top+1;

cin>>a[top];

void Stack::pop()

if(top==-1)

cout<<"\n Underflow :";

else

cout<<"\n Deleted element is: "<<a[top];

top--;

void Stack::show()

if(top==-1)

cout<<"\n Empty :";

else

{
cout<<"\n Element are: \n";

for(int i=top;i>=0;i--)

cout<<"\n"<<a[i];

int main()

Stack a;

int ch;

while(1)

cout<<"\n------------Menu------------";

cout<<"\n 1.Push";

cout<<"\n 2.Pop";

cout<<"\n 3.Show";

cout<<"\n 4.Exit";

cout<<"\n----------------------------";

cout<<"\n Enter your choice: ";

cin>>ch;

switch(ch)

case 1: a.push();

break;

case 2: a.pop();

break;

case 3: a.show();
break;

case 4: exit(0);

default:cout<<"\n Wrong Choice :";

/*Output:

------------Menu------------

1.Push

2.Pop

3.Show

4.Exit

----------------------------

Enter your choice: 1

Enter item: 21

------------Menu------------

1.Push

2.Pop

3.Show

4.Exit

----------------------------

Enter your choice: 1

Enter item: 32

------------Menu------------

1.Push
2.Pop

3.Show

4.Exit

----------------------------

Enter your choice: 1

Enter item: 54

------------Menu------------

1.Push

2.Pop

3.Show

4.Exit

----------------------------

Enter your choice: 2

Deleted element is: 54

------------Menu------------

1.Push

2.Pop

3.Show

4.Exit

----------------------------

Enter your choice: 3

Element are:

32

21

------------Menu------------
1.Push

2.Pop

3.Show

4.Exit

----------------------------

Enter your choice:4*/

7. Polish Notation (Infix to Postfix):

#include <iostream>

#include <stack>

#include <algorithm>

using namespace std;

bool isOperator(char c)

if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^' )

return true;

else

return false;

int Precedence (char c)

if(c=='^')

return 3;

else if(c=='*'|| c=='/')

return 2;

else if(c=='+'|| c=='-')

return 1;
else

return -1;

string InfixToPostfix(stack<char>s, string infix)

string postfix;

for(int i=0; i<infix.length(); i++)

if((infix[i]>='a' && infix[i]<='z') || (infix[i]>='A' && infix[i]<='Z'))

postfix += infix[i];

else if(infix[i]=='(')

s.push(infix[i]);

else if(infix[i]==')')

while((s.top() !='(') && (!s.empty()))

char temp = s.top();

postfix += temp;

s.pop();

if(s.top()=='(')

s.pop();

else if(isOperator(infix[i]))
{

if(s.empty())

s.push(infix[i]);

else

if(Precedence(infix[i]) > Precedence(s.top()))

s.push(infix[i]);

else if(Precedence(infix[i]) == Precedence(s.top() && (infix[i]=='^')))

s.push(infix[i]);

else

while((!s.empty() && (Precedence(infix[i]) <= Precedence(s.top()))))

postfix += s.top();

s.pop();

s.push(infix[i]);

while(!s.empty())

postfix += s.top();
s.pop();

return postfix;

int main()

string infix_exp, postfix_exp;

cout<<"Enter a Infix Expression: "<<endl;

cin>>infix_exp;

stack<char>stack;

cout<<"Infix Expression: "<<infix_exp;

postfix_exp= InfixToPostfix(stack,infix_exp);

cout<<endl<<"Postfix Expression: "<<postfix_exp;

return 0;

/*Output:

Enter a Infix Expression:

A-(B+C)

Infix Expression: A-(B+C)

Postfix Expression: ABC+- */

8. Postfix Evaluation

#include <iostream>

#include <string.h>

using namespace std;

class Evaluation

public:

int st[50];

int top;

Evalution()
{

top=-1;

void push(int item);

int pop();

int operation(int,int,char);

int CalculatePostfix(char str[]);

};

void Evaluation::push(int item)

top++;

st[top]=item;

int Evaluation::pop()

int item = st[top];

top--;

return item;

int Evaluation::operation(int a, int b, char opr)

switch(opr)

case '+':

return a+b;

break;

case '-':

return a-b;

break;

case '*':
return a*b;

break;

case '/':

return a/b;

break;

default:

return 0;

int Evaluation::CalculatePostfix(char str[])

int index=0;

int len=strlen(str);

while(index<len)

if(isdigit(str[index]))

push(str[index]-'0');

else

int x=pop();

int y = pop();

int result=operation(y,x,str[index]);

push(result);

index++;

return pop();

}
int main()

char str[50];

Evaluation e;

cout<<"Enter the Postfix :";

cin>>str;

int result=e.CalculatePostfix(str);

cout<<"The result is: "<<result;

/*Output:

Enter the Postfix : 23+45+*

The result is: 45 */


9. Queue

A) Simple Queue

#include <iostream>

using namespace std;

class queue

int a[4],front,rear;

public:

queue()

front=-1;

rear=-1;

void push();

void pop();

void show();

};

void queue::push()

int item;

if(rear>=3)

cout<<"\n Queue is full";

else

cout<<"\n Enter your element:";

cin>>item;

rear=rear+1;
a[rear]=item;

if(front==-1)

front=0;

void queue::pop()

if(front==-1)

cout<<"Queue is empty";

else

cout<<"\n Deleted element is : "<<a[front];

if(front==rear)

front=-1;

rear=-1;

else

front=front+1;

void queue::show()
{

if(front==-1)

cout<<"\n Queue is Empty";

else

cout<<"Your element are: ";

for(int i=front; i<=rear; i++)

cout<<"\n"<<a[i];

int main()

queue a;

int ch;

do

cout<<"\n 1. Insert";

cout<<"\n 2. Delete";

cout<<"\n 3. Show";

cout<<"\n 4. Exit";

cout<<"\n Enter your choice :";

cin>>ch;

switch(ch)

case 1: a.push();
break;

case 2: a.pop();

break;

case 3: a.show();

break;

case 4: exit(0);

} while(ch!=4);

}/*

Output:

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :1

Enter your element:32

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :21

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :54


1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :2

Deleted element is : 32

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :3

Queue is Empty

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :4

*/

B] Circular queue

#include<iostream>

using namespace std;

#define max 3

class circular

int queue[max];

int front, rear, item;

public:
circular()

front=-1;

rear=-1;

void insert();

void del();

void show();

};

void circular::insert()

if(rear==max-1 &&front==0 || front==rear+1)

cout<<"Queue is overflow";

else

cout<<"\n Enter the item";

cin>>item;

if(front==-1)

front=0;

rear=0;

else if(rear==max-1)

rear=0;

else

{
rear++;

queue[rear]=item;

void circular::del()

if(front==-1)

cout<<"\n Queue is underflow...";

else

item=queue[front];

cout<<"\n Deleted item is"<<item;

if(front==rear)

front=-1;

rear=-1;

else if(front==max-1)

front=0;

else

front++;

}
void circular::show()

int i;

if(front==-1)

cout<<"\n Queue is empty..";

else

if(rear<front)

for(i=0;i<=rear;i++)

cout<<" "<<queue[i];

for(i=front;i<max;i++)

cout<<" "<<queue[i];

else

for(i=front;i<=rear;i++)

cout<<" "<<queue[i];

int main()
{

circular a;

int ch;

do

cout<<"\n 1. Insert";

cout<<"\n 2. Delete";

cout<<"\n 3. Show";

cout<<"\n 4. Exit";

cout<<"\n Enter your choice :";

cin>>ch;

switch(ch)

case 1: a.insert();

break;

case 2: a.del();

break;

case 3: a.show();

break;

case 4: exit(0);

}while(ch!=4);

//Output:

/*

1. Insert

2. Delete
3. Show

4. Exit

Enter your choice :1

Enter the item21

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :1

Enter the item32

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :1

Enter the item43

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :2

Deleted item is21

1. Insert
2. Delete

3. Show

4. Exit

Enter your choice :3

32 43

1. Insert

2. Delete

3. Show

4. Exit

Enter your choice :*/

10. Deques

#include<iostream>

using namespace std;

#define size 5

class Deque

int deque[size],f,r,x;

public:

Deque()

f=-1,r=-1;

void insert_front();

void insert_rear();

void display();

void getfront();

void getrear();

void delete_front();

void delete_rear();

};
void Deque::insert_front()

cout<<"Enter your item: ";

cin>>x;

if((f==0 && r==size-1) || (f==r+1))

cout<<"Overflow";

else if((f==-1) && (r==-1))

f=r=0;

deque[f]=x;

else if(f==0)

f=size-1;

deque[f]=x;

else

f=f-1;

deque[f]=x;

void Deque::insert_rear()

cout<<"Enter your item: ";

cin>>x;

if((f==0 && r==size-1) || (f==r+1))


{

cout<<"Overflow";

else if((f==-1) && (r==-1))

r=0;

deque[r]=x;

else if(r==size-1)

r=0;

deque[r]=x;

else

r++;

deque[r]=x;

void Deque::display()

int i=f;

cout<<"\n Element in a deque are: ";

while(i!=r)

cout<<deque[i];

i=(i+1)%size;

cout<<deque[r];
}

void Deque::getfront()

if((f==-1) && (r==-1))

cout<<"Deque is empty";

else

cout<<"\n The value of the element at front is: "<<deque[f];

void Deque::getrear()

if((f==-1) && (r==-1))

cout<<"Deque is empty";

else

cout<<"\n The value of the element at front is: "<<deque[r];

void Deque::delete_front()

if((f==-1) && (r==-1))

cout<<"Deque is Empty";

else if(f==r)
{

cout<<"\n The deleted elemet is: "<<deque[f];

f=-1;

r=-1;

else if(f==(size-1))

cout<<"\n The deleted elemet is: "<<deque[f];

f=0;

else

cout<<"\n The deleted elemet is: "<<deque[f];

f=f+1;

void Deque::delete_rear()

if((f==-1) && (r==-1))

cout<<"Deque is Empty";

else if(f==r)

cout<<"\n The deleted elemet is: "<<deque[r];

f=-1;

r=1;

else if(r==0)

{
cout<<"\n The deleted elemet is: "<<deque[r];

r=size-1;

else

cout<<"\n The deleted elemet is: "<<deque[r];

r=r-1;

int main()

Deque d;

int ch;

do

cout<<"\n 1. Insert at front";

cout<<"\n 2. Insert at rear";

cout<<"\n 3. Delete at front";

cout<<"\n 4. Delete at rear";

cout<<"\n 5. Show";

cout<<"\n 6. Get front value";

cout<<"\n 7. Get rear value";

cout<<"\n 8. Exit";

cout<<"\n Enter your choice";

cin>>ch;

switch(ch)

case 1: d.insert_front();

break;

case 2: d.insert_rear();
break;

case 3: d.delete_front();

break;

case 4: d.delete_rear();

break;

case 5: d.display();

break;

case 6: d.getfront();

break;

case 7: d.getrear();

break;

case 8: exit(0);

}while(ch!=8);

// return 0;

/*output:

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice1

Enter your item: 21

1. Insert at front
2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice1

Enter your item: 43

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice2

Enter your item:

42

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit
Enter your choice56

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice2

Enter your item: 54

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice3

The deleted elemet is: 43

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value


7. Get rear value

8. Exit

Enter your choice4

The deleted elemet is: 54

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice5

Element in a deque are: 2142

1. Insert at front

2. Insert at rear

3. Delete at front

4. Delete at rear

5. Show

6. Get front value

7. Get rear value

8. Exit

Enter your choice8 */


4]. Tree [BST, inorder, preorder, postorder]

#include <iostream>

using namespace std;

class tree

int info;

tree *left, *right;

public:

void insert();

void preorder(tree*);

void inorder(tree*);

void postorder(tree*);

void search();

};

tree *root=NULL, *move, *back, *temp;

int item,top;

void tree::insert()

temp=new tree;

cout<<"\n Enter the item: ";

cin>>item;

temp->info=item;

temp->left=NULL;

temp->right=NULL;

if(root==NULL)

root=temp;

else

{
move=root;

while(move!=NULL)

back=move;

if(temp->info<=move->info)

move=move->left;

else

move=move->right;

if(temp->info <= back->info)

back->left=temp;

else

back->right=temp;

void tree::preorder(tree *root)

if(root!=NULL)

cout<<" "<<root->info;

preorder(root->left);

preorder(root->right);
}

void tree::inorder(tree *root)

if(root!=NULL)

inorder(root->left);

cout<<" "<<root->info;

inorder(root->right);

void tree::postorder(tree *root)

if(root!=NULL)

postorder(root->left);

postorder(root->right);

cout<<" "<<root->info;

void tree::search()

if(root==NULL)

cout<<"Empty tree";

else

cout<<"\n Enter element which you want to search: ";

cin>>item;
move=root;

while(move!=NULL)

if(item==move->info)

cout<<"\n Element present";

break;

if(item<move->info)

move=move->left;

else

move=move->right;

if(move==NULL)

cout<<"\n Item is not present";

int main()

tree t;

int ch;

do

{
cout<<"\n 1. Insert";

cout<<"\n 2. Preorder";

cout<<"\n 3. Inorder";

cout<<"\n 4. Postorder";

cout<<"\n 5. Search";

cout<<"\n 6. Exit";

cout<<"\n Enter your choice :";

cin>>ch;

switch(ch)

case 1: t.insert();

break;

case 2: t.preorder(root);

break;

case 3: t.inorder(root);

break;

case 4: t.postorder(root);

break;

case 5: t.search();

break;

case 6: exit(0);

}while(ch!=6);

/*

output

1. Insert

2. Preorder

3. Inorder
4. Postorder

5. Search

6. Exit

Enter your choice :1

Enter the item: 54

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :1

Enter the item: 65

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :1

Enter the item: 32

1. Insert

2. Preorder

3. Inorder
4. Postorder

5. Search

6. Exit

Enter your choice :1

Enter the item: 76

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :1

Enter the item: 38

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :2

54 32 38 65 76

1. Insert

2. Preorder

3. Inorder

4. Postorder
5. Search

6. Exit

Enter your choice :3

32 38 54 65 76

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :4

38 32 76 65 54

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :5

Enter element which you want to search: 76

Element present

1. Insert

2. Preorder

3. Inorder

4. Postorder

5. Search

6. Exit

Enter your choice :6 */


13. BFS

#include <iostream>
using namespace std;

class BFS

int matrix[5][5],n;

int visited[5];

int q[5],front,rear;

public:

void get();

void bfs(int v);

};

void BFS::get()

int i,j;

front=rear=1;

cout<<"\n Enter the number of node :";

cin>>n;

cout<<"\n Enter the matrix :";

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

cin>>matrix[i][j];

for(i=1;i<=n;i++)
{

visited[i]=0;

void BFS::bfs(int v)

int i;

int u=v;

cout<<"\n visited nodes using BFS :";

visited[v]=1;

cout<<v<<"\t";

do

for(int w=1;w<=n;w++)

if(matrix[u][w]==1)

q[rear++]=w;

visited[w]=1;

cout<<w<<"\t";

if(front==rear)

break;

u=q[front++];

}while(1);
}

int main()

BFS A;

int v;

A.get();

cout<<"\n Enter the starting node :";

cin>>v;

A.bfs(v);

/*

output

1111

0000

1000

1100

1110

0001

0011

0111

0110

1001

0100

0010

1101

1011

1010

0101*/

14. Heap Tree:


#include <iostream>

using namespace std;

void convertHeap(int arr[], int len, int x)

int largest = x;

int left = 2 * x + 1;

int right = 2 * x + 2;

if( left < len && arr[left] > arr[largest])

largest = left;

if( right < len && arr[ right] > arr[ largest])

largest =right;

if( largest != x)

swap(arr[x] , arr[largest]);

convertHeap(arr, len, largest);

void heapSort(int arr[] , int len){

for(int i = len/2-1; i>=0; i--)

convertHeap(arr, len, i);

for(int i=len-1; i>=0; i--)


{

swap(arr[0], arr[i]);

convertHeap(arr, i, 0);

void display(int arr[], int len)

for( int i=0; i<len; i++)

cout << arr[i] <<" ";

cout<<endl;

int main()

int arr[5] = {58,25,74,82,21};

int len = 5;

heapSort(arr, len);

display(arr,len);

/*

output:

21 25 58 74 82 */

You might also like