kbkunalb
BAN USER#include<iostream>
#include<vector>
using namespace std;
bool jump( bool river[], int n, int initial, int curr) {
if(initial >= n)
return false;
if(initial <=0)
return false;
if( ((curr + initial) < n) && !river[curr + initial])
return false;
if( (curr + initial) >= n)
return true;
else
return (jump(river,n,initial,curr+initial) || jump(river,n,initial-1,curr+initial-1) || jump(river,n,initial+1,curr+initial+1));
}
bool is_possible(bool river[] , int n) {
for(int i=0; i< n; i++) {
if(river[i]) {
if(jump(river, n, i+1, 0))
return true;
}
}
return false;
}
int main() {
bool river[] = { true, false, false, true, false, false, true, true, true, false, false, false, true, false, false, false, true, true, false, false, false}; // River of stones where jump is either allowed or not allowed
int n = sizeof(river)/sizeof(river[0]);
if( is_possible(river, n) )
cout << " Frog CAN jump " << endl;
else
cout << " Frog CANNOT jump " << endl;
bool river2[] = { true, false, false, true, false, false, true, true, true, false, false, false, true, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}; // River of stones where jump is either allowed or not allowed
n = sizeof(river2)/sizeof(river2[0]);
if( is_possible(river2, n) )
cout << " Frog CAN jump " << endl;
else
cout << " Frog CANNOT jump " << endl;
return 0;
}
Declare fun() as non static.
Then you can't make a call to fun() just by referring it via class name.
You have to call this function through object only instead of class like :-
ABC a; // statement 1
a.fun(); // statement 2
But as you the constructor is private and you can't make an instance, so
statement 1 would be illegal.
Also, still you could able to do it by using the below piece of code :-
ABC *a; // statement 1
a->fun(); // statement 2
As fun() is not making any reference to "this" object, so the above 2 statements were possible.
To overcome this, access some data from fun() as below :-
class A {
private:
int z;
A() { cout << " Constrcutor got called " << endl; }
~A() { cout << " destructor got called " << endl; }
public:
static void fun() { A aa; } // statement 1
void fun2() { A aa; } // Statement 2
void fun3() { z = 10; A aa; } // Statement 3
};
int main() {
A::fun();
A *a;
a->fun2();
a->fun3();
}
Now statement 3 will give segmentation fault and call to the constructor will be restricted in this way.
#include<iostream>
using namespace std;
int main() {
int n = 4;
int **arr = new int*[n];
for(int i=0; i<n; i++)
arr[i] = new int[n];
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = i + (2*j) + 1;
}
}
cout << " Print matrix " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << " Output " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(!(i%2))
cout << arr[i][j] << " ";
else
cout << arr[i][n-j-1] << " ";
}
}
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<int> v ;
int n=0;
cout << " Enter total number of integers : " << endl;
cin >> n;
cout << " Enter " << n << " positive and negative numbers : " ;
int temp_sum = 0, max_sum =0, start_index = -1, end_index = -1, temp_start_index = 0;
for(int i=0; i<n; i++) {
int num = 0;
cout << endl << i << "th number : ";
cin >> num;
v.push_back(num);
if(num >=0) {
temp_sum = temp_sum + num;
if(max_sum < temp_sum) {
max_sum = temp_sum;
end_index = i;
start_index = temp_start_index;
}
}
else {
temp_sum = 0;
temp_start_index = i + 1;
}
}
cout << " Subarray with max sum : " << endl;
for(int i = start_index; i <= end_index; i++) {
cout << v[i] << " ";
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
int i = 0, k=0, prev_index = -1, prev_sum = 0, c=0, curr_index = -1, curr_sum = 0;
string str = "abcd2bc4d3e2";
cout << " str is : " << str << endl;
cout << " Enter k : " ;
cin >> k;
while(i < str.size() ) {
if(str[i] >= '0' && str[i] <= '9') {
prev_index = curr_index;
prev_sum = curr_sum;
curr_index = i;
curr_sum = curr_sum + (c * (str[i] - '0'));
c=0;
}
else
c++;
if(k == 0) {
cout << str[0] << endl;
break;
}
else if( prev_index == -1 && k < curr_sum) {
k = k - prev_sum;
k = k%curr_index;
cout << " element is : " << str[k] << endl;;
break;
}
else if( k >= prev_sum && k < curr_sum && prev_sum != curr_sum) {
if( k == prev_sum ) {
cout << " element is : " << str[prev_index+1] << endl;
break;
}
if( k == curr_sum ) {
cout << " element is : " << str[i+1] << endl;
break;
}
k = k - prev_sum;
k = k % (curr_index - prev_index - 1);
cout << " element is : " << str[prev_index + k + 1] << endl;;
break;
}
i++;
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
int i = 0, k=0, prev_index = -1, prev_sum = 0, c=0, curr_index = -1, curr_sum = 0;
string str = "abcd2bc4d3e2";
cout << " str is : " << str << endl;
cout << " Enter k : " ;
cin >> k;
while(i < str.size() ) {
if(str[i] >= '0' && str[i] <= '9') {
prev_index = curr_index;
prev_sum = curr_sum;
curr_index = i;
curr_sum = curr_sum + (c * (str[i] - '0'));
c=0;
}
else
c++;
if(k == 0) {
cout << str[0] << endl;
break;
}
else if( prev_index == -1 && k < curr_sum) {
k = k - prev_sum;
k = k%curr_index;
break;
}
else if( k >= prev_sum && k < curr_sum && prev_sum != curr_sum) {
if( k == prev_sum )
break;
if( k == curr_sum )
break;
k = k - prev_sum;
k = k % (curr_index - prev_index - 1);
break;
}
i++;
}
return 0;
}
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 6, 7, 8, 9, 5, 4, 12, 15, 18, 17, 25, 20};
int s = sizeof(arr)/sizeof(arr[0]);
int k=0;
cout << " Enter k : ";
cin >> k;
for(int i=0; i<(s/k); i++) {
int t=0;
for(int j=(i*k); j< (i*k) + k/2; j++) {
int l = arr[j+t];
arr[j+t] = arr[j+k-1-t];
arr[j+k-1-t] = l;
t++;
}
}
return 0;
}
bool scan(vector<string> &grid, int rows, vector<string> & sub_grid, int sub_rows) {
for(int i=0; i<rows; i++) {
int x=0;
//cout << " Processing : " << grid[i] << endl;
for(int j=0; j<sub_rows; j++) {
x = grid[i].find(sub_grid[j]);
if(x == -1)
break;
int k=0;
//cout << x << endl;
for(k=0;k<sub_rows-1;k++) {
string str = grid[i+k+1].substr(x,sub_grid[0].size());
//cout << str << endl;
if(str != sub_grid[j+k+1]) {
j=sub_rows+1;
break;
}
}
if(k>=sub_rows-1)
return true;
}
//cout << " x is " << x << endl;
if(x!= -1) {
string spaces;
//for(int z=0;z<x+sub_grid[0].size();z++)
spaces.push_back('a');
//cout << "spaces : " << spaces << endl;
//grid[i].replace(0,x+sub_grid[0].size(),spaces);
grid[i].replace(x,1,spaces);
//cout << " string after replacing is : " << grid[i] << endl;
i--;
continue;
}
}
return false;
}
int calculate_subarr( int *arr, int n, int k) {
deque<int> loc;
int i=0, one_count=0;
int max_count = 0;
while(i<n && loc.size() < k) {
if(arr[i] == 0)
loc.push_back(i);
}
for(int i=0; i<n; i++) {
if(arr[i] == 1)
one_count++;
}
while(loc.size() == k) {
int min = loc[0];
for(int i=min-1; i>0;i--) {
if(arr[i] == 1)
min--;
else
break;
}
int max = loc.back();
for(int i=max+1; i<n;i++) {
if(arr[i] == 1)
max++;
else
break;
}
int curr_max_count = max - min + 1;
if( curr_max_count > one_count )
curr_max_count = one_count;
if( curr_max_count > max_count )
max_count = curr_max_count;
loc.pop_front();
int maxc = max + 1;
while(maxc < n) {
if(arr[maxc] == 0) {
loc.push_back(maxc);
break;
}
maxc++;
}
}
return max_count;
}
#include<iostream>
#include<deque>
#include<vector>
#include<list>
using namespace std;
void displayq (deque<vector<int> > &d) {
for(int i=0; i<d.size(); i++) {
vector<int> v = d[i];
for(int j=0; j<v.size(); j++) {
cout << v[j] << " ";
}
for(int j=0; j<v.size(); j++) {
cout << char('A' + v[j] -1) ;
}
cout << endl;
}
}
int main() {
int arr[] = {1,1,2,3};
int n = sizeof(arr)/sizeof(arr[0]);
int i=0;
deque<vector<int> > dqlstint;
vector <int> v ;
for(int i=0; i<n; i++)
v.push_back(arr[i]);
dqlstint.push_back(v);
while(i<dqlstint.size() ) {
vector<int> front = dqlstint[i];
int temp=0;
for(int j=0; j<front.size()-1; j++) {
vector<int> front2 = front;
if( (temp = ((front[j]*10)+front[j+1])) <= 26 && front[j] < 10 && front[j+1] < 10 ) {
front2[j] = temp;
front2.erase(front2.begin() + j +1);
int k=0;
while(k<dqlstint.size() ) {
if(dqlstint[k] == front2)
break;
k++;
}
if(k == dqlstint.size() )
dqlstint.push_back(front2);
}
}
i++;
}
displayq(dqlstint);
return 0;
}
void display_same_level(struct btree **b) {
deque< struct btree* > dtree;
if(*b)
dtree.push_back(*b);
int level=0;
int element_at_level=1;
while(!dtree.empty() ) {
struct btree* t = dtree.front();
cout << t->nodedata << " " ;
dtree.pop_front();
element_at_level--;
if(element_at_level == 0) {
level++;
}
if(t->left) {
dtree.push_back(t->left);
element_at_level++;
}
if(t->right) {
dtree.push_back(t->right);
element_at_level++;
}
}
}
#include<iostream>
using namespace std;
int main() {
int i=0, n=0, s=0, prev=0, next=0;
cout << " Enter n " ;
cin >> n;
while(i<n)
{
if(s == n) {
cout << " Sum is found for " << prev << " to " << next << endl;
break;
}
while(s > n ) {
s = s - prev;
prev++;
if(s == n)
{
cout << " Sum is found for " << prev << " to " << next << endl;
break;
}
}
if(s == n)
break;
s = s + i;
next = i;
i++;
}
if( s > n)
cout << " Sum for " << n << " not exist " << endl;
return 0;
}
void find_dup(string ststr)
{
map <string , int> msstr;
string sstr = ststr.substr(0,10);
int i=1;
while(sstr.size() == 10)
{
msstr[sstr]++;
sstr = ststr.substr(i,10);
i++;
}
map <string , int> :: iterator msit = msstr.begin();
for( ; msit != msstr.end(); msit++)
{
if((*msit).second > 1 )
cout << " String : " << (*msit).first << " count " << (*msit).second << endl;
}
}
#include<iostream>
#include<map>
using namespace std;
void pop_arr(int *ptr, int n)
{
cout << " Populate the numbers " << endl;
for(int i=0; i<n; i++)
{
cout << " Enter " << i << "th element : " << endl;
cin >> ptr[i];
}
}
void display(map<int, int> &m)
{
map<int, int> :: iterator it;
for(it = m.begin(); it != m.end(); it++)
cout << " Key : " << (*it).first << " frequency " << (*it).second << endl;
}
int main()
{
int n=0;
cout << " Enter the total number of numbers : " << endl;
cin >> n;
int *ptr = new int[n];
pop_arr(ptr,n);
map<int, int> map_occur;
for(int i=0; i<n; i++)
map_occur[ptr[i] ]++;
display(map_occur);
multimap<int, int> map_occur2;
map<int, int> :: iterator it;
for(it = map_occur.begin(); it != map_occur.end(); it++)
map_occur2.insert(make_pair((*it).second, (*it).first) );
multimap<int, int> :: iterator mit = map_occur2.begin();
int min_num = (*mit).first;
for(mit = map_occur2.begin(); (*mit).first == min_num; mit++)
cout << " Key : " << (*mit).first << " frequency " << (*mit).second << endl;
cout << " Number occuring second least time : " << (*mit).second << endl;
return 0;
}
/* Below program will provide all the sets having sum equal to given number, not only the set of 2 numbers */
/* This program will provide all the list of numbers having sum equal to some particular number. This list could have 1,2,3,4
,5,.... numbers. for example, if in a list, there are 2,4,6,5,3,1 numbers, and we have to find the numbers having sum as 7, t
hen the sets of numbers would be {1,6},{2,5},{3,4},{1,2,4} */
#include<stdio.h>
struct list
{
int data;
struct list * next;
}
;
struct list * arr_list[200];
int list_count=0,prev=0,row=0,prev_row=0;
int fact(int i)
{
int x,y,z=1;
for(x=1;x<=i;x++)
{
z=z*x;
}
return(z);
}
void check_display()
{
/*int x,y;
struct list * temp;
printf("\n inside check_display \n");
for(x=0;x<list_count+1;x++)
{
temp=arr_list[x];
printf("%d. ",x);
while(temp!=NULL)
{
printf(" %d -> ",temp->data);
temp=temp->next;
}
printf("\n");
}*/
}
void sum_check(int num)
{
int x,y,sum=0;
struct list * temp;
printf("\n inside sum_check \n");
for(x=0;x<list_count;x++)
{
temp=arr_list[x];
//printf("<%.3d>. ",x);
sum=0;
while(temp!=NULL)
{
sum=sum+temp->data;
//printf(" %d -> ",temp->data);
temp=temp->next;
}
if(sum==num)
{
printf("\n for LIST <%d> , sum is <%d> \n",x,sum);
}
//printf("\n");
}
}
void display()
{
int x,y;
struct list * temp;
printf("\n inside display \n");
for(x=0;x<list_count;x++)
{
temp=arr_list[x];
printf("<%.3d>. ",x);
while(temp!=NULL)
{
printf(" %d -> ",temp->data);
temp=temp->next;
}
printf("\n");
}
}
void adr_cpy(struct list** target,struct list** source)
{
printf("\n inside adr_cpy \n");
struct list* temp;
temp=(struct list*)malloc(sizeof(struct list));
(*target)=temp;
while((*source)!=NULL)
{
temp->data=(*source)->data;
(*source)=(*source)->next;
temp->next=(struct list*)malloc(sizeof(struct list));
temp=temp->next;
}
temp=NULL;
}
calcsum(int j, int num, int count, int arra[])
{
int check_dup=0,x,y,z,r=0,local_list_count=0,temp_list_count=0;
struct list *temp,*temp2,*temp3,*temp4;
printf("\n Inside calcsum \n");
printf("Print the list of pairs containing <%d> elements from the array \n",j);
if(j<=1)
{
x=0;
for(x=0;x<count;x++)
{
//if(arra[x]==num)
//{
printf("\n list of numbers having only 1 set which satisfies the condition \n");
printf("arra[%d] is <%d> num <%d> \n",x,arra[x],num);
arr_list[list_count]=(struct list *) malloc(sizeof(struct list));
arr_list[list_count]->data=arra[x];
arr_list[list_count]->next=NULL;
list_count++;
//}
}
r=fact(count)/(fact(j)*fact(count-j));
row=r+row;
printf("\n row is <%d> for count <%d> j <%d> \n",row,count,j);
prev=1;
}
else if(j==2)
{
for(x=0;x<count-1;x++)
{
//if(arra[x] < num)
//{
for(y=x+1;y<count;y++)
{
//if(arra[y] < num)
//{
//if((arra[x] + arra[y])==num)
//{
arr_list[list_count]=(struct list *) malloc(sizeof(struct list));
temp=(struct list *) malloc(sizeof(struct list));
temp->data=arra[y];
temp->next=NULL;
arr_list[list_count]->data=arra[x];
arr_list[list_count]->next=temp;
list_count++;
//}
//}
}
//}
}
r=fact(count)/(fact(j)*fact(count-j));
row=r+row;
r=fact(count)/(fact(j-1)*fact(count-(j-1)));
prev_row=r+prev_row;
printf("\n row is <%d> for count <%d> j <%d> \n",row,count,j);
prev=2;
check_display();
}
else
{
r=fact(count)/(fact(prev)*fact(count-prev));
x=prev_row;
printf("\n calsum : else <%d> \n",j);
for(y=0;y<count;y++) /* check for arra[y] element and add it to the lists */
{
temp=arr_list[x];
for(x=prev_row;x<row;x++) /* check for the lists which are ;previously created for j-1 */
{
printf("\n check for x list <%d> \n",x);
temp=arr_list[x];
printf("\n in the below loop, check whether %d element exist in list %x \n",arra[y],x);
while(temp!=NULL)
{
printf("\n data is <%d> element is <%d> \n",temp->data,arra[y]);
if(arra[y]==(temp->data))
{
break;
}
temp=temp->next;
}
temp4=(struct list *) malloc(sizeof(struct list));
printf("\n Check whether temp is NULL, if NULL , add the element else check next list \n");
if(temp==NULL) /* create new list and insert arra[y] into it when arra[y] doesnt exist in that list */
{
fflush(stdout);
printf("\n in the list <%d> , element <%d> doesnt exist, so add \n",x,arra[y]);
printf("\n inside add if condition to add the element \n");
temp=arr_list[x];
if(arra[y] < temp->data) /* number to be inserted is the first element of new list */
{
printf("\n if element should be added at first position \n");
temp2=(struct list *) malloc(sizeof(struct list));
temp2->data=arra[y];
temp2->next=temp;
/* CROW arr_list[x]=temp2; */
arr_list[list_count]=(struct list *) malloc(sizeof(struct list));
arr_list[list_count]->data=temp2->data;
arr_list[list_count]->next=temp2->next;
list_count=list_count+1;
/* CROW added above */
//arr_list[list_count]=(struct list *) malloc(sizeof(struct list));
//arr_list[list_count]=temp2;
//list_count++;
}
else
{
printf("\n if element not to be added at first position \n");
arr_list[list_count]=(struct list *) malloc(sizeof(struct list)); /* CROW */
arr_list[list_count]=temp4;
while(temp->next!=NULL)
{
printf(" data <%d> next_data <%d> element <%d> \n",temp->data,(temp->next->data),arra[y]);
printf("\n insert <%d> into list <%d> \n",temp->data,list_count);
temp4->data=temp->data; /*CROW */
if((temp->data < arra[y]) && ((temp->next->data) > arra[y]))
{
printf("\n if element to be added in between the limits \n");
temp2=(struct list *) malloc(sizeof(struct list));
temp2->data=arra[y];
// CROW temp2->next=temp->next;
// CROW temp->next=temp2;
//temp2->next=temp->next;
// CROW temp->next=temp2;
break;
}
temp4->next=(struct list *) malloc(sizeof(struct list)); /* CROW */
temp4=temp4->next;
temp=temp->next;
}
if(temp->next==NULL)
{
//temp4->next=(struct list *) malloc(sizeof(struct list)); /* CROW */
//temp4=temp4->next;
printf("\n insert %d into list <%d>\n",temp->data,list_count);
temp4->data=temp->data;
printf("\n if element to be added in the end of the list \n");
temp2=(struct list *) malloc(sizeof(struct list));
temp4->next=(struct list *) malloc(sizeof(struct list));
temp4->next=temp2;
printf("\n insert %d into list <%d>\n",arra[y],list_count);
temp2->data=arra[y];
temp2->next=NULL;
// CROW2 temp->next=temp2;
//temp4->next=temp2;
list_count++;
}
else /* CROW */
{
check_display();
printf("\n inside else to populate remaining list \n");
//arr_list[list_count]->data=temp->data;
temp4->next=(struct list *) malloc(sizeof(struct list));
temp4=temp4->next;
temp4->data=temp2->data;
printf("\n insert <%d> into list <%d> \n",temp2->data,list_count);
temp=temp->next;
check_display();
while(temp!=NULL)
{
printf("\nwhile insert <%d> into list <%d> \n",temp->data,list_count);
temp4->next=(struct list *) malloc(sizeof(struct list));
temp4=temp4->next;
temp4->data=temp->data;
temp=temp->next;
}
temp4->next=NULL;
list_count++;
}
}
}
display();
printf("\n check for duplicacy \n\n");
for(check_dup=row;check_dup<list_count-1;check_dup++)
{
printf("\n check_dup is <%d> list_count is <%d> \n",check_dup,list_count);
temp=arr_list[check_dup];
temp2=arr_list[list_count-1];
fflush(stdout);
printf("\n before while \n");
while(((temp->data)==(temp2->data)) && temp->next!=NULL)
{
temp=temp->next;
temp2=temp2->next;
printf(" check data \n");
}
printf(" after while \n");
fflush(stdout);
if((temp->next==NULL && temp2->next==NULL) && (temp->data)==(temp2->data))
{
printf("\n duplicacy found \n");
list_count--;
//free(arr_list[list_count-1]);
break;
}
}
/* temp list created with arra[y] .. check if duplicate already created */
//arr_list[list_count]=(struct list *) malloc(sizeof(struct list));
//arr_list[list_count]->data=j;
//list_count++;
display();
}
}
r=fact(count)/(fact(j)*fact(count-j));
row=r+row;
r=fact(count)/(fact(j-1)*fact(count-(j-1)));
prev_row=r+prev_row;
printf("\n row is <%d> for count <%d> j <%d> \n",row,count,j);
prev++;
//calcsum(j-1,num,count,arra);
}
}
sum_series(int num, int count, int arra[])
{
int i,j,k;
printf("inside sum_series \n");
for(i=0;i<count;i++)
{
printf("\n element <%d> <%d> \n",i,arra[i]);
}
for(j=1;j<=count;j++)
{
//if(arra[j-1]<num )
calcsum(j,num,count,arra);
}
}
void main()
{
int i=0,j=0,k=0,temp;
int arra[10],arrb[10];
int num;
printf("\nEnter number of digits for the arra y : ");
scanf("%d",&k);
for(i=0;i<k;i++)
{
printf("\n Enter Element <%d> \n",i);
scanf("%d",&arra[i]);
}
printf("\n Enter number : ");
scanf("%d",&num);
printf("\n Sort the array \n");
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(arra[i]>arra[j])
{
temp=arra[i];
arra[i]=arra[j];
arra[j]=temp;
}
}
}
for(i=0;i<k;i++)
{
printf("\n element <%d> <%d> \n",i,arra[i]);
}
sum_series(num,k,arra);
display();
sum_check(num);
}
/* String reverse : reverse the words of a string */
#include<stdio.h>
#include<string.h>
char * word_rev(char *str)
{
char * tempstr,*wrdrev;
int len,i,j,k=0;
tempstr=(char *)malloc(sizeof(char)*100);
len=strlen(str);
printf("strlen is <%d> \n",len);
printf("\n reversing the string \n");
for(i=0;i<len;i++)
*(tempstr+i)=*(str+len-i-1);
printf("\n string after reverse <%s> \n",tempstr);
wrdrev=(char *)malloc(sizeof(char)*100);
for(i=0;i<len;i++)
{
if((i==0) || (*(tempstr+i)==' '))
{
if(*(tempstr+i)==' ')
{
*(wrdrev+i) = ' ';
i++;
}
for(j=i+1;j<=len;j++)
{
if((j==len) || (*(tempstr+j)==' '))
{
for(k=0;k<(j-i);k++)
{
*(wrdrev+i+k)=*(tempstr+j-k-1);
}
break;
}
}
}
}
printf("\n string after word reverse <%s> \n",wrdrev);
return(wrdrev);
}
void main()
{
int i,j;
char *str,*tempstr;
str=(char *)malloc(sizeof(char)*100);
printf("\n Enter the input string : \n");
gets(str);
printf("\n string before reverse <%s> \n",str);
tempstr=word_rev(str);
printf("\n MAIN before end : string is <%s> \n",tempstr);
}
/* Create binary tree and find the shortest path between 2 nodes */
#include<stdio.h>
int tot=0;
int sp=0;
int count=0;
struct btree
{
int data;
struct btree * left;
struct btree * right;
};
int insert(struct btree ** parent,int nodedata)
{
int i=0;
struct btree * temp;
printf("\n Inside INSERT \n");
if((*parent)==NULL)
{
printf("\n Populating node for <%d> data \n",nodedata);
temp=(struct btree *)malloc(sizeof(struct btree));
temp->data=nodedata;
temp->right=NULL;
temp->left=NULL;
(*parent)=temp;
tot++;
}
else
{
if((*parent)->data > nodedata)
{
printf("\n Insert to the left of <%d> \n",(*parent)->data);
insert(&((*parent)->left),nodedata);
}
else if((*parent)->data < nodedata)
{
printf("\n Insert to the right of <%d> \n",(*parent)->data);
insert(&((*parent)->right),nodedata);
}
else
{
printf("\n return \n");
return 0;
}
}
}
int find_path(struct btree **parent,int node,int path[])
{
printf("\n Inside find_path for node <%d> at node <%d> \n",node,(*parent)->data);
if((*parent)==NULL)
{
printf("\n Binary tree is empty or node doesnt exist \n");
return 0;
}
else
{
if(((*parent)->data > node))
{
path[count++]=(*parent)->data;
find_path(&((*parent)->left),node,path);
}
else if(((*parent)->data < node))
{
path[count++]=(*parent)->data;
find_path(&((*parent)->right),node,path);
}
else
{
printf("\n Row found \n");
path[count++]=(*parent)->data;
return(count);
}
}
}
void main()
{
int s,i=1,j=0,node1,node2,count1,count2;
int path1[10],path2[10];
struct btree * parent=NULL,*temp2;
printf("\n Inside MAIN \n");
temp2=(struct btree * ) malloc(sizeof(struct btree));
printf("\n Data to be inserted is \n");
scanf("%d",&j);
temp2->data=j;
temp2->right=NULL;
temp2->right=NULL;
parent=temp2;
printf("insert another node \n 1 YES \n 2 NO \n");
scanf("%d",&i);
while(i==1)
{
printf("Insert node \n");
printf("\n Data to be inserted is \n");
scanf("%d",&j);
insert(&parent,j);
printf("insert another node \n 1 YES \n 2 NO \n");
scanf("%d",&i);
}
printf("\n find the distance between 2 nodes \n");
printf("\n NODE 1 :\n");
scanf("%d",&node1);
printf("\n NODE 2 :\n");
scanf("%d",&node2);
count=0;
count1=find_path(&parent,node1,path1);
count1=count;
printf("\n count1 is <%d> \n",count1);
for(i=0;i<count1;i++)
{
printf("path1[%d] is <%d> \n",i,path1[i]);
}
count=0;
count2=find_path(&parent,node2,path2);
printf("\n count is <%d> \n",count);
count2=count;
for(i=0;i<count2;i++)
{
printf("path2[%d] is <%d> \n",i,path2[i]);
}
if(count1<count2)
count=count1;
else
count=count2;
s=0;
for(i=0;i<count;i++)
{
if(path1[i]==path2[i])
{
s=s+2;
}
else
break;
}
printf("s is %d \n",s);
s=count1+count2-s;
printf("\n shortest path is <%d> \n",s);
}
//Json object using composite pattern
- kbkunalb October 01, 2020class IJsonObject {
public:
virtual void print() = 0;
};
class IJsonObjects : public IJsonObject {
string str_key;
string str_indent;
public:
vector<IJsonObject*> objects;
IJsonObjects() : str_key("") , str_indent("") {
}
IJsonObjects(string str) : str_key(str), str_indent(" ") {
}
void print() {
if (str_key.length() > 0) {
cout << str_indent << "\"" << str_key << "\" : {";
}
for (auto elem : objects) {
cout << str_indent;
elem->print();
}
if (str_key.length() > 0) {
cout << str_indent << "}" << endl;
}
}
};
class IJsonInt : public IJsonObject {
public:
int val;
string str_key;
IJsonInt(string str, int v) : str_key(str), val(v) {
}
void print() {
cout << "\"" << str_key << "\" : " << val << endl;
}
};
class IJsonStr : public IJsonObject {
public:
string str_val;
string str_key;
IJsonStr(string str, string v) : str_key(str), str_val(v) {
}
void print() {
cout << "\"" << str_key << "\" : \"" << str_val << "\"" << endl;
}
};
void process_json_str() {
cout << "\n\n Json block: " << endl;
IJsonObjects* json_root = new IJsonObjects;
IJsonObject * json_obj1 = new IJsonInt { "version", 10 };
json_root->objects.push_back(json_obj1);
IJsonObject* json_obj2 = new IJsonStr{ "OS", "Windows 10" };
IJsonObject* json_obj3 = new IJsonInt{ "Build number", 6093 };
json_root->objects.push_back(json_obj2);
json_root->objects.push_back(json_obj3);
IJsonObjects* json_subroot = new IJsonObjects{"Customer Details"};
IJsonObject* json_obj4 = new IJsonInt{ "Age", 30 };
IJsonObject* json_obj5 = new IJsonStr{ "name", "Kunal" };
json_subroot->objects.push_back(json_obj4);
json_subroot->objects.push_back(json_obj5);
json_root->objects.push_back(json_subroot);
IJsonObjects* json_subroot2 = new IJsonObjects{ "Contract Details" };
IJsonObject* json_obj6 = new IJsonInt{ "Cost per licence", 15 };
IJsonObject* json_obj7 = new IJsonStr{ "Licence", "3 Years" };
json_subroot2->objects.push_back(json_obj6);
json_subroot2->objects.push_back(json_obj7);
json_subroot->objects.push_back(json_subroot2);
json_root->print();
return;
}