Re: [algogeeks] Re: whats a CALL BACK in C?

2012-05-31 Thread Bhavesh agrawal
Here is an example of callbacks in C.

Let's say you want to write some code that allows registering
callbacks to be called when some event occurs.

First define the type of function used for the callback:

typedef void (*event_cb_t)(const struct event *evt, void *userdata);
Now, define a function that is used to register a callback:

int event_cb_register(event_cb_t cb, void *userdata);
This is what code would look like that registers a callback:

static void my_event_cb(const struct event *evt, void *data)
{
/* do stuff and things with the event */
}

...
   event_cb_register(my_event_cb, my_custom_data);
...
In the internals of the event dispatcher, the callback may be stored
in a struct that looks something like this:

struct event_cb {
event_cb_t cb;
void *data;
};
This is what the code looks like that executes a callback.

struct event_cb *callback;

...

/* Get the event_cb that you want to execute */

callback-cb(event, callback-data);

source: 
http://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented

On 5/29/12, Gene gene.ress...@gmail.com wrote:
 A callback is a function, say B, that you provide to some other
 function F in order to control F's behavior.

 The intuition is that F is defined with a hole in its specification
 that it fills up by calling back to the B you furnished.

 A simple example of a callback is the comparison function argument of
 qsort() .  For a more interesting example, look up the API for zlib,
 which is nicely designed with several callbacks.  The raw Win32 API
 also uses callbacks for various purposes.

 In the C language, callback is through a simple function pointer to B
 because that's all you have. In higher level languages, the function
 can carry data in a closure. With a Java-like object model, a
 listener object can be provided with both data and one or more
 methods. The normal way to approximate this higher level language
 behavior is by defining the C API with a void* parameter to F that it
 in turn passes back to B.  So when you call F, you can also provide
 data for B to use when F calls it later.  The zlib API uses this
 technique.

 On May 28, 4:22 pm, rahul r. srivastava rahul.ranjan...@gmail.com
 wrote:
 whats a CALL BACK in C?

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Amazon : exponentiation(x,n)

2012-05-31 Thread Ashish Goel
the return type is int, hence when xpowern becomes bigger than INT_MAX, it
should throw an exception.


Best Regards
Ashish Goel
Think positive and find fuel in failure
+919985813081
+919966006652


On Thu, May 31, 2012 at 11:59 AM, Amol Sharma amolsharm...@gmail.comwrote:

 i didn't got your problem where is the overflow.. ??

 btw i will do the same like this :

 int pow(int b, int e)
 {
int result = (e  1) ? b:1;
while( e  1 )
{
b = ((long long int)b * b);
e = 1;
if( e  1 )
result = ((long long int)result * b);
}
return result;
 }
 --


 Amol Sharma
 Third Year Student
 Computer Science and Engineering
 MNNIT Allahabad
  http://gplus.to/amolsharma99 
 http://twitter.com/amolsharma99http://in.linkedin.com/pub/amol-sharma/21/79b/507http://www.simplyamol.blogspot.com/






 On Thu, May 31, 2012 at 9:54 AM, Ashish Goel ashg...@gmail.com wrote:

  This algo is log(n) algo for finding power. However, this also has a
 problem of overflow. How do we control this.

 int power(int x, int n) {
   int expo =1;
   int even=x;
   while (n0)
   {
  while (n  0x1==0) {
 n=1; /*divide by 2*/
 even*=even;
  }
  expo = expo*even;
  n=1; /*n will be odd here always*/
   }
   return expo;
 }

 this is utilizing the fact that n is a binary number and can be written
 as x*xE when odd or xE otherwise.

 Best Regards

 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Amazon : exponentiation(x,n)

2012-05-31 Thread Amol Sharma
for checking overflow you can check the value after and before the
multiplication..if value after multiplication is less then there is
overflow for sure

now code will look like this :


