Anand Rai
BAN USERGuys i present a simple solution for C/C++ , it very easy in JAVA so i ma not writing for JAVA
#include <unistd.h> /* defines fork(), and pid_t. */
#include <sys/wait.h> /* defines the wait() system call. */
int status=0;
void main()
{
/* storage place for the pid of the child process, and its exit status. */
pid_t child_pid;
int child_status;
/* lets fork off a child process... */
child_pid = fork();
/* check what the fork() call actually did */
switch (child_pid)
{
case -1:/* fork() failed */
perror("fork"); /* print a system-defined error message */
exit(1);
case 0:/* fork() succeeded, we're inside the child process */
f1();
status=1;
exit(0);/* here the CHILD process exits, not the parent. */
default:/* fork() succeeded, we're inside the parent process */
wait(&child_status);/* wait till the child process exits */
if(0==status)//oops some exception occured during f1()
{
// do exception management, i mean free memory
}
}
}
Class Data{
int phone;
// other data
}
Class Record{
Vector myRecords=new vector();
void add(Data d)
{
myRecords.add(d);
}
}
class Store{
Hashtable table=new Hashtable();
void add(Data d)
{
if(table.containsKey(d.phone))
((Record)table.get(d.phone)).add(d);
else table.add(d.phone,(new Record()).add(d));
}
Data[] getKey(int ph)
{
Record ans=table.get(d.phone);
if(ans==NULL) return NULL;
Enumeration enum1=ans.myRecords().keys();
Data[] array=new[enum1.size()] Data();
int i=0;
while(enum1.hasMorelements()&&++i)
array[i-1]=(Data)enum1.nextElement();
return array;
}
}
Class Card{
public:
const static int CLUBS=1;
const static int SPADE=2;
const static int HEART=3;
const static int DIAMOND=4;
Card(int suite,int number)
{
(*this).suite=suite;
(*this).number=number;
}
private:
int suite;
int number;
}
#define LOOP(start,end) for(i=start;i<=end;i++)
#define ASSIGN(n,i,start) mydeck[i]=Card(n,i-start)
Class Deck{
public :
Card[52] mydeck;
Deck()
{
int i=0;
LOOP(0,12)
ASSIGN(Card.CLUBS,i,0);
LOOP(13,25)
ASSIGN(Card.SPADES,i,13);
LOOP(26,38)
ASSIGN(Card.HEARTS,i,26);
LOOP(39,51)
ASSIGN(Card.DIAMOND,i,39);
}
void shuffle(){
int i=0,j=0;
LOOP(0,51){
j=random(i,51);
swap(j,i);}
}
void swap(int i,int j){
Card tmp=mydeck[i];
mydeck[i]=mydeck[j];
mydeck[j]=tmp;
}
}
Guys i guess if we compare Inorder and Preorder/Post order traversal of binary search tree that can tell if two binary search trees are similar or not .
Otherwise for n Ary tree we can have following algorithm
Stack st1,st2;
Compare tree(Tree *t1,Tree *t2)
{
st1.push(all child of t1);
st2.push(all child of t2);
while(!st1.isempty()&&!st2.isempty())
{ if(t1->data==t2->data&&(((temp1=st1.pop())->data==(temp2=st2.pop())->data)||temp1==temp2==NULL))
Compare(temp1,temp2);
}
}
Above algorithm will be quite easy for Binary trees .
Compare(BinaryTree *t1,BinaryTree *t2)
{
if(t1==NULL && t2==NULL) return true;
return (t1!=NULL && t2!=NULL && t1->data==t2->data && Compare(t1->left,t2->left) && Compare(t1->right,t2->right));
}
In case of link list while allocating nodes worst case possible scenario may be that memory gets allocated in various pages , which may result in significant page faults , degarding the performance . Whereas it may not be the same case with Array as is it a contigous allocation , provided array data elements are not of page size .
- Anand Rai September 10, 2007
but what if both Random5() return same value
- Anand Rai October 07, 2007here is a simple solution
long Random7()
{
long i=Random5();
return i+(i/5)*2;
}