Sunday, September 7, 2014

Implement a stack using one queue

Implement stack only using one queue.



 #include<stdio.h>  
 #include<stdlib.h>  
 typedef struct queue node;  
 struct queue{  
         int d;  
         node *n;          
   
 };  
 int size=0;  
 node *f,*r;  
 void enq(int d)  
 {  
         node *t;  
         t=(node *)malloc(sizeof(node));  
         t->d=d;  
         t->n=NULL;  
         if(r)  
         {  
                 r->n=t;  
                 r=t;  
         }  
         else  
         {  
                 f=r=t;  
         }  
         size++;  
 }  
 node *deq()  
 {  
         node *t;  
         if(f)  
         {  
                 size--;  
                 t=f;  
                 f=f->n;  
                 return t;  
   
         }  
         else  
         {  
                 printf("Error");  
         }  
 }  
 void push(int d)  
 {  
         enq(d);  
   
 }  
 void pop()  
 {  
         int i=0,j=size;  
         node *t;  
         while(i<j-1)  
         {  
                 t=deq();  
                 enq(t->d);  
                 i++;  
         }  
         t=deq();  
         printf("%d ",t->d);          
   
 }  
 void main()  
 {  
         push(1);  
         push(2);  
         push(3);  
         push(4);  
         push(5);  
         pop();  
         pop();  
         pop();  
         pop();  
         pop();  
   
           
 }  

Saturday, September 6, 2014

Clone a Binary Tree with Random Pointers Given a Binary Tree where every node has following structure.
struct node {
int key;
struct node *left,*right,*random;
}
The random pointer points to any random node of the binary tree and can even point to NULL, clone the given binary tree.
You can get code in c++ Here: https://gist.github.com/gk1721/36775e17719d460ac231