int pow(int b, int e)
{
   int result = (e  1) ? b:1;
   int prev;
   while( e  1 )
   {
   prev=b;
   b = (b * b);
   if(bprev)
  return -1; //OVERFLOW
   e = 1;
   if( e  1 )
   {
  prev=result;
   result = (result * b);
   if(prevresult)
return -1;  //OVERFLOW
   }
   return result;
}
--


Amol Sharma
Third Year Student
Computer Science and Engineering
MNNIT Allahabad
 http://gplus.to/amolsharma99
http://twitter.com/amolsharma99http://in.linkedin.com/pub/amol-sharma/21/79b/507http://www.simplyamol.blogspot.com/






On Thu, May 31, 2012 at 12:07 PM, Ashish Goel ashg...@gmail.com wrote:

 the return type is int, hence when xpowern becomes bigger than INT_MAX, it
 should throw an exception.


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 11:59 AM, Amol Sharma amolsharm...@gmail.comwrote:

 i didn't got your problem where is the overflow.. ??

 btw i will do the same like this :

 int pow(int b, int e)
 {
int result = (e  1) ? b:1;
while( e  1 )
{
b = ((long long int)b * b);
e = 1;
if( e  1 )
result = ((long long int)result * b);
}
return result;
 }
 --


 Amol Sharma
 Third Year Student
 Computer Science and Engineering
 MNNIT Allahabad
  http://gplus.to/amolsharma99 
 http://twitter.com/amolsharma99http://in.linkedin.com/pub/amol-sharma/21/79b/507http://www.simplyamol.blogspot.com/






 On Thu, May 31, 2012 at 9:54 AM, Ashish Goel ashg...@gmail.com wrote:

  This algo is log(n) algo for finding power. However, this also has a
 problem of overflow. How do we control this.

 int power(int x, int n) {
   int expo =1;
   int even=x;
   while (n0)
   {
  while (n  0x1==0) {
 n=1; /*divide by 2*/
 even*=even;
  }
  expo = expo*even;
  n=1; /*n will be odd here always*/
   }
   return expo;
 }

 this is utilizing the fact that n is a binary number and can be written
 as x*xE when odd or xE otherwise.

 Best Regards

 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Linked list using void pointer

2012-05-31 Thread Hassan Monfared
Why don't you use templates ?

template class T
class LNode
{
public:
LNode(T pData,LNode *pNext):data(pData),next(pNext){}
T data;
LNode *next;
};
template class T
class LList
{
protected:
LNodeT *head;
LNodeT *tail;
public:
LList()
{
head=tail=NULL;
}

void push_back(T pData)
{
if(head==NULL)
{
head=tail=new LNodeT(pData,NULL);
return;
}
tail-next=new LNodeT(pData,NULL);
tail=tail-next;
}
void push_front(T pData)
{
if(head==NULL)
{
head=tail=new LNodeT(pData,NULL);
return;
}
LNodeT *nnode=new LNodeT(pData,head);
head=nnode;
}
void Print()
{
LNodeint *cur=head;
while(cur)
{
coutcur-data',';
cur=cur-next;
}
cout  endl;
}
void Reverse()
{
LNodeT *cur=head;
LNodeT *prev=NULL;
LNodeT *tmp;
while(cur)
{
tmp=cur-next;
cur-next=prev;
prev=cur;
cur=tmp;
}
tmp=head;
head=prev;
tail=tmp;
}
};

Regards

On Thu, May 31, 2012 at 8:49 AM, mahendra sengar sengar.m...@gmail.comwrote:

 how to implement generioc linked list..using void pointer...i havent
 used void pointer much so, m not able to use it properly in linked
 list..please help asap !!!

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread Ashish Goel
the LL is unsorted, is there any better solution that this?

struct node* deleteNodes(struct node *pHead, struct node *pPrev)
{
  struct  node *pLL = *pHead;
  if (!pLL) return NULL;
  struct node *pCurr = pLL;

  struct node *pRest = deleteNodes(pCurr-next, pCurr);
  if (!pRest) return pCurr;
  if (pCurr-data pRest-data)
  {
if (pPrev) { pPrev-next = pRest; };
free(pCurr);
  }
 else
 {
   pCurr-next = pRest;
 }
   return pCurr;
}


Best Regards
Ashish Goel
Think positive and find fuel in failure
+919985813081
+919966006652

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread atul anand
@Ashish :  please clarify this ques...

delete a node in SLL if it is less than *any* of the succesor node ..

1 2 8 10 3 4 7 12

delete 1,2,8,10,3,4,7

ouput will be single node i.e 12

dats what question asks?

On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread Ashish Goel
yes

Best Regards
Ashish Goel
Think positive and find fuel in failure
+919985813081
+919966006652


On Thu, May 31, 2012 at 2:34 PM, atul anand atul.87fri...@gmail.com wrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread atul anand
then i guess ...it can be done using stack..with O(n) complexity..
it is similar to finding next greater element 

http://www.geeksforgeeks.org/archives/8405

element in the stack at the end of the algo...are the element which will
remain in the linked list . if stack is not empty then keep poping elements
and create a SLL.

On Thu, May 31, 2012 at 4:29 PM, Ashish Goel ashg...@gmail.com wrote:

 yes

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 2:34 PM, atul anand atul.87fri...@gmail.comwrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread Ashish Goel
that is what i have done by using recursion=stack.

my code has problem, after  free(pCurr);, i should have return pRest;
Best Regards
Ashish Goel
Think positive and find fuel in failure
+919985813081
+919966006652


On Thu, May 31, 2012 at 4:37 PM, atul anand atul.87fri...@gmail.com wrote:

 then i guess ...it can be done using stack..with O(n) complexity..
 it is similar to finding next greater element 

 http://www.geeksforgeeks.org/archives/8405

 element in the stack at the end of the algo...are the element which will
 remain in the linked list . if stack is not empty then keep poping elements
 and create a SLL.


 On Thu, May 31, 2012 at 4:29 PM, Ashish Goel ashg...@gmail.com wrote:

 yes

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 2:34 PM, atul anand atul.87fri...@gmail.comwrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread Navin Kumar
I think the easiest method to do this problem is this:

http://k2code.blogspot.in/2011/09/deleting-node-in-singly-linked-list-if.html

On Thu, May 31, 2012 at 5:58 PM, Ashish Goel ashg...@gmail.com wrote:

 that is what i have done by using recursion=stack.

 my code has problem, after  free(pCurr);, i should have return pRest;

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 4:37 PM, atul anand atul.87fri...@gmail.comwrote:

 then i guess ...it can be done using stack..with O(n) complexity..
 it is similar to finding next greater element 

 http://www.geeksforgeeks.org/archives/8405

 element in the stack at the end of the algo...are the element which will
 remain in the linked list . if stack is not empty then keep poping elements
 and create a SLL.


 On Thu, May 31, 2012 at 4:29 PM, Ashish Goel ashg...@gmail.com wrote:

 yes

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 2:34 PM, atul anand atul.87fri...@gmail.comwrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Amazon : Find popular cost

2012-05-31 Thread g4ur4v
How to find popular cost of the books,
say
   book1 $10
   book2 $20
   book3 $40
   book4 $50
   book5 $10
   book6 $20


http://www.careercup.com/question?id=13720752

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Linked list using void pointer

2012-05-31 Thread Nishant Pandey
this way u can do it in c  creation and printing of generic lisk list .

void(List **p,void *data, unsigned int n)
{
List *temp;
int i;

/* Error check is ignored */
temp = malloc(sizeof(List));
temp-data = malloc(n);
for (i = 0; i  n; i++)
*(char *)(temp-data + i) = *(char *)(data + i);
temp-next = *p;
*p = temp;
}

void(List *p,void (*f)(void*))
{
while (p)
{
(*f)(p-data);
p = p-next;
}
}


void printstr(void *str)
{
printf( \%s\, (char *)str);

}

Regads
Nishant Pandey

On Thu, May 31, 2012 at 1:15 PM, Hassan Monfared hmonfa...@gmail.comwrote:

 Why don't you use templates ?
 
 template class T
 class LNode
 {
 public:
 LNode(T pData,LNode *pNext):data(pData),next(pNext){}
  T data;
 LNode *next;
 };
 template class T
 class LList
 {
 protected:
 LNodeT *head;
 LNodeT *tail;
 public:
 LList()
 {
 head=tail=NULL;
  }

 void push_back(T pData)
 {
  if(head==NULL)
 {
 head=tail=new LNodeT(pData,NULL);
  return;
 }
 tail-next=new LNodeT(pData,NULL);
  tail=tail-next;
 }
 void push_front(T pData)
  {
 if(head==NULL)
 {
  head=tail=new LNodeT(pData,NULL);
 return;
 }
  LNodeT *nnode=new LNodeT(pData,head);
 head=nnode;
 }
  void Print()
 {
 LNodeint *cur=head;
  while(cur)
 {
 coutcur-data',';
  cur=cur-next;
 }
 cout  endl;
  }
 void Reverse()
 {
  LNodeT *cur=head;
 LNodeT *prev=NULL;
 LNodeT *tmp;
  while(cur)
 {
 tmp=cur-next;
  cur-next=prev;
 prev=cur;
 cur=tmp;
  }
 tmp=head;
 head=prev;
  tail=tmp;
 }
 };

 Regards

 On Thu, May 31, 2012 at 8:49 AM, mahendra sengar sengar.m...@gmail.comwrote:

 how to implement generioc linked list..using void pointer...i havent
 used void pointer much so, m not able to use it properly in linked
 list..please help asap !!!

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Cheers,

Nishant Pandey |Specialist Tools Development  |
npan...@google.comgvib...@google.com |
+91-9911258345

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread Nishant Pandey
if i am getting this questions correctly then we have to delete the element
till its next is not null ??
please comment if i am wrong ?

On Thu, May 31, 2012 at 5:58 PM, Ashish Goel ashg...@gmail.com wrote:

 that is what i have done by using recursion=stack.

 my code has problem, after  free(pCurr);, i should have return pRest;

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 4:37 PM, atul anand atul.87fri...@gmail.comwrote:

 then i guess ...it can be done using stack..with O(n) complexity..
 it is similar to finding next greater element 

 http://www.geeksforgeeks.org/archives/8405

 element in the stack at the end of the algo...are the element which will
 remain in the linked list . if stack is not empty then keep poping elements
 and create a SLL.


 On Thu, May 31, 2012 at 4:29 PM, Ashish Goel ashg...@gmail.com wrote:

 yes

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 2:34 PM, atul anand atul.87fri...@gmail.comwrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Cheers,

Nishant Pandey |Specialist Tools Development  |
npan...@google.comgvib...@google.com |
+91-9911258345

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Linked list using void pointer

2012-05-31 Thread Nishant Pandey
in case of generic link list when u have to create the node u have to copy
the data part character by character ie always one -one byte and in this
way whatever be the data type of ur data u can easily get the data and
u will keep  doing this until the size of the data that to entered .

On Thu, May 31, 2012 at 9:49 AM, mahendra sengar sengar.m...@gmail.comwrote:

 how to implement generioc linked list..using void pointer...i havent
 used void pointer much so, m not able to use it properly in linked
 list..please help asap !!!

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Cheers,

Nishant Pandey |Specialist Tools Development  |
npan...@google.comgvib...@google.com |
+91-9911258345

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Linked list using void pointer

2012-05-31 Thread Gene
You didn't say C or C++.  It makes a difference.  A void pointer is
just a pointer that can point to any kind of data.  You convert it to
a specific type by using casts.  So just implement an exogenous list
the same  way you would if data had some type Foo.  The replace all
the Foo pointers with void*.  In C++ you can wrap the implementation
in a template so the list methods automatically do the casting.

In case you aren't familiar with the term, an exogenous list is just
one where list nodes _point to_ data rather than containing data.  For
example, this list node is exogenous:

typedef struct node_s {
  struct node_s *next;
  FOO *ptr_to_data;  // replace with void* to make this useful for any
data type.
} NODE;

This one is endogenous:

typedef struct node_s {
  struct node_s *next;
  FOO data;
} NODE;

On May 31, 12:19 am, mahendra sengar sengar.m...@gmail.com wrote:
 how to implement generioc linked list..using void pointer...i havent
 used void pointer much so, m not able to use it properly in linked
 list..please help asap !!!

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] MS Question: Delete a node in single linked list if it is less than any of the successor nodes

