20130411

How to learn recursive function call in cpp/C++/C

/*How to learn recursive function call in cpp/C++/C
 Cpp Program to learn recursive function */
#include<iostream>
using namespace std;
int sum(int m, int k);
int main()
{
int i,m,k;
m=3;
k=2;
i=sum(m,k);
cout<<"sum="<<i<<endl;
//return 0;
}
int sum(int rm, int rk)
{
cout<<rm<<" "<<rk<<endl;
if(rm==0) return(rk);//termination condition
else return(1+sum(rk, rm-1));//recursive condition//if not +1 then sum=0
}

How to distinguish between call by value, call by reference, call by address

/*How to distinguish between call by value, call by reference, call by address.
Program to distinguish between pass by value,pass by reference,pass by address. Call by reference and call by address affects the calling value while call by value doe not. Most important difference is that in call by value new object is created which is some times such as when size of the variable is too large is not desirable so call by reference is preferred over call by value*/

#include<stdio.h>
#include<iostream>
using namespace std;
void callbyreference(int * x);//pass by address
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(&x);
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyreference(int * x)
{
*x=*x+5;//mind the*
}


//CALL BY REFERNCE, PASS BY REFERNCE
/*

20130330

How to write cpp program for Circular Queue using Array


How to write C++ program for Circular Queue using Array
C++ program to implement Circular Queue using Array
Circular Queue implementation using Array
CPP program for implementing Circular Queue using Array


#include<iostream>
using namespace std;
#define MAXSIZE 4

class queue
{
private:
int cq[MAXSIZE];
int front, rear;
public:
queue()
{
int cq[MAXSIZE]={0};//error: array must be initialized with a brace-enclosed initializer
front= -1;
rear = -1;
}


void insert(int item)
{
cout<<"\n in insert.\t";
if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) || front==(rear+1)%MAXSIZE )
{
cout<<"Q is full.\n";
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;

cq[rear]=item;

cout<<"\n front = "<<front<<" rear = "<<rear<<endl;

}

}


void display()
{
int i;
for(i=0;i<MAXSIZE;i++)
{
cout<<cq[i]<<" ";
}
cout<<endl;
}


void deletee()
{
if(front==-1)
{
cout<<"Q is empty.\n";
}
else
{
int temp;
temp=cq[front];
cq[front]=0;
if(front==rear)//only 1 elt was there
front=rear=-1;//delete the single so q is empty
else
front=(front+1)%MAXSIZE;//if more than 1 elt then delete frm front

cout<<"\n Deleted elt is:"<<temp<<endl;
cout<<"\n front = "<<front<<" rear = "<<rear<<endl;
}
}

};
int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.display();

q.insert(111);//Q is full so make same place by deleting fronts
q.display();

q.deletee();
q.display();
q.insert(100);
q.display();



return 0;
}
/*
in insert.
front = 0 rear = 0

in insert.
front = 0 rear = 1

in insert.
front = 0 rear = 2

in insert.
front = 0 rear = 3
10 20 30 40

in insert. Q is full.
10 20 30 40

Deleted elt is 10.

front = 1 rear = 3
0 20 30 40

in insert.
front = 1 rear = 0
100 20 30 40
*/
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program for Circular Queue using Linked List



 /*C++ Program for Circular Queue implementation using linked list
Cpp program to implement Circular Queue using Linked List*/
C++ Program for implementing Circular Queue with Linked List

#include<iostream>
using namespace std;

class queue
{
private:
struct node
{
int data;
node* link;
};
node *front;
node *rear;

public:
void insert(int);
void display();
void del();
queue()
{

How to write cpp program for BST Binary Search Tree


/*BST-Binary Search Tree Program for  insertion and traversal
How to write C++ Program for BST-Binary Search Tree
Cpp program for BST Binary Search Tree implementation with class*/
-->
//bst-Binary Search tree
#include<iostream>
using namespace std;

class BST
{
private:
struct node
{
int data;
node *left;
node *right;
};
node* start;

public:
BST(){start=NULL;}
void insert(int data);
void traverse();
void inorder(node *);
void preorder(node *);
void postorder(node *);
};

void BST:: insert(int d)
{
node *newnode;
newnode=new node;
newnode->data=d;
newnode->left=NULL;
newnode->right=NULL;

node *ptr;
ptr=start;
if(start==NULL)
{
start=newnode;
}
else
{
while(ptr!=NULL)
{
if(d<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=newnode;
ptr=NULL;
}
else
{
ptr=ptr->left;
}
}
else
{
//if(d>ptr->data)
//{
if(ptr->right==NULL)
{
ptr->right=newnode;
ptr=NULL;
}
else
{
ptr=ptr->right;
}
// }
}
}
}
}

void BST::traverse()
{
cout<<"\n Inorder:\n";
inorder(start);
cout<<"\n Preorder:\n";
preorder(start);
cout<<"\n Postorder:\n";
postorder(start);

}
void BST::inorder(node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<ptr->data<<"\t";
inorder(ptr->right);
}
}
void BST::preorder(node *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<"\t";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data<<"\t";

}
}


int main()
{
BST b;
b.insert(8);
b.insert(3);
b.insert(10);
b.insert(1);
b.insert(6);
b.insert(7);
b.insert(4);
b.insert(10);
b.insert(14);
b.insert(13);

b.traverse();




return 0;
}
/*
$ g++ bst_revision.cxx
$ ./a.out

Inorder:
1 3 4 6 7 8 10 10 13 14
Preorder:
8 3 1 6 4 7 10 10 14 13
Postorder:
1 4 7 6 3 13 14 10 10 8
*


Depth-first
  • Pre-order traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right)
  • In-order traversal sequence: A, B, C, D, E, F, G, H ,I (left, root, right)
  • Post-order traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)
Breadth-first
  • Level-order traversal sequence: F, B, G, A, D, I, C, E, H
Inorder:
1 2 3 4 5 6 7 8 9
Pre order:
6 2 14 3 5 7 9 8
Post order:
1 3 5 4 2 8 9 7 6
Simples Way to learn Data Structure in C++ CPP or C