2012-05-31 Thread atul anand
@navin : +1

On 5/31/12, Navin Kumar algorithm.i...@gmail.com wrote:
 I think the easiest method to do this problem is this:

 http://k2code.blogspot.in/2011/09/deleting-node-in-singly-linked-list-if.html

 On Thu, May 31, 2012 at 5:58 PM, Ashish Goel ashg...@gmail.com wrote:

 that is what i have done by using recursion=stack.

 my code has problem, after  free(pCurr);, i should have return pRest;

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 4:37 PM, atul anand
 atul.87fri...@gmail.comwrote:

 then i guess ...it can be done using stack..with O(n) complexity..
 it is similar to finding next greater element 

 http://www.geeksforgeeks.org/archives/8405

 element in the stack at the end of the algo...are the element which will
 remain in the linked list . if stack is not empty then keep poping
 elements
 and create a SLL.


 On Thu, May 31, 2012 at 4:29 PM, Ashish Goel ashg...@gmail.com wrote:

 yes

 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 2:34 PM, atul anand
 atul.87fri...@gmail.comwrote:

 @Ashish :  please clarify this ques...

 delete a node in SLL if it is less than *any* of the succesor node ..

 1 2 8 10 3 4 7 12

 delete 1,2,8,10,3,4,7

 ouput will be single node i.e 12

 dats what question asks?

 On Thu, May 31, 2012 at 2:16 PM, Ashish Goel ashg...@gmail.com
 wrote:

 the LL is unsorted, is there any better solution that this?

 struct node* deleteNodes(struct node *pHead, struct node *pPrev)
 {
   struct  node *pLL = *pHead;
   if (!pLL) return NULL;
   struct node *pCurr = pLL;

   struct node *pRest = deleteNodes(pCurr-next, pCurr);
   if (!pRest) return pCurr;
   if (pCurr-data pRest-data)
   {
 if (pPrev) { pPrev-next = pRest; };
 free(pCurr);
   }
  else
  {
pCurr-next = pRest;
  }
return pCurr;
 }


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Amazon : Find popular cost

2012-05-31 Thread atul anand
here find popular post means to find most repeated costso we can
use combination of max-heap and hashtable.

hashtable is required to keep track for distinct cost.

so if cost is not present in hashtable then add it to the hashtable
and then add it to the Max-heap with counter value =1...then hepify.
next time we find that cost in hashtable then we just need to
increment counter of that cost in heap and thne hepify.At any point of
time ...element at the top is the element with popular cost.

On 5/31/12, g4ur4v gauravyadav1...@gmail.com wrote:
 How to find popular cost of the books,
 say
book1 $10
book2 $20
book3 $40
book4 $50
book5 $10
book6 $20


 http://www.careercup.com/question?id=13720752

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Tree/Graph implementation

2012-05-31 Thread Mad Coder
@Gene: Can you tell some other ways of graph representation in case of
sparse matrix as till now I consider adjacency list as the best method for
the same.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Amazon : exponentiation(x,n)

2012-05-31 Thread Dave
@Amol: You've chosen to use -1 as an error return. But -1 is the correct 
response if b = -1 and e is odd. Furthermore, isn't the inequality in the 
if(prevresult) statement backwards. E.g., if b = 2 and e = 3, then the 
code returns -1. Even reversing the inequality doesn't fix the problem when 
b = -2 and e = 3, for which the correct response is -8, without overflow.
 
Dave

On Thursday, May 31, 2012 2:04:24 AM UTC-5, Amol Sharma wrote:

 for checking overflow you can check the value after and before the 
 multiplication..if value after multiplication is less then there is 
 overflow for sure

 now code will look like this :


 int pow(int b, int e)
 {
int result = (e  1) ? b:1;
int prev;
while( e  1 )
{
prev=b;
b = (b * b);
if(bprev)
   return -1; //OVERFLOW
e = 1;
if( e  1 )
{
   prev=result;
result = (result * b);
if(prevresult)
 return -1;  //OVERFLOW
}
return result;
 }
 --


 Amol Sharma
 Third Year Student
 Computer Science and Engineering
 MNNIT Allahabad
  http://gplus.to/amolsharma99 
 http://twitter.com/amolsharma99http://in.linkedin.com/pub/amol-sharma/21/79b/507http://www.simplyamol.blogspot.com/
  






 On Thu, May 31, 2012 at 12:07 PM, Ashish Goel ashg...@gmail.com wrote:

 the return type is int, hence when xpowern becomes bigger than INT_MAX, 
 it should throw an exception. 


 Best Regards
 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652


 On Thu, May 31, 2012 at 11:59 AM, Amol Sharma amolsharm...@gmail.comwrote:

 i didn't got your problem where is the overflow.. ??

 btw i will do the same like this :

 int pow(int b, int e)
 {
int result = (e  1) ? b:1;
while( e  1 )
{
b = ((long long int)b * b);
e = 1;
if( e  1 )
result = ((long long int)result * b);
}
return result;
 }
 --


 Amol Sharma
 Third Year Student
 Computer Science and Engineering
 MNNIT Allahabad
  http://gplus.to/amolsharma99 
 http://twitter.com/amolsharma99http://in.linkedin.com/pub/amol-sharma/21/79b/507http://www.simplyamol.blogspot.com/
  






 On Thu, May 31, 2012 at 9:54 AM, Ashish Goel ashg...@gmail.com wrote:

  This algo is log(n) algo for finding power. However, this also has a 
 problem of overflow. How do we control this.

 int power(int x, int n) {  
   int expo =1;
   int even=x;
   while (n0)
   {
  while (n  0x1==0) {
 n=1; /*divide by 2*/
 even*=even;
  }
  expo = expo*even;
  n=1; /*n will be odd here always*/   
   }
   return expo;
 }

 this is utilizing the fact that n is a binary number and can be written 
 as x*xE when odd or xE otherwise.

 Best Regards

 Ashish Goel
 Think positive and find fuel in failure
 +919985813081
 +919966006652

 -- 
 You received this message because you are subscribed to the Google 
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to 
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/algogeeks?hl=en.


  -- 
 You received this message because you are subscribed to the Google 
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to 
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/algogeeks?hl=en.


  -- 
 You received this message because you are subscribed to the Google Groups 
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to 
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/algogeeks?hl=en.




-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/UuSiE5US8SkJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.