<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7662544687317968079</id><updated>2012-02-14T18:08:52.791-08:00</updated><title type='text'>Interview Questions</title><subtitle type='html'>Please post your answer in comments if you have a better solution.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>83</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8458610940303538540</id><published>2007-08-15T04:28:00.000-07:00</published><updated>2007-08-15T04:29:06.280-07:00</updated><title type='text'>Write a C program to create a mirror copy of a tree (left nodes become right and right nodes become left)!</title><content type='html'>This C code will create a new mirror copy tree.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mynode *copy(mynode *root)&lt;br /&gt;{&lt;br /&gt;  mynode *temp;&lt;br /&gt;  &lt;br /&gt;  if(root==NULL)return(NULL);&lt;br /&gt;   &lt;br /&gt;  temp = (mynode *) malloc(sizeof(mynode));&lt;br /&gt;  temp-&gt;value = root-&gt;value;&lt;br /&gt;&lt;br /&gt;  temp-&gt;left  = copy(root-&gt;right);&lt;br /&gt;  temp-&gt;right = copy(root-&gt;left);&lt;br /&gt;&lt;br /&gt;  return(temp);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This code will will only print the mirror of the tree&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void tree_mirror(struct node* node) &lt;br /&gt;{ &lt;br /&gt;   struct node *temp;&lt;br /&gt;&lt;br /&gt;   if (node==NULL) &lt;br /&gt;   { &lt;br /&gt;     return; &lt;br /&gt;   } &lt;br /&gt;   else &lt;br /&gt;   { &lt;br /&gt;      tree_mirror(node-&gt;left); &lt;br /&gt;      tree_mirror(node-&gt;right); &lt;br /&gt;&lt;br /&gt;      // Swap the pointers in this node &lt;br /&gt;      temp = node-&gt;left; &lt;br /&gt;      node-&gt;left = node-&gt;right; &lt;br /&gt;      node-&gt;right = temp; &lt;br /&gt;   } &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8458610940303538540?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8458610940303538540/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8458610940303538540' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8458610940303538540'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8458610940303538540'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-create-mirror-copy.html' title='Write a C program to create a mirror copy of a tree (left nodes become right and right nodes become left)!'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4970641196037050726</id><published>2007-08-15T04:27:00.001-07:00</published><updated>2007-08-15T04:27:47.060-07:00</updated><title type='text'>Write a C program to compute the maximum depth in a tree?</title><content type='html'>int maxDepth(struct node* node) &lt;br /&gt;{ &lt;br /&gt;  if (node==NULL) &lt;br /&gt;  { &lt;br /&gt;    return(0); &lt;br /&gt;  } &lt;br /&gt;  else &lt;br /&gt;  { &lt;br /&gt;    int leftDepth  = maxDepth(node-&gt;left); &lt;br /&gt;    int rightDepth = maxDepth(node-&gt;right); &lt;br /&gt;    if (leftDepth &gt; rightDepth) return(leftDepth+1); &lt;br /&gt;    else return(rightDepth+1); &lt;br /&gt;  } &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4970641196037050726?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4970641196037050726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4970641196037050726' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4970641196037050726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4970641196037050726'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-compute-maximum.html' title='Write a C program to compute the maximum depth in a tree?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1339712499168537098</id><published>2007-08-15T04:26:00.001-07:00</published><updated>2007-08-15T04:26:44.539-07:00</updated><title type='text'>Write a C program to find the mininum value in a binary search tree.</title><content type='html'>Here is some sample C code. The idea is to keep on moving till you hit the left most node in the tree&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int minValue(struct node* node) &lt;br /&gt;{ &lt;br /&gt;  struct node* current = node; &lt;br /&gt;&lt;br /&gt;  while (current-&gt;left != NULL) &lt;br /&gt;  { &lt;br /&gt;    current = current-&gt;left; &lt;br /&gt;  }&lt;br /&gt; &lt;br /&gt;  return(current-&gt;data); &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On similar lines, to find the maximum value, keep on moving till you hit the right most node of the tree.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1339712499168537098?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1339712499168537098/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1339712499168537098' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1339712499168537098'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1339712499168537098'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-find-mininum-value.html' title='Write a C program to find the mininum value in a binary search tree.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4485556749062338957</id><published>2007-08-15T04:24:00.000-07:00</published><updated>2007-08-15T04:25:12.262-07:00</updated><title type='text'>Write C code to determine if two trees are  identical .</title><content type='html'>Here is a C program using recursion&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int identical(struct node* a, struct node* b) &lt;br /&gt;{ &lt;br /&gt;  if (a==NULL &amp;&amp; b==NULL){return(true);} &lt;br /&gt;  else if (a!=NULL &amp;&amp; b!=NULL) &lt;br /&gt;  { &lt;br /&gt;    return(a-&gt;data == b-&gt;data &amp;&amp; &lt;br /&gt;           identical(a-&gt;left, b-&gt;left) &amp;&amp; &lt;br /&gt;           identical(a-&gt;right, b-&gt;right)); &lt;br /&gt;  } &lt;br /&gt;  else return(false); &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4485556749062338957?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4485556749062338957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4485556749062338957' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4485556749062338957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4485556749062338957'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-code-to-determine-if-two-trees.html' title='Write C code to determine if two trees are  identical .'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-808170397262913633</id><published>2007-08-15T04:23:00.000-07:00</published><updated>2007-08-15T04:24:07.882-07:00</updated><title type='text'>Write a C program to delete a tree (i.e, free up its nodes)</title><content type='html'>Free up the nodes using Postorder traversal!.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-808170397262913633?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/808170397262913633/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=808170397262913633' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/808170397262913633'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/808170397262913633'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-delete-tree-ie-free.html' title='Write a C program to delete a tree (i.e, free up its nodes)'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8451576067318632899</id><published>2007-08-15T04:21:00.001-07:00</published><updated>2007-08-15T04:21:57.852-07:00</updated><title type='text'>Write a C program to determine the number of elements (or size) in a tree.</title><content type='html'>int tree_size(struct node* node) &lt;br /&gt;{ &lt;br /&gt;  if (node==NULL) &lt;br /&gt;  { &lt;br /&gt;    return(0); &lt;br /&gt;  } &lt;br /&gt;  else &lt;br /&gt;  { &lt;br /&gt;    return(tree_size(node-&gt;left) + tree_size(node-&gt;right) + 1); &lt;br /&gt;  } &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8451576067318632899?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8451576067318632899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8451576067318632899' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8451576067318632899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8451576067318632899'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-determine-number-of.html' title='Write a C program to determine the number of elements (or size) in a tree.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-9122532727949981775</id><published>2007-08-15T04:13:00.000-07:00</published><updated>2007-08-15T04:14:55.879-07:00</updated><title type='text'>Write a C program to find the depth or height of a tree.</title><content type='html'>Here is some C code to get the height of the three&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;tree_height(mynode *p)&lt;br /&gt;{&lt;br /&gt;   if(p==NULL)return(0);&lt;br /&gt;   if(p-&gt;left){h1=tree_height(p-&gt;left);}&lt;br /&gt;   if(p=&gt;right){h2=tree_height(p-&gt;right);}&lt;br /&gt;   return(max(h1,h2)+1);&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The degree of the leaf is zero. The degree of a tree is the max of its element degrees. A binary tree of height n, h &gt; 0, has at least h and at most (2^h -1) elements in it. The height of a binary tree that contains n, n&gt;0, elements is at most n and atleast log(n+1) to the base 2.&lt;br /&gt;&lt;br /&gt;Log(n+1) to the base 2 = h&lt;br /&gt;&lt;br /&gt;n = (2^h - 1)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-9122532727949981775?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/9122532727949981775/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=9122532727949981775' title='206 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/9122532727949981775'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/9122532727949981775'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-c-program-to-find-depth-or-height.html' title='Write a C program to find the depth or height of a tree.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>206</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-134435806047034790</id><published>2007-08-15T04:08:00.001-07:00</published><updated>2007-08-15T04:08:52.008-07:00</updated><title type='text'>Write your own trim() or squeeze() function to remove the spaces from a string.</title><content type='html'>#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;char *trim(char *s);&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt;  char str1[]=" Hello   I am Good ";&lt;br /&gt;  printf("\n\nBefore trimming : [%s]", str1);&lt;br /&gt;  printf("\n\nAfter trimming  : [%s]", trim(str1));&lt;br /&gt;  &lt;br /&gt;  getch();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// The trim() function...&lt;br /&gt;&lt;br /&gt;char *trim(char *s) &lt;br /&gt;{ &lt;br /&gt;    char *p, *ps; &lt;br /&gt;    &lt;br /&gt;    for (ps = p = s; *s != '\0'; s++) &lt;br /&gt;    {&lt;br /&gt;        if (!isspace(*s)) &lt;br /&gt;        {&lt;br /&gt;            *p++ = *s; &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    *p = '\0'; &lt;br /&gt;&lt;br /&gt;    return(ps); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before trimming : [ Hello   I am Good ]&lt;br /&gt;After trimming  : [HelloIamGood]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Another version of this question requires one to reduce multiple spaces, tabs etc to single spaces...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-134435806047034790?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/134435806047034790/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=134435806047034790' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/134435806047034790'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/134435806047034790'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-your-own-trim-or-squeeze-function.html' title='Write your own trim() or squeeze() function to remove the spaces from a string.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8410152583554902780</id><published>2007-08-15T04:05:00.000-07:00</published><updated>2007-08-15T04:06:14.568-07:00</updated><title type='text'>How to add two numbers without using the plus operator?</title><content type='html'>Actually,&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SUM   = A XOR B&lt;br /&gt;CARRY = A AND B&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On a wicked note, you can add two numbers wihtout using the + operator as follows&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a - (- b)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8410152583554902780?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8410152583554902780/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8410152583554902780' title='102 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8410152583554902780'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8410152583554902780'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/how-to-add-two-numbers-without-using.html' title='How to add two numbers without using the plus operator?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>102</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8353804180059455616</id><published>2007-08-15T04:03:00.000-07:00</published><updated>2007-08-15T04:04:20.566-07:00</updated><title type='text'>How to generate prime numbers? How to generate the next prime after a given prime?</title><content type='html'>This is a very vast subject. There are numerous methods to generate primes or to find out if a given number is a prime number or not. Here are a few of them. I strongly recommend you to search on the Internet for more elaborate information.&lt;br /&gt;&lt;br /&gt;Brute Force&lt;br /&gt;Test each number starting with 2 and continuing up to the number of primes we want to generate. We divide each numbr by all divisors upto the square root of that number. If no factors are found, its a prime.&lt;br /&gt;&lt;br /&gt;Using only primes as divisors&lt;br /&gt;Test each candidate only with numbers that have been already proven to be prime. To do so, keep a list of already found primes (probably using an array, a file or bit fields).&lt;br /&gt;&lt;br /&gt;Test with odd candidates only&lt;br /&gt;We need not test even candidates at all. We could make 2 a special case and just print it, not include it in the list of primes and start our candidate search with 3 and increment by 2 instead of one everytime.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Table method&lt;br /&gt;Suppose we want to find all the primes between 1 and 64. We write out a table of these numbers, and proceed as follows. 2 is the first integer greater than 1, so its obviously prime. We now cross out all multiples of two. The next number we haven't crossed out is 3. We circle it and cross out all its multiples. The next non-crossed number is 5, sp we circle it and cross all its mutiples. We only have to do this for all numbers less than the square root of our upper limit, since any composite in the table must have atleast one factor less than the square root of the upper limit. Whats left after this process of elimination is all the prime numbers between 1 and 64.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8353804180059455616?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8353804180059455616/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8353804180059455616' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8353804180059455616'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8353804180059455616'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/how-to-generate-prime-numbers-how-to.html' title='How to generate prime numbers? How to generate the next prime after a given prime?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7915260913943558125</id><published>2007-08-15T03:44:00.000-07:00</published><updated>2007-08-15T03:46:57.670-07:00</updated><title type='text'>Write a program to print numbers from 1 to 100 without using loops!</title><content type='html'>Another "Yeah, I am a jerk, kick me! kind of a question. I simply dont know why they ask these questions. &lt;br /&gt;&lt;br /&gt;Nevertheless, for the benefit of mankind...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1 (Using recursion)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void printUp(int startNumber, int endNumber) &lt;br /&gt;{ &lt;br /&gt;  if (startNumber &gt; endNumber) &lt;br /&gt;    return; &lt;br /&gt;  &lt;br /&gt;  printf("[%d]\n", startNumber++); &lt;br /&gt;  printUp(startNumber, endNumber); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2 (Using goto)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void printUp(int startNumber, int endNumber) &lt;br /&gt;{ &lt;br /&gt;start: &lt;br /&gt;&lt;br /&gt;    if (startNumber &gt; endNumber) &lt;br /&gt;    {&lt;br /&gt;       goto end; &lt;br /&gt;    }&lt;br /&gt;    else &lt;br /&gt;    { &lt;br /&gt;       printf("[%d]\n", startNumber++); &lt;br /&gt;       goto start; &lt;br /&gt;    } &lt;br /&gt;&lt;br /&gt;end: &lt;br /&gt;    return;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7915260913943558125?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7915260913943558125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7915260913943558125' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7915260913943558125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7915260913943558125'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/08/write-program-to-print-numbers-from-1.html' title='Write a program to print numbers from 1 to 100 without using loops!'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8591081056334756388</id><published>2007-07-09T11:02:00.000-07:00</published><updated>2007-07-09T11:03:17.445-07:00</updated><title type='text'>Write a program to check if the stack grows up or down.</title><content type='html'>Try noting down the address of a local variable. Call another function with a local variable declared in it and check the address of that local variable and compare!.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#include &lt; stdlib.h &gt;&lt;br /&gt;&lt;br /&gt;void stack(int *local1);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  int local1;&lt;br /&gt;  stack(&amp;local1);&lt;br /&gt;  exit(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void stack(int *local1)&lt;br /&gt;{&lt;br /&gt;   int local2;&lt;br /&gt;   printf("\nAddress of first  local : [%u]", local1);&lt;br /&gt;   printf("\nAddress of second local : [%u]", &amp;local2); &lt;br /&gt;   if(local1 &lt; &amp;local2)&lt;br /&gt;   {&lt;br /&gt;     printf("\nStack is growing downwards.\n");&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;     printf("\nStack is growing upwards.\n");&lt;br /&gt;   }&lt;br /&gt;   printf("\n\n");&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8591081056334756388?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8591081056334756388/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8591081056334756388' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8591081056334756388'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8591081056334756388'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-program-to-check-if-stack-grows.html' title='Write a program to check if the stack grows up or down.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5452608779365259285</id><published>2007-07-09T11:00:00.000-07:00</published><updated>2007-07-09T11:01:05.435-07:00</updated><title type='text'>Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?</title><content type='html'>Here is a simple, yet efficient C program to accomplish the same...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#include &lt; conio.h &gt;&lt;br /&gt;&lt;br /&gt;int isSubset(char *a, char *b);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; char str1[]="defabc";&lt;br /&gt; char str2[]="abcfed";&lt;br /&gt;&lt;br /&gt; if(isSubset(str1, str2)==0)&lt;br /&gt; {&lt;br /&gt;   printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);&lt;br /&gt; }&lt;br /&gt; else&lt;br /&gt; {&lt;br /&gt;   printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; getch();&lt;br /&gt; return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to check if characters in "b" are a subset&lt;br /&gt;// of the characters in "a"&lt;br /&gt;&lt;br /&gt;int isSubset(char *a, char *b)&lt;br /&gt;{&lt;br /&gt; int letterPresent[256];&lt;br /&gt; int i;&lt;br /&gt;&lt;br /&gt; for(i=0; i &lt; 256; i++)&lt;br /&gt;    letterPresent[i]=0;&lt;br /&gt;&lt;br /&gt; for(i=0; a[i]!='\0'; i++)&lt;br /&gt;    letterPresent[a[i]]++;&lt;br /&gt;&lt;br /&gt; for(i=0; b[i]!='\0'; i++)&lt;br /&gt;    if(!letterPresent[b[i]])&lt;br /&gt;       return(1);&lt;br /&gt;&lt;br /&gt; return(0);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5452608779365259285?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5452608779365259285/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5452608779365259285' title='18 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5452608779365259285'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5452608779365259285'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/given-two-strings-and-b-how-would-you.html' title='Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>18</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-783836361451170026</id><published>2007-07-09T10:58:00.000-07:00</published><updated>2007-07-09T10:59:26.701-07:00</updated><title type='text'>How can we sum the digits of a given number in single statement?</title><content type='html'>Try something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;void main()&lt;br /&gt;{&lt;br /&gt;  int num=123456; &lt;br /&gt;  int sum=0;&lt;br /&gt;&lt;br /&gt;  for(;num &gt; 0;sum+=num%10,num/=10);  // This is the "single line".&lt;br /&gt;&lt;br /&gt;  printf("\nsum = [%d]\n", sum);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If there is a simpler way to do this, let me know!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-783836361451170026?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/783836361451170026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=783836361451170026' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/783836361451170026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/783836361451170026'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-can-we-sum-digits-of-given-number.html' title='How can we sum the digits of a given number in single statement?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8210360932491403901</id><published>2007-07-09T10:56:00.001-07:00</published><updated>2007-07-09T10:56:59.697-07:00</updated><title type='text'>Is there a way to multiply matrices in lesser than o(n^3) time complexity?</title><content type='html'>Yes. Divide and conquer method suggests Strassen's matrix multiplication method to be used. If we follow this method, the time complexity is O(n^2.81) times rather O(n^3) times. &lt;br /&gt;&lt;br /&gt;Here are some more details about this method. &lt;br /&gt;&lt;br /&gt;Suppose we want to multiply two matrices of size N x N: for example A x B = C&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[C11 C12]   [A11 A12] [B11 B12]&lt;br /&gt;[C21 C22] = [A21 A22] [B21 B22]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, this guy called Strassen's somehow :) came up with a bunch of equations to calculate the 4 elements of the resultant matrix&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C11 = a11*b11 + a12*b21 &lt;br /&gt;C12 = a11*b12 + a12*b22 &lt;br /&gt;C21 = a21*b11 + a22*b21 &lt;br /&gt;C22 = a21*b12 + a22*b22&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you are aware, the rudimentary matrix multiplication goes something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void matrix_mult()&lt;br /&gt;{&lt;br /&gt;  for (i = 1; i &lt;= N; i++) &lt;br /&gt;  {                                                                  &lt;br /&gt;     for (j = 1; j &lt;= N; j++) &lt;br /&gt;     {                   &lt;br /&gt;        compute Ci,j;                                                           &lt;br /&gt;     }&lt;br /&gt;  } &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So, essentially, a 2x2 matrix multiplication can be accomplished using 8 multiplications. And the complexity becomes &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2^log 8 =2^3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplications and 18 additions or subtractions. So now the complexity becomes&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2^log7 =2^2.807&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how he did it&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;P1 = (A11+ A22)(B11+B22) &lt;br /&gt;P2 = (A21 + A22) * B11 &lt;br /&gt;P3 = A11 * (B12 - B22) &lt;br /&gt;P4 = A22 * (B21 - B11) &lt;br /&gt;P5 = (A11 + A12) * B22 &lt;br /&gt;P6 = (A21 - A11) * (B11 + B12) &lt;br /&gt;P7 = (A12 - A22) * (B21 + B22) &lt;br /&gt;&lt;br /&gt;C11 = P1 + P4 - P5 + P7&lt;br /&gt;C12 = P3 + P5 &lt;br /&gt;C21 = P2 + P4 &lt;br /&gt;C22 = P1 + P3 - P2 + P6 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, there is no need to memorize this stuff!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8210360932491403901?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8210360932491403901/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8210360932491403901' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8210360932491403901'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8210360932491403901'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/is-there-way-to-multiply-matrices-in.html' title='Is there a way to multiply matrices in lesser than o(n^3) time complexity?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4042348207634349928</id><published>2007-07-09T10:54:00.001-07:00</published><updated>2007-07-09T10:54:55.846-07:00</updated><title type='text'>Write a simple piece of code to split a string at equal intervals.</title><content type='html'>Suppose you have a big string&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is a big string which I want to split at equal intervals, without caring about the words.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, to split this string say into smaller strings of 20 characters each, try this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#define maxLineSize 20&lt;br /&gt;&lt;br /&gt;split(char *string)&lt;br /&gt;{&lt;br /&gt;  int i, length;&lt;br /&gt;  char dest[maxLineSize + 1];&lt;br /&gt;&lt;br /&gt;  i           = 0;&lt;br /&gt;  length      = strlen(string);&lt;br /&gt;&lt;br /&gt;  while((i+maxLineSize) &lt;= length)&lt;br /&gt;  {&lt;br /&gt;     strncpy(dest, (string+i), maxLineSize);&lt;br /&gt;     dest[maxLineSize - 1] = '\0';&lt;br /&gt;     i = i + strlen(dest) - 1;&lt;br /&gt;     printf("\nChunk : [%s]\n", dest);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  strcpy(dest, (string + i));&lt;br /&gt;  printf("\nChunk : [%s]\n", dest);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4042348207634349928?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4042348207634349928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4042348207634349928' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4042348207634349928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4042348207634349928'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-simple-piece-of-code-to-split.html' title='Write a simple piece of code to split a string at equal intervals.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4589522447546096286</id><published>2007-07-09T10:52:00.000-07:00</published><updated>2007-07-09T10:53:11.323-07:00</updated><title type='text'>How to fast multiply a number by 7?</title><content type='html'>Try&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(num &lt;&lt; 3 - num)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is same as&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;num*8 - num = num * (8-1) = num * 7&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4589522447546096286?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4589522447546096286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4589522447546096286' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4589522447546096286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4589522447546096286'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-to-fast-multiply-number-by-7.html' title='How to fast multiply a number by 7?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-6827427240915078053</id><published>2007-07-09T10:50:00.000-07:00</published><updated>2007-07-09T10:51:36.467-07:00</updated><title type='text'>How do you get the line numbers in C?</title><content type='html'>Use the following Macros&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;__FILE__ Source file name (string constant) format "patx.c" &lt;br /&gt;__LINE__ Current source line number (integer) &lt;br /&gt;__DATE__ Date compiled (string constant)format "Dec 14 1985" &lt;br /&gt;__TIME__ Time compiled (string constant) format "15:24:26" &lt;br /&gt;__TIMESTAMP__ Compile date/time (string constant)format "Tue Nov 19 11:39:12 1997" &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Usage example&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;static char stamp[] =    "***\nmodule " __FILE__    "\ncompiled " __TIMESTAMP__     "\n***";&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{   &lt;br /&gt;   ...&lt;br /&gt;   &lt;br /&gt;   if ( (fp = fopen(fl,"r")) == NULL )   &lt;br /&gt;   {      &lt;br /&gt;      printf( "open failed, line %d\n%s\n",__LINE__, stamp );      &lt;br /&gt;      exit( 4 );   &lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And the output is something like&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;*** open failed, line 67 &lt;br /&gt;******&lt;br /&gt;module myfile.c &lt;br /&gt;compiled Mon Jan 15 11:15:56 1999 &lt;br /&gt;***&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-6827427240915078053?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/6827427240915078053/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=6827427240915078053' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6827427240915078053'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6827427240915078053'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-do-you-get-line-numbers-in-c.html' title='How do you get the line numbers in C?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7153916071357949262</id><published>2007-07-09T10:49:00.001-07:00</published><updated>2007-07-09T10:49:47.032-07:00</updated><title type='text'>How to scan a string till we hit a new line using scanf()?</title><content type='html'>Use&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;scanf("%[^\n]", address);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7153916071357949262?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7153916071357949262/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7153916071357949262' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7153916071357949262'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7153916071357949262'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-to-scan-string-till-we-hit-new-line.html' title='How to scan a string till we hit a new line using scanf()?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4812290997541906295</id><published>2007-07-09T10:46:00.000-07:00</published><updated>2007-07-09T10:47:14.882-07:00</updated><title type='text'>How to swap the two nibbles in a byte ?</title><content type='html'>Try this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;unsigned char swap_nibbles(unsigned char c)&lt;br /&gt;{&lt;br /&gt;  unsigned char temp1, temp2;&lt;br /&gt;  temp1 = c &amp; 0x0F;&lt;br /&gt;  temp2 = c &amp; 0xF0;&lt;br /&gt;  temp1=temp1 &lt;&lt; 4;&lt;br /&gt;  temp2=temp2 &gt;&gt; 4;  &lt;br /&gt;&lt;br /&gt;  return(temp2|temp1); //adding the bits&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;  char ch=0x34;&lt;br /&gt;  printf("\nThe exchanged value is %x",swap_nibbles(ch));&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4812290997541906295?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4812290997541906295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4812290997541906295' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4812290997541906295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4812290997541906295'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-to-swap-two-nibbles-in-byte.html' title='How to swap the two nibbles in a byte ?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1774827215880143720</id><published>2007-07-09T10:45:00.001-07:00</published><updated>2007-07-09T10:45:43.988-07:00</updated><title type='text'>Is there something we can do in C but not in C++?</title><content type='html'>I have a really funny answer&lt;br /&gt;&lt;br /&gt;Declare variable names that are keywords in C++ but not C.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;  int old, new=3;&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This will compile in C, but not in C++!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1774827215880143720?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1774827215880143720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1774827215880143720' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1774827215880143720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1774827215880143720'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/is-there-something-we-can-do-in-c-but.html' title='Is there something we can do in C but not in C++?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3811196523091789386</id><published>2007-07-09T10:37:00.000-07:00</published><updated>2007-07-09T10:38:57.429-07:00</updated><title type='text'>How do you compare floating point numbers?</title><content type='html'>This is Wrong!.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;double a, b;&lt;br /&gt;&lt;br /&gt;if(a == b)&lt;br /&gt;{&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above code might not work always. Thats because of the way floating point numbers are stored.&lt;br /&gt;&lt;br /&gt;A good way of comparing two floating point numbers is to have a accuracy threshold which is relative to the magnitude of the two floating point numbers being compared. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; math.h &gt;&lt;br /&gt;if(fabs(a - b) &lt;= accurary_threshold * fabs(a))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is a lot of material on the net to know how floating point numbers can be compared. Got for it if you really want to understand. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Another way which might work is something like this. I have not tested it!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int compareFloats(float f1, float f2)&lt;br /&gt;{&lt;br /&gt;  char *b1, *b2; &lt;br /&gt;  int i; &lt;br /&gt;&lt;br /&gt;  b1 = (char *)&amp;f1; &lt;br /&gt;  b2 = (char *)&amp;f2; &lt;br /&gt;&lt;br /&gt;  /* Assuming sizeof(float) is 4 bytes) */ &lt;br /&gt;&lt;br /&gt;  for (i = 0; i &lt; 4; i++, b1++, b2++) &lt;br /&gt;  {&lt;br /&gt;    if (*b1 != *b2) &lt;br /&gt;    {&lt;br /&gt;      return(NOT_EQUAL); /* You must have defined this before */ &lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return(EQUAL);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3811196523091789386?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3811196523091789386/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3811196523091789386' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3811196523091789386'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3811196523091789386'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-do-you-compare-floating-point.html' title='How do you compare floating point numbers?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4167471258783874388</id><published>2007-07-09T10:35:00.000-07:00</published><updated>2007-07-09T10:36:55.961-07:00</updated><title type='text'>Write a program to add two long positive numbers (each represented by linked lists).</title><content type='html'>Check out this simple implementation&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mynode *long_add(mynode *h1, mynode *h2, mynode *h3)&lt;br /&gt;{&lt;br /&gt;  mynode *c, *c1, *c2;&lt;br /&gt;  int sum, carry, digit;&lt;br /&gt; &lt;br /&gt;  carry = 0;&lt;br /&gt;  c1 = h1-&gt;next;&lt;br /&gt;  c2 = h2-&gt;next;&lt;br /&gt;  &lt;br /&gt;  while(c1 != h1 &amp;&amp; c2 != h2)&lt;br /&gt;  {&lt;br /&gt;     sum   = c1-&gt;value + c2-&gt;value + carry;&lt;br /&gt;     digit = sum % 10;&lt;br /&gt;     carry = sum / 10;&lt;br /&gt;&lt;br /&gt;     h3 = insertNode(digit, h3);&lt;br /&gt;  &lt;br /&gt;     c1 = c1-&gt;next;&lt;br /&gt;     c2 = c2-&gt;next;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  if(c1 != h1)&lt;br /&gt;  {&lt;br /&gt;     c = c1;&lt;br /&gt;     h = h1;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;     c = c2; &lt;br /&gt;     h = h2;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  while(c != h)&lt;br /&gt;  {&lt;br /&gt;    sum   = c-&gt;value + carry;&lt;br /&gt;    digit = sum % 10;&lt;br /&gt;    carry = sum / 10;&lt;br /&gt;    h3 = insertNode(digit, h3);&lt;br /&gt;    c = c-&gt;next;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  if(carry==1)&lt;br /&gt;  {&lt;br /&gt;     h3 = insertNode(carry, h3);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return(h3);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4167471258783874388?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4167471258783874388/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4167471258783874388' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4167471258783874388'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4167471258783874388'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-program-to-add-two-long-positive.html' title='Write a program to add two long positive numbers (each represented by linked lists).'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3293798616303871970</id><published>2007-07-09T10:32:00.000-07:00</published><updated>2007-07-09T10:35:12.098-07:00</updated><title type='text'>Write code to add two polynomials.</title><content type='html'>Here is some pseudocode&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mynode *polynomial_add(mynode *h1, mynode *h2, mynode *h3)&lt;br /&gt;{&lt;br /&gt;   mynode *p1, *p2;&lt;br /&gt;   int x1, x2, y1, y2, cf1, cf2, cf;&lt;br /&gt;&lt;br /&gt;   p1 = h1-&gt;next;&lt;br /&gt;  &lt;br /&gt;   while(p1!=h1)&lt;br /&gt;   {&lt;br /&gt;      x1  = p1-&gt;px;&lt;br /&gt;      y1  = p1-&gt;py;&lt;br /&gt;      cf1 = p1-&gt;cf;&lt;br /&gt;&lt;br /&gt;      // Search for this term in the second polynomial&lt;br /&gt;  &lt;br /&gt;      p2 = h2-&gt;next;&lt;br /&gt;   &lt;br /&gt;      while(p2 != h2)&lt;br /&gt;      {&lt;br /&gt;         x2  = p2-&gt;px;&lt;br /&gt;         y2  = p2-&gt;py;&lt;br /&gt;         cf2 = p2-&gt;cf;&lt;br /&gt;    &lt;br /&gt;         if(x1 == x2 &amp;&amp; y1 == y2)break;&lt;br /&gt;&lt;br /&gt;         p2 = p2-&gt;next;&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;      if(p2 != h2)&lt;br /&gt;      {&lt;br /&gt;          // We found something in the second polynomial.&lt;br /&gt;          &lt;br /&gt;          cf = cf1 + cf2;&lt;br /&gt;          p2-&gt;flag = 1;&lt;br /&gt; &lt;br /&gt;          if(cf!=0){h3=addNode(cf,x1,y1,h3);}&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;         h3=addNode(cf,x1,y1,h3);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      p1 = p1-&gt;next;&lt;br /&gt;&lt;br /&gt;   }//while&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   // Add the remaining elements of the second polynomail to the result&lt;br /&gt;&lt;br /&gt;   while(p2 != h2)&lt;br /&gt;   {&lt;br /&gt;      if(p2 -&gt; flag ==0)&lt;br /&gt;      {&lt;br /&gt;          h3=addNode(p2-&gt;cf, p2-&gt;px, p2-&gt;py, h3);&lt;br /&gt;      }&lt;br /&gt;      p2=p2-&gt;next;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return(h3);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3293798616303871970?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3293798616303871970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3293798616303871970' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3293798616303871970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3293798616303871970'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-code-to-add-two-polynomials.html' title='Write code to add two polynomials.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4092185058924680800</id><published>2007-07-09T10:30:00.000-07:00</published><updated>2007-07-09T10:31:22.647-07:00</updated><title type='text'>Write C code to implement the Binary Search algorithm.</title><content type='html'>Here is a C function&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int binarySearch(int arr[],int size, int item)&lt;br /&gt;{&lt;br /&gt;   int left, right, middle;&lt;br /&gt;   left  = 0;&lt;br /&gt;   right = size-1;&lt;br /&gt;&lt;br /&gt;   while(left &lt;= right)&lt;br /&gt;   {&lt;br /&gt;      middle = ((left + right)/2);&lt;br /&gt;&lt;br /&gt;      if(item == arr[middle])&lt;br /&gt;      {&lt;br /&gt;        return(middle);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      if(item &gt; arr[middle])&lt;br /&gt;      {&lt;br /&gt;        left  = middle+1;&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;        right = middle-1;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return(-1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that the Binary Search algorithm has a prerequisite that the array passed to it must be already sorted in ascending order. This will not work on an unsorted array. The complexity of this algorithm is O(log(n)).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4092185058924680800?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4092185058924680800/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4092185058924680800' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4092185058924680800'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4092185058924680800'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-implement-binary-search.html' title='Write C code to implement the Binary Search algorithm.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-6707327058967994701</id><published>2007-07-09T08:22:00.000-07:00</published><updated>2007-07-09T08:24:10.677-07:00</updated><title type='text'>Write a C program to check for palindromes.</title><content type='html'>An example of a palidrome is "avon sees nova"&lt;br /&gt;&lt;br /&gt;There a number of ways in which we can find out if a string is a palidrome or not. Here are a few sample C programs...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#include &lt; string.h &gt;&lt;br /&gt;#include &lt; stdlib.h &gt;&lt;br /&gt;#include &lt; ctype.h &gt;&lt;br /&gt;&lt;br /&gt;void isPalindrome(char *string);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  isPalindrome("avon sees nova");&lt;br /&gt;  isPalindrome("a");&lt;br /&gt;  isPalindrome("avon sies nova");&lt;br /&gt;  isPalindrome("aa");&lt;br /&gt;  isPalindrome("abc");&lt;br /&gt;  isPalindrome("aba");&lt;br /&gt;  isPalindrome("3a2");&lt;br /&gt;  exit(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void isPalindrome(char *string)&lt;br /&gt;{&lt;br /&gt;  char *start, *end;&lt;br /&gt;&lt;br /&gt;  if(string)&lt;br /&gt;  { &lt;br /&gt;     start = string;&lt;br /&gt;     end   = string + strlen(string) - 1;&lt;br /&gt;&lt;br /&gt;     while((*start == *end) &amp;&amp; (start!=end))&lt;br /&gt;     { &lt;br /&gt;       if(start &lt; end)start++;&lt;br /&gt;       if(end &gt; start)end--;&lt;br /&gt;     } &lt;br /&gt;&lt;br /&gt;     if(*start!=*end)&lt;br /&gt;     {&lt;br /&gt;        printf("\n[%s] - This is not a palidrome!\n", string);&lt;br /&gt;     }&lt;br /&gt;     else&lt;br /&gt;     {&lt;br /&gt;        printf("\n[%s] - This is a palidrome!\n", string);&lt;br /&gt;     }&lt;br /&gt;  }&lt;br /&gt;  printf("\n\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#include &lt; string.h &gt;&lt;br /&gt;#include &lt; stdlib.h &gt;&lt;br /&gt;#include &lt; ctype.h &gt;&lt;br /&gt;&lt;br /&gt;int isPalindrome(char string[]);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  isPalindrome("avon sees nova");&lt;br /&gt;  isPalindrome("a");&lt;br /&gt;  isPalindrome("avon sies nova");&lt;br /&gt;  isPalindrome("aa");&lt;br /&gt;  isPalindrome("abc");&lt;br /&gt;  isPalindrome("aba");&lt;br /&gt;  isPalindrome("3a2");&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int isPalindrome(char string[])&lt;br /&gt;{&lt;br /&gt;  int count, countback, end, N;&lt;br /&gt;  &lt;br /&gt;  N   = strlen(string);&lt;br /&gt;  end = N-1;&lt;br /&gt;&lt;br /&gt;  for((count=0, countback = end); count &lt;= (end/2); ++count,--countback)&lt;br /&gt;  {&lt;br /&gt;    if(string[count]!=string[countback])&lt;br /&gt;    {&lt;br /&gt;      return(1);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  printf("\n[%s] is a palidrome!\n", string);&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-6707327058967994701?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/6707327058967994701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=6707327058967994701' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6707327058967994701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6707327058967994701'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-check-for.html' title='Write a C program to check for palindromes.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4353714370004983160</id><published>2007-07-09T08:11:00.000-07:00</published><updated>2007-07-09T08:13:23.567-07:00</updated><title type='text'>Write a C program to multiply two matrices.</title><content type='html'>Are you sure you know this? A lot of people think they already know this, but guess what? So take a good look at this C program. Its asked in most of the interviews as a warm up question.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Matrix A (m*n)&lt;br /&gt;// Matrix B (n*k)&lt;br /&gt;// Matrix C (m*k)&lt;br /&gt;&lt;br /&gt;for(i=0; i &lt; m; i++)&lt;br /&gt;{&lt;br /&gt;   for(j=0;j &lt; k;j++)&lt;br /&gt;   {&lt;br /&gt;      c[i][j]=0;&lt;br /&gt;      for(l=0;l &lt; n;l++)&lt;br /&gt;           c[i][j] += a[i][l] * b[l][j];&lt;br /&gt;   }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4353714370004983160?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4353714370004983160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4353714370004983160' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4353714370004983160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4353714370004983160'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-multiply-two.html' title='Write a C program to multiply two matrices.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-554645344724617653</id><published>2007-07-09T08:10:00.000-07:00</published><updated>2007-07-09T08:11:16.619-07:00</updated><title type='text'>How would you find the size of structure without using sizeof()?</title><content type='html'>Try using pointers&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;struct MyStruct&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;  int j;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   struct MyStruct *p=0;&lt;br /&gt;   int size = ((char*)(p+1))-((char*)p);&lt;br /&gt;   printf("\nSIZE : [%d]\nSIZE : [%d]\n", size);&lt;br /&gt;   return 0;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-554645344724617653?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/554645344724617653/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=554645344724617653' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/554645344724617653'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/554645344724617653'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-would-you-find-size-of-structure.html' title='How would you find the size of structure without using sizeof()?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7906615079954771895</id><published>2007-07-09T08:05:00.000-07:00</published><updated>2007-07-09T08:08:26.651-07:00</updated><title type='text'>Write C code to dynamically allocate one, two and three dimensional arrays (using malloc()).</title><content type='html'>Its pretty simple to do this in the C language if you know how to use C pointers. Here are some example C code snipptes....&lt;br /&gt;&lt;br /&gt;One dimensional array&lt;br /&gt;&lt;br /&gt;int *myarray = malloc(no_of_elements * sizeof(int));&lt;br /&gt;&lt;br /&gt;//Access elements as myarray[i]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two dimensional array&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int **myarray = (int **)malloc(no_of_rows * sizeof(int *));&lt;br /&gt;for(i = 0; i &lt; no_of_rows; i++)&lt;br /&gt;{&lt;br /&gt; myarray[i] = malloc(no_of_columns * sizeof(int));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Access elements as myarray[i][j]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2 (keep the array's contents contiguous)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int **myarray = (int **)malloc(no_of_rows * sizeof(int *));&lt;br /&gt;myarray[0] = malloc(no_of_rows * no_of_columns * sizeof(int));&lt;br /&gt;&lt;br /&gt;for(i = 1; i &lt; no_of_rows; i++)&lt;br /&gt;  myarray[i] = myarray[0] + (i * no_of_columns);&lt;br /&gt;&lt;br /&gt;// Access elements as myarray[i][j]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int *myarray = malloc(no_of_rows * no_of_columns * sizeof(int));&lt;br /&gt;&lt;br /&gt;// Access elements using myarray[i * no_of_columns + j].&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Three dimensional array&lt;br /&gt;&lt;br /&gt;#define MAXX 3&lt;br /&gt;#define MAXY 4&lt;br /&gt;#define MAXZ 5&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;    int ***p,i,j;&lt;br /&gt;    p=(int ***) malloc(MAXX * sizeof(int ***));&lt;br /&gt;&lt;br /&gt;    for(i=0;i &lt; MAXX;i++)&lt;br /&gt;    {&lt;br /&gt;        p[i]=(int **)malloc(MAXY * sizeof(int *));&lt;br /&gt;        for(j=0;j &lt; MAXY;j++)&lt;br /&gt;            p[i][j]=(int *)malloc(MAXZ * sizeof(int));&lt;br /&gt;    }&lt;br /&gt;        &lt;br /&gt;    for(k=0;k &lt; MAXZ;k++)&lt;br /&gt;        for(i=0;i &lt; MAXX;i++)&lt;br /&gt;            for(j=0;j &lt; MAXY;j++)&lt;br /&gt;                p[i][j][k]= &lt; something &gt;;&lt;br /&gt;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7906615079954771895?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7906615079954771895/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7906615079954771895' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7906615079954771895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7906615079954771895'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-dynamically-allocate.html' title='Write C code to dynamically allocate one, two and three dimensional arrays (using malloc()).'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3198518095776863520</id><published>2007-07-09T07:45:00.001-07:00</published><updated>2007-07-09T07:45:58.317-07:00</updated><title type='text'>How do you initialize a pointer inside a function?</title><content type='html'>This is one of the very popular interview questions, so take a good look at it!.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;myfunction(int *ptr)&lt;br /&gt;{&lt;br /&gt;  int myvar = 100;&lt;br /&gt;  ptr = &amp;myvar;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt; int *myptr;&lt;br /&gt; myfunction(myptr);&lt;br /&gt;&lt;br /&gt; //Use pointer myptr.&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Do you think this works? It does not!.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Arguments in C are passed by value. The called function changed the passed copy of the pointer, and not the actual pointer. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are two ways around this problem&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;Pass in the address of the pointer to the function (the function needs to accept a pointer-to-a-pointer).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;calling_function()&lt;br /&gt;{&lt;br /&gt;  char *string;&lt;br /&gt;  return_string(/* Pass the address of the pointer */&amp;string);&lt;br /&gt;  printf(?\n[%s]\n?, string);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;boolean return_string(char **mode_string /*Pointer to a pointer! */)&lt;br /&gt;{&lt;br /&gt;  *string = (char *) malloc(100 * sizeof(char)); // Allocate memory to the pointer passed, not its copy.&lt;br /&gt;  DISCARD strcpy((char *)*string, (char *)?Something?);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;Make the function return the pointer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *myfunc()&lt;br /&gt;{&lt;br /&gt;  char *temp = "string";&lt;br /&gt;  return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  puts(myfunc());&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3198518095776863520?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3198518095776863520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3198518095776863520' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3198518095776863520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3198518095776863520'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-do-you-initialize-pointer-inside.html' title='How do you initialize a pointer inside a function?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3756315675078142651</id><published>2007-07-08T11:41:00.000-07:00</published><updated>2007-07-09T07:43:56.290-07:00</updated><title type='text'>Write code to remove duplicates in a sorted array.</title><content type='html'>There are a number of ways to remove duplicates from a sorted array. Here are a few C programs...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;In this simple C program, we change the original array and also send the new size of the array back to the caller.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;int removeDuplicates(int a[], int array_size);&lt;br /&gt;&lt;br /&gt;// The main function&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  &lt;br /&gt;  // Different test cases..&lt;br /&gt;  int my_array1[]    = {1, 1, 2, 3, 5, 6, 6, 7, 10, 25, 100, 123, 123};&lt;br /&gt;  int my_array1_size = 13;&lt;br /&gt;&lt;br /&gt;  int my_array2[]    = {1, 2, 3, 5, 6};&lt;br /&gt;  int my_array2_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array3[]    = {1, 1, 1, 1, 1};&lt;br /&gt;  int my_array3_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array4[]    = {123, 123};&lt;br /&gt;  int my_array4_size = 2;&lt;br /&gt;&lt;br /&gt;  int my_array5[]    = {1, 123, 123};&lt;br /&gt;  int my_array5_size = 3;&lt;br /&gt;&lt;br /&gt;  int my_array6[]    = {123, 123, 166};&lt;br /&gt;  int my_array6_size = 3;&lt;br /&gt;  &lt;br /&gt;  int my_array7[]    = {1, 2, 8, 8 , 24, 60, 60, 60, 60, 75, 100, 100, 123};&lt;br /&gt;  int my_array7_size = 13;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;  my_array1_size = removeDuplicates(my_array1, my_array1_size);&lt;br /&gt;  my_array2_size = removeDuplicates(my_array2, my_array2_size);&lt;br /&gt;  my_array3_size = removeDuplicates(my_array3, my_array3_size);&lt;br /&gt;  my_array4_size = removeDuplicates(my_array4, my_array4_size);&lt;br /&gt;  my_array5_size = removeDuplicates(my_array5, my_array5_size);&lt;br /&gt;  my_array6_size = removeDuplicates(my_array6, my_array6_size);  &lt;br /&gt;  my_array7_size = removeDuplicates(my_array7, my_array7_size);  &lt;br /&gt;  &lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to remove the duplicates&lt;br /&gt;int removeDuplicates(int a[], int array_size) &lt;br /&gt;{ &lt;br /&gt;  int i, j;&lt;br /&gt;  &lt;br /&gt;  j = 0; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  // Print old array...&lt;br /&gt;  printf("\n\nOLD : ");&lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;     printf("[%d] ", a[i]);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  // Remove the duplicates ...&lt;br /&gt;  for (i = 1; i &lt; array_size; i++) &lt;br /&gt;  { &lt;br /&gt;    if (a[i] != a[j]) &lt;br /&gt;    { &lt;br /&gt;      j++; &lt;br /&gt;      a[j] = a[i]; // Move it to the front &lt;br /&gt;    } &lt;br /&gt;  } &lt;br /&gt;&lt;br /&gt;  // The new array size..&lt;br /&gt;  array_size = (j + 1);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  // Print new array...&lt;br /&gt;  printf("\n\nNEW : ");&lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;     printf("[%d] ", a[i]);&lt;br /&gt;  }&lt;br /&gt;  printf("\n\n");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  // Return the new size...&lt;br /&gt;  return(j + 1); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [1] [1] [2] [3] [5] [6] [6] [7] [10] [25] [100] [123] [123]&lt;br /&gt;NEW : [1] [2] [3] [5] [6] [7] [10] [25] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [1] [2] [3] [5] [6]&lt;br /&gt;NEW : [1] [2] [3] [5] [6]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [1] [1] [1] [1] [1]&lt;br /&gt;NEW : [1]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [123] [123]&lt;br /&gt;NEW : [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [1] [123] [123]&lt;br /&gt;NEW : [1] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [123] [123] [166]&lt;br /&gt;NEW : [123] [166]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OLD : [1] [2] [8] [8] [24] [60] [60] [60] [60] [75] [100] [100] [123]&lt;br /&gt;NEW : [1] [2] [8] [24] [60] [75] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;If we dont want to change the input array and just want to print the array without any duplicates, the solution is very simple. Check out the removeDuplicatesNoModify() function in the program below. It keeps a track of the most recently seen number and does not print any duplicates of it when traversing the sorted array.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;void removeDuplicatesNoModify(int my_array[], int my_array1_size);&lt;br /&gt;void print_array(int array[], int array_size, int current_pos, int dup_start, int dup_end);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// The main function&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  // Different inputs...&lt;br /&gt;  int my_array1[]    = {1, 1, 2, 3, 5, 6, 6, 7, 10, 25, 100, 123, 123};&lt;br /&gt;  int my_array1_size = 13;&lt;br /&gt;&lt;br /&gt;  int my_array2[]    = {1, 2, 3, 5, 6};&lt;br /&gt;  int my_array2_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array3[]    = {1, 1, 1, 1, 1};&lt;br /&gt;  int my_array3_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array4[]    = {123, 123};&lt;br /&gt;  int my_array4_size = 2;&lt;br /&gt;&lt;br /&gt;  int my_array5[]    = {1, 123, 123};&lt;br /&gt;  int my_array5_size = 3;&lt;br /&gt;&lt;br /&gt;  int my_array6[]    = {123, 123, 166};&lt;br /&gt;  int my_array6_size = 3;&lt;br /&gt;  &lt;br /&gt;  int my_array7[]    = {1, 2, 8, 8 , 24, 60, 60, 60, 60, 75, 100, 100, 123};&lt;br /&gt;  int my_array7_size = 13;&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;  removeDuplicatesNoModify(my_array1, my_array1_size);&lt;br /&gt;  removeDuplicatesNoModify(my_array2, my_array2_size);&lt;br /&gt;  removeDuplicatesNoModify(my_array3, my_array3_size);&lt;br /&gt;  removeDuplicatesNoModify(my_array4, my_array4_size);&lt;br /&gt;  removeDuplicatesNoModify(my_array5, my_array5_size);&lt;br /&gt;  removeDuplicatesNoModify(my_array6, my_array6_size);  &lt;br /&gt;  removeDuplicatesNoModify(my_array7, my_array7_size);  &lt;br /&gt;&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//  This function just prints the array without duplicates.&lt;br /&gt;//  It does not modify the original array!&lt;br /&gt;&lt;br /&gt;void removeDuplicatesNoModify(int array[], int array_size) &lt;br /&gt;{   &lt;br /&gt;   int i, last_seen_unique;&lt;br /&gt;&lt;br /&gt;   if(array_size &lt;= 1){return;}&lt;br /&gt;   &lt;br /&gt;   last_seen_unique = array[0];&lt;br /&gt;   &lt;br /&gt;   printf("\n\nOld : ", array_size);&lt;br /&gt;&lt;br /&gt;   for(i = 0; i &lt; array_size; i++)&lt;br /&gt;   {&lt;br /&gt;     printf("[%2d] ", array[i]);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\nNew : ", array_size);&lt;br /&gt;&lt;br /&gt;   printf("[%2d] ", array[0]);&lt;br /&gt;   for(i=1; i &lt; array_size; i++)&lt;br /&gt;   {&lt;br /&gt;       if(array[i]!=last_seen_unique)&lt;br /&gt;       {&lt;br /&gt;          printf("[%2d] ", array[i]);&lt;br /&gt;          last_seen_unique = array[i];&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;   &lt;br /&gt;   printf("\n");&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output..&lt;br /&gt;&lt;br /&gt;Old : [ 1] [ 1] [ 2] [ 3] [ 5] [ 6] [ 6] [ 7] [10] [25] [100] [123] [123]&lt;br /&gt;New : [ 1] [ 2] [ 3] [ 5] [ 6] [ 7] [10] [25] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [ 1] [ 2] [ 3] [ 5] [ 6]&lt;br /&gt;New : [ 1] [ 2] [ 3] [ 5] [ 6]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [ 1] [ 1] [ 1] [ 1] [ 1]&lt;br /&gt;New : [ 1]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [123] [123]&lt;br /&gt;New : [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [ 1] [123] [123]&lt;br /&gt;New : [ 1] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [123] [123] [166]&lt;br /&gt;New : [123] [166]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [ 1] [ 2] [ 8] [ 8] [24] [60] [60] [60] [60] [75] [100] [100] [123]&lt;br /&gt;New : [ 1] [ 2] [ 8] [24] [60] [75] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;Here is a slightly compilcated, but more visual version of the removeDuplicates() function. It shrinks the original array as and when it find duplicates. It is also optimized to identify continuous strings of duplicates and eliminate them at one shot.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;void removeDuplicates(int array[], int *array_size) ;&lt;br /&gt;void print_array(int array[], int array_size, int current_pos, int dup_start, int dup_end);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// The main function&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  // Different inputs...&lt;br /&gt;  int my_array1[]    = {1, 1, 2, 3, 5, 6, 6, 7, 10, 25, 100, 123, 123};&lt;br /&gt;  int my_array1_size = 13;&lt;br /&gt;&lt;br /&gt;  int my_array2[]    = {1, 2, 3, 5, 6};&lt;br /&gt;  int my_array2_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array3[]    = {1, 1, 1, 1, 1};&lt;br /&gt;  int my_array3_size = 5;&lt;br /&gt;&lt;br /&gt;  int my_array4[]    = {123, 123};&lt;br /&gt;  int my_array4_size = 2;&lt;br /&gt;&lt;br /&gt;  int my_array5[]    = {1, 123, 123};&lt;br /&gt;  int my_array5_size = 3;&lt;br /&gt;&lt;br /&gt;  int my_array6[]    = {123, 123, 166};&lt;br /&gt;  int my_array6_size = 3;&lt;br /&gt;  &lt;br /&gt;  int my_array7[]    = {1, 2, 8, 8 , 24, 60, 60, 60, 60, 75, 100, 100, 123};&lt;br /&gt;  int my_array7_size = 13;&lt;br /&gt;&lt;br /&gt;  removeDuplicates(my_array1, &amp;my_array1_size);&lt;br /&gt;  removeDuplicates(my_array2, &amp;my_array2_size);&lt;br /&gt;  removeDuplicates(my_array3, &amp;my_array3_size);&lt;br /&gt;  removeDuplicates(my_array4, &amp;my_array4_size);&lt;br /&gt;  removeDuplicates(my_array5, &amp;my_array5_size);&lt;br /&gt;  removeDuplicates(my_array6, &amp;my_array6_size);  &lt;br /&gt;  removeDuplicates(my_array7, &amp;my_array7_size);  &lt;br /&gt;  &lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Changes the original array and resets the size of the array if duplicates&lt;br /&gt;// have been removed.&lt;br /&gt;&lt;br /&gt;void removeDuplicates(int array[], int *array_size) &lt;br /&gt;{   &lt;br /&gt;   int i, j, k, l; &lt;br /&gt;   int current_pos;&lt;br /&gt;   int dup_start;&lt;br /&gt;   int dup_end;&lt;br /&gt;&lt;br /&gt;   printf("\nInitial array (size : [%d])\n\n", *array_size);&lt;br /&gt;   for(i = 0; i &lt; *array_size; i++)&lt;br /&gt;   {&lt;br /&gt;     printf("[%2d] ", array[i]);&lt;br /&gt;   }&lt;br /&gt;   printf("\n\n\n------------------------------------------------\n");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   if(*array_size == 1){return;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   // Remove the dups...&lt;br /&gt;   for (i = 0; (i &lt; *array_size); i++) &lt;br /&gt;   { &lt;br /&gt;      //Start with the next element in the array and check if its a duplicate...&lt;br /&gt;      for(j = i+1; (j &lt; *array_size); j++)&lt;br /&gt;      {&lt;br /&gt;         if(array[i]!=array[j])&lt;br /&gt;         {  &lt;br /&gt;            // No duplicate, just continue...&lt;br /&gt;            break;&lt;br /&gt;         }&lt;br /&gt;         else&lt;br /&gt;         {&lt;br /&gt;            // The next element is a duplicate. &lt;br /&gt;            // See if there are more duplicates, as we want to optimize here. &lt;br /&gt;            //&lt;br /&gt;            // That is, if we have something like &lt;br /&gt;            // &lt;br /&gt;            // Array : [1, 1, 1, 2]&lt;br /&gt;            // &lt;br /&gt;            // then, we want to copy 2 directly in the second position and reduce the &lt;br /&gt;            // array to &lt;br /&gt;            // &lt;br /&gt;            // Array : [1, 2].&lt;br /&gt;            // &lt;br /&gt;            // in a single iteration.&lt;br /&gt;            &lt;br /&gt;            current_pos = i;&lt;br /&gt;            dup_start = j;&lt;br /&gt;            &lt;br /&gt;            j++;&lt;br /&gt;            &lt;br /&gt;            while((array[i]==array[j]) &amp;&amp; (j &lt; *array_size))&lt;br /&gt;            {&lt;br /&gt;              j++;&lt;br /&gt;            }&lt;br /&gt;            &lt;br /&gt;            dup_end = j-1;&lt;br /&gt;            &lt;br /&gt;            print_array(array, *array_size, current_pos, dup_start, dup_end);&lt;br /&gt;            &lt;br /&gt;            // Now remove elements of the array from "dup_start" to "dup_end"&lt;br /&gt;            // and shrink the size of the array.&lt;br /&gt;         &lt;br /&gt;            for(k = (dup_end + 1), l = dup_start ; k &lt; *array_size;)&lt;br /&gt;            {&lt;br /&gt;               array[l++]=array[k++];&lt;br /&gt;            } &lt;br /&gt;            &lt;br /&gt;            // Reduce the array size by the number of elements removed.&lt;br /&gt;            *array_size = *array_size - (dup_end - dup_start + 1);&lt;br /&gt;            &lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;   } &lt;br /&gt;   &lt;br /&gt;   printf("\n\n------------------------------------------------");&lt;br /&gt;   printf("\n\nFinal array (size : [%d])\n\n", *array_size);&lt;br /&gt;   for(i = 0; i &lt; *array_size; i++)&lt;br /&gt;   {&lt;br /&gt;     printf("[%2d] ", array[i]);&lt;br /&gt;   }&lt;br /&gt;   printf("\n\n");&lt;br /&gt;   &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// This function prints the array with some special pointers to the numbers that&lt;br /&gt;// are duplicated. &lt;br /&gt;// &lt;br /&gt;// Dont bother too much about this function, it just helps in understanding&lt;br /&gt;// how and where the duplicates are being removed from.&lt;br /&gt;&lt;br /&gt;void print_array(int array[], int array_size, int current_pos, int dup_start, int dup_end)&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;  &lt;br /&gt;  printf("\n\n");&lt;br /&gt;  &lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;    printf("[%2d] ", array[i]);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  printf("\n");&lt;br /&gt;  &lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;    if((i == current_pos) || &lt;br /&gt;       (i == dup_start &amp;&amp; i == dup_end) || &lt;br /&gt;       ((i == dup_start || i == dup_end) &amp;&amp; (dup_start != dup_end)))&lt;br /&gt;    {&lt;br /&gt;       printf("  ^  ");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       printf("     ");&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  printf("\n");&lt;br /&gt;  &lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;    if((i == current_pos) || &lt;br /&gt;       (i == dup_start &amp;&amp; i == dup_end) || &lt;br /&gt;       ((i == dup_start || i == dup_end) &amp;&amp; (dup_start != dup_end)))&lt;br /&gt;    {&lt;br /&gt;       printf("  |  ");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       printf("     ");&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  printf("\n");&lt;br /&gt;  &lt;br /&gt;  for(i = 0; i &lt; array_size; i++)&lt;br /&gt;  {&lt;br /&gt;    if(i == current_pos)&lt;br /&gt;    {&lt;br /&gt;       printf("  C  ");&lt;br /&gt;    }&lt;br /&gt;    else if(i == dup_start &amp;&amp; i == dup_end)&lt;br /&gt;    {&lt;br /&gt;       printf(" S/E ");&lt;br /&gt;    }&lt;br /&gt;    else if((i == dup_start || i == dup_end) &amp;&amp; (dup_start != dup_end))&lt;br /&gt;    {&lt;br /&gt;       if(i == dup_start)&lt;br /&gt;       {&lt;br /&gt;          printf("  S--");&lt;br /&gt;       }&lt;br /&gt;       else&lt;br /&gt;       {&lt;br /&gt;          printf("--E  ");&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;    else if(i &gt; dup_start &amp;&amp; i &lt; dup_end)&lt;br /&gt;    {&lt;br /&gt;       printf("-----");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       printf("     ");&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output (for one of the inputs)...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C - Current position.&lt;br /&gt;S - Start of duplicates.&lt;br /&gt;E - End of duplicates.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old : [ 1] [ 2] [ 8] [ 8] [24] [60] [60] [60] [60] [75] [100] [100] [123]&lt;br /&gt;&lt;br /&gt;-------------------------------------------------------------------------&lt;br /&gt;[ 1] [ 2] [ 8 (C) ] [ 8   (S/E) ] [24] [60] [60] [60] [60] [75] [100] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[ 1] [ 2] [ 8] [24] [60 (C)] [60 (S)] [60 (E)] [60] [75] [100] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[ 1] [ 2] [ 8] [24] [60] [75] [100  (C)] [100 (S/E)] [123]&lt;br /&gt;                               &lt;br /&gt;-------------------------------------------------------------------------&lt;br /&gt;New : [ 1] [ 2] [ 8] [24] [60] [75] [100] [123]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If there are other elegant methods of removing duplicate numbers from an array, please let me know!.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3756315675078142651?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3756315675078142651/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3756315675078142651' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3756315675078142651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3756315675078142651'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-code-to-remove-duplicates-in.html' title='Write code to remove duplicates in a sorted array.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1000434535470781491</id><published>2007-07-08T10:53:00.000-07:00</published><updated>2007-07-08T10:54:35.445-07:00</updated><title type='text'>Finding a duplicated integer problem.</title><content type='html'>Given an array of n integers from 1 to n with one integer repeated..&lt;br /&gt;&lt;br /&gt;Here is the simplest of C programs (kind of dumb)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#include &lt; stdlib.h &gt;&lt;br /&gt;#include &lt; conio.h &gt;&lt;br /&gt;&lt;br /&gt;int i,j=0,k,a1[10];&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;     printf("Enter the array of numbers between 1 and 100(you can repeat the numbers):");&lt;br /&gt;     for(i=0;i &lt;= 9;i++)&lt;br /&gt;     {&lt;br /&gt;         scanf("%d",&amp;a1[i]);&lt;br /&gt;     }&lt;br /&gt;     &lt;br /&gt;     while(j &lt; 10)&lt;br /&gt;     {&lt;br /&gt;        for(k=0;k &lt; 10;k++)&lt;br /&gt;        {&lt;br /&gt;            if(a1[j]==a1[k] &amp;&amp; j!=k)&lt;br /&gt;            {&lt;br /&gt;                printf("Duplicate found!");&lt;br /&gt;                printf("The duplicate is %d\n",a1[j]);&lt;br /&gt;                getch();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        j=j+1;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    getch();&lt;br /&gt;    return(0);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1000434535470781491?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1000434535470781491/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1000434535470781491' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1000434535470781491'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1000434535470781491'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/finding-duplicated-integer-problem.html' title='Finding a duplicated integer problem.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7744294523514334207</id><published>2007-07-08T10:51:00.001-07:00</published><updated>2007-07-08T10:51:52.633-07:00</updated><title type='text'>Write a C program to find the GCD of two numbers.</title><content type='html'>Here is a C program .... &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;int gcd(int a, int b);&lt;br /&gt;int gcd_recurse(int a, int b);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 6,4,  gcd(6,4));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 4,6,  gcd(4,6));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 3,17, gcd(3,17));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 17,3, gcd(17,3));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 1,6,  gcd(1,6));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 10,1, gcd(10,1));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 10,6, gcd(10,6));&lt;br /&gt;&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 6,4,  gcd_recurse(6,4));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 4,6,  gcd_recurse(4,6));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 3,17, gcd_recurse(3,17));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 17,3, gcd_recurse(17,3));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 1,6,  gcd_recurse(1,6));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 10,1, gcd_recurse(10,1));&lt;br /&gt;  printf("\nGCD(%2d,%2d) = [%d]", 10,6, gcd_recurse(10,6));&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;  getch();&lt;br /&gt;  getch();&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Iterative algorithm&lt;br /&gt;int gcd(int a, int b)&lt;br /&gt;{&lt;br /&gt;   int temp;&lt;br /&gt;   &lt;br /&gt;   while(b)&lt;br /&gt;   {&lt;br /&gt;      temp = a % b; &lt;br /&gt;      a = b; &lt;br /&gt;      b = temp;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return(a);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Recursive algorithm&lt;br /&gt;int gcd_recurse(int a, int b)&lt;br /&gt;{&lt;br /&gt;   int temp;&lt;br /&gt;&lt;br /&gt;   temp = a % b;&lt;br /&gt;&lt;br /&gt;   if (temp == 0)&lt;br /&gt;   {&lt;br /&gt;      return(b);&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;      return(gcd_recurse(b, temp));&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output ...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Iterative&lt;br /&gt;----------------&lt;br /&gt;GCD( 6, 4) = [2]&lt;br /&gt;GCD( 4, 6) = [2]&lt;br /&gt;GCD( 3,17) = [1]&lt;br /&gt;GCD(17, 3) = [1]&lt;br /&gt;GCD( 1, 6) = [1]&lt;br /&gt;GCD(10, 1) = [1]&lt;br /&gt;GCD(10, 6) = [2]&lt;br /&gt;&lt;br /&gt;Recursive&lt;br /&gt;----------------&lt;br /&gt;GCD( 6, 4) = [2]&lt;br /&gt;GCD( 4, 6) = [2]&lt;br /&gt;GCD( 3,17) = [1]&lt;br /&gt;GCD(17, 3) = [1]&lt;br /&gt;GCD( 1, 6) = [1]&lt;br /&gt;GCD(10, 1) = [1]&lt;br /&gt;GCD(10, 6) = [2]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that you should add error handling to check if someone has passed negative numbers and zero.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7744294523514334207?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7744294523514334207/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7744294523514334207' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7744294523514334207'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7744294523514334207'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-find-gcd-of-two.html' title='Write a C program to find the GCD of two numbers.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2616496051122052413</id><published>2007-07-08T10:48:00.000-07:00</published><updated>2007-07-08T10:49:46.036-07:00</updated><title type='text'>Write C code to check if an integer is a power of 2 or not in a single line?</title><content type='html'>Even this is one of the most frequently asked interview questions. I really dont know whats so great in it. Nevertheless, here is a C program&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if(!(num &amp; (num - 1)) &amp;&amp; num)&lt;br /&gt;{&lt;br /&gt;  // Power of 2!&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if(((~i+1)&amp;i)==i)&lt;br /&gt;{&lt;br /&gt; //Power of 2!&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I leave it up to you to find out how these statements work.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2616496051122052413?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2616496051122052413/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2616496051122052413' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2616496051122052413'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2616496051122052413'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-check-if-integer-is.html' title='Write C code to check if an integer is a power of 2 or not in a single line?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8946169153675733930</id><published>2007-07-08T10:46:00.000-07:00</published><updated>2007-07-08T10:48:02.430-07:00</updated><title type='text'>Write a C progam to convert from decimal to any base (binary, hex, oct etc...).</title><content type='html'>Here is some really cool C code&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  decimal_to_anybase(10, 2);&lt;br /&gt;  decimal_to_anybase(255, 16);&lt;br /&gt;  getch();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;decimal_to_anybase(int n, int base)&lt;br /&gt;{&lt;br /&gt;  int i, m, digits[1000], flag;&lt;br /&gt;  i=0;&lt;br /&gt;  &lt;br /&gt;  printf("\n\n[%d] converted to base [%d] : ", n, base);&lt;br /&gt;  &lt;br /&gt;  while(n)&lt;br /&gt;  {&lt;br /&gt;     m=n%base;&lt;br /&gt;     digits[i]="0123456789abcdefghijklmnopqrstuvwxyz"[m];&lt;br /&gt;     n=n/base;&lt;br /&gt;     i++;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   //Eliminate any leading zeroes&lt;br /&gt;   for(i--;i &gt;= 0;i--)&lt;br /&gt;   {&lt;br /&gt;     if(!flag &amp;&amp; digits[i]!='0')flag=1;&lt;br /&gt;     if(flag)printf("%c",digits[i]);&lt;br /&gt;   }   &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8946169153675733930?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8946169153675733930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8946169153675733930' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8946169153675733930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8946169153675733930'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-progam-to-convert-from-decimal.html' title='Write a C progam to convert from decimal to any base (binary, hex, oct etc...).'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7379713400506061555</id><published>2007-07-08T10:44:00.000-07:00</published><updated>2007-07-08T10:45:50.722-07:00</updated><title type='text'>Write a C program which produces its own source code as its output.</title><content type='html'>This is one of the most famous interview questions&lt;br /&gt;&lt;br /&gt;One of the famous C programs is...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So how does it work? &lt;br /&gt;&lt;br /&gt;It's not difficult to understand this program. In the following statement, &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;printf(f,34,f,34,10); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;the parameter "f" not only acts as the format string, but also as a value for the %s specifier. The ASCII value of double quotes is 34, and that of new-line is 10. With these fact ready, the solution is just a matter of tracing the program.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7379713400506061555?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7379713400506061555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7379713400506061555' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7379713400506061555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7379713400506061555'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-which-produces-its-own.html' title='Write a C program which produces its own source code as its output.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5140290137511035580</id><published>2007-07-08T10:41:00.000-07:00</published><updated>2007-07-08T10:43:10.874-07:00</updated><title type='text'>Write C code to return a string from a function.</title><content type='html'>This is one of the most popular interview questions&lt;br /&gt;&lt;br /&gt;This C program wont work!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *myfunction(int n)&lt;br /&gt;{&lt;br /&gt;   char buffer[20];&lt;br /&gt;   sprintf(buffer, "%d", n);&lt;br /&gt;   return retbuf; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This wont work either!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *myfunc1()&lt;br /&gt;{&lt;br /&gt;  char temp[] = "string";&lt;br /&gt;  return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *myfunc2()&lt;br /&gt;{&lt;br /&gt;   char temp[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};&lt;br /&gt;   return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    puts(myfunc1());&lt;br /&gt;    puts(myfunc2());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The returned pointer should be to a static buffer (like static char buffer[20];), or to a buffer passed in by the caller function, or to memory obtained using malloc(), but not to a local array. &lt;br /&gt;&lt;br /&gt;This will work&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *myfunc()&lt;br /&gt;{&lt;br /&gt;   char *temp = "string";&lt;br /&gt;   return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   puts(someFun());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So will this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;calling_function()&lt;br /&gt;{ &lt;br /&gt;  char *string; &lt;br /&gt;  return_string(&amp;string); &lt;br /&gt;  printf(?\n[%s]\n?, string);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;boolean return_string(char **mode_string /*Pointer to a pointer! */) &lt;br /&gt;{ &lt;br /&gt;   *string = (char *) malloc(100 * sizeof(char)); &lt;br /&gt;   DISCARD strcpy((char *)*string, (char *)?Something?); &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5140290137511035580?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5140290137511035580/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5140290137511035580' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5140290137511035580'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5140290137511035580'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-return-string-from.html' title='Write C code to return a string from a function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1284606467268985306</id><published>2007-07-08T10:39:00.000-07:00</published><updated>2007-07-08T10:40:19.635-07:00</updated><title type='text'>Write C code to solve the Tower of Hanoi problem.</title><content type='html'>Here is an example C program to solve the Tower Of Hanoi problem...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;    towers_of_hanio(n,'L','R','C');&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;towers_of_hanio(int n, char from, char to, char temp)&lt;br /&gt;{&lt;br /&gt;    if(n &gt; 0)&lt;br /&gt;    {&lt;br /&gt;        tower_of_hanio(n-1, from, temp, to);&lt;br /&gt;        printf("\nMove disk %d from %c to %c\n", n, from, to);&lt;br /&gt;        tower_of_hanio(n-1, temp, to, from);&lt;br /&gt;    }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1284606467268985306?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1284606467268985306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1284606467268985306' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1284606467268985306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1284606467268985306'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-solve-tower-of-hanoi.html' title='Write C code to solve the Tower of Hanoi problem.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2433759459906314202</id><published>2007-07-08T10:36:00.000-07:00</published><updated>2007-07-08T10:37:54.365-07:00</updated><title type='text'>What Little-Endian and Big-Endian? How can I determine whether a machine's byte order is big-endian or little endian? How can we convert from one to a</title><content type='html'>First of all, Do you know what Little-Endian and Big-Endian mean? &lt;br /&gt;&lt;br /&gt;Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first. &lt;br /&gt;&lt;br /&gt;For example, a 4 byte, 32-bit integer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Byte3 Byte2 Byte1 Byte0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;will be arranged in memory as follows: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Base_Address+0   Byte0   &lt;br /&gt;    Base_Address+1   Byte1   &lt;br /&gt;    Base_Address+2   Byte2   &lt;br /&gt;    Base_Address+3   Byte3   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Intel processors use "Little Endian" byte order.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  Base_Address+0   Byte3&lt;br /&gt;  Base_Address+1   Byte2&lt;br /&gt;  Base_Address+2   Byte1&lt;br /&gt;  Base_Address+3   Byte0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Motorola, Solaris processors use "Big Endian" byte order.&lt;br /&gt;&lt;br /&gt;In "Little Endian" form, code which picks up a 1, 2, 4, or longer byte number proceed in the same way for all formats. They first pick up the lowest order byte at offset 0 and proceed from there. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision mathematic routines are easy to code. In "Big Endian" form, since the high-order byte comes first, the code can test whether the number is positive or negative by looking at the byte at offset zero. Its not required to know how long the number is, nor does the code have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some code to determine what is the type of your machine&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int num = 1;&lt;br /&gt;if(*(char *)&amp;num == 1)&lt;br /&gt;{&lt;br /&gt;  printf("\nLittle-Endian\n");&lt;br /&gt;}&lt;br /&gt;else &lt;br /&gt;{&lt;br /&gt;  printf("Big-Endian\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is some code to convert from one Endian to another.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int myreversefunc(int num)&lt;br /&gt;{&lt;br /&gt;  int byte0, byte1, byte2, byte3;    &lt;br /&gt;&lt;br /&gt;  byte0 = (num &amp; x000000FF) &gt;&gt;  0 ;&lt;br /&gt;  byte1 = (num &amp; x0000FF00) &gt;&gt;  8 ;&lt;br /&gt;  byte2 = (num &amp; x00FF0000) &gt;&gt; 16 ;&lt;br /&gt;  byte3 = (num &amp; xFF000000) &gt;&gt; 24 ;&lt;br /&gt;&lt;br /&gt;  return((byte0 &lt;&lt; 24) | (byte1 &lt;&lt; 16) | (byte2 &lt;&lt; 8) | (byte3 &lt;&lt; 0));&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2433759459906314202?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2433759459906314202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2433759459906314202' title='30 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2433759459906314202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2433759459906314202'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/what-little-endian-and-big-endian-how.html' title='What Little-Endian and Big-Endian? How can I determine whether a machine&apos;s byte order is big-endian or little endian? How can we convert from one to a'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>30</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2829574474948477226</id><published>2007-07-08T10:34:00.000-07:00</published><updated>2007-07-08T10:36:10.956-07:00</updated><title type='text'>Solve the Rat In A Maze problem using backtracking.</title><content type='html'>This is one of the classical problems of computer science. There is a rat trapped in a maze. There are multiple paths in the maze from the starting point to the ending point. There is some cheese at the exit. The rat starts from the entrance of the maze and wants to get to the cheese. &lt;br /&gt;&lt;br /&gt;This problem can be attacked as follows. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Have a m*m matrix which represents the maze.&lt;br /&gt;&lt;br /&gt;For the sake of simplifying the implementation, have a boundary around your matrix and fill it up with all ones. This is so that you know when the rat is trying to go out of the boundary of the maze. In the real world, the rat would know not to go out of the maze, but hey! So, initially the matrix (I mean, the maze) would be something like (the ones represent the "exra" boundary we have added). The ones inside specify the obstacles.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;111111111111111111111&lt;br /&gt;100000000000000000001&lt;br /&gt;100000010000000000001&lt;br /&gt;100000010000000000001&lt;br /&gt;100000000100001000001&lt;br /&gt;100001000010000000001&lt;br /&gt;100000000100000000001&lt;br /&gt;100000000000000000001&lt;br /&gt;111111111111111111111&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The rat can move in four directions at any point in time (well, right, left, up, down). Please note that the rat can't move diagonally. Imagine a real maze and not a matrix. In matrix language&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Moving right means adding {0,1} to the current coordinates.&lt;br /&gt;&lt;br /&gt;Moving left means adding {0,-1} to the current coordinates.&lt;br /&gt;&lt;br /&gt;Moving up means adding {-1,0} to the current coordinates.&lt;br /&gt;&lt;br /&gt;Moving right means adding {1,0} to the current coordinates.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The rat can start off at the first row and the first column as the entrance point.&lt;br /&gt;&lt;br /&gt;From there, it tries to move to a cell which is currently free. A cell is free if it has a zero in it.&lt;br /&gt;&lt;br /&gt;It tries all the 4 options one-by-one, till it finds an empty cell. If it finds one, it moves to that cell and marks it with a 1 (saying it has visited it once). Then it continues to move ahead from that cell to other cells.&lt;br /&gt;&lt;br /&gt;If at a particular cell, it runs out of all the 4 options (that is it cant move either right, left, up or down), then it needs to backtrack. It backtracks till a point where it can move ahead and be closer to the exit.&lt;br /&gt;&lt;br /&gt;If it reaches the exit point, it gets the cheese, ofcourse.&lt;br /&gt;&lt;br /&gt;The complexity is O(m*m).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some pseudocode to chew upon&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;findpath()&lt;br /&gt;{&lt;br /&gt;  Position offset[4];&lt;br /&gt;  Offset[0].row=0; offset[0].col=1;//right;&lt;br /&gt;  Offset[1].row=1; offset[1].col=0;//down;&lt;br /&gt;  Offset[2].row=0; offset[2].col=-1;//left;&lt;br /&gt;  Offset[3].row=-1; offset[3].col=0;//up;&lt;br /&gt;&lt;br /&gt;  // Initialize wall of obstacles around the maze&lt;br /&gt;  for(int i=0; i &lt; m+1;i++)&lt;br /&gt;    maze[0][i] = maze[m+1][i]=1; maze[i][0] = maze[i][m+1]=1;&lt;br /&gt;&lt;br /&gt;  Position here;&lt;br /&gt;  Here.row=1;&lt;br /&gt;  Here.col=1;&lt;br /&gt;&lt;br /&gt;  maze[1][1]=1;&lt;br /&gt;  int option = 0;&lt;br /&gt;  int lastoption = 3;&lt;br /&gt;&lt;br /&gt;  while(here.row!=m || here.col!=m)&lt;br /&gt;  {&lt;br /&gt;     //Find a neighbor to move&lt;br /&gt;     int r,c;&lt;br /&gt;&lt;br /&gt;       while (option &lt;= LastOption)&lt;br /&gt;       {&lt;br /&gt;          r=here.row + offset[position].row;&lt;br /&gt;          c=here.col + offset[option].col;&lt;br /&gt;          if(maze[r][c]==0)break;&lt;br /&gt;          option++;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       //Was a neighbor found?&lt;br /&gt;       if(option &lt;= LastOption)&lt;br /&gt;       {&lt;br /&gt;         path-&gt;Add(here);&lt;br /&gt;         here.row=r;here.col=c;&lt;br /&gt;         maze[r][c]=1; &lt;br /&gt;         option=0;&lt;br /&gt;       }&lt;br /&gt;       else&lt;br /&gt;       {&lt;br /&gt;          if(path-&gt;Empty())return(False);&lt;br /&gt;          Position  next;&lt;br /&gt;          Path-&gt;Delete(next);&lt;br /&gt;          If(new.row==here.row)&lt;br /&gt;             Option=2+next.col - here.col;&lt;br /&gt;          Else { option = 3 + next.row - here.col;}&lt;br /&gt;             Here=next; &lt;br /&gt;       }&lt;br /&gt;       return(TRUE);&lt;br /&gt;  }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2829574474948477226?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2829574474948477226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2829574474948477226' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2829574474948477226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2829574474948477226'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/solve-rat-in-maze-problem-using.html' title='Solve the Rat In A Maze problem using backtracking.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-128937791092115837</id><published>2007-07-08T10:30:00.000-07:00</published><updated>2007-07-08T10:33:25.172-07:00</updated><title type='text'>How to generate fibonacci numbers? How to find out if a given number is a fibonacci number or not? Write C programs to do both.</title><content type='html'>Lets first refresh ourselves with the Fibonacci sequence&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1, 1, 2, 3, 5, 8, 13, 21, 34, 55, .....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Fibonacci numbers obey the following rule&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;F(n) = F(n-1) + F(n-2)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is an iterative way to generate fibonacci numbers and also return the nth number.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int fib(int n)&lt;br /&gt;{&lt;br /&gt;   int f[n+1];&lt;br /&gt;   f[1] = f[2] = 1;&lt;br /&gt; &lt;br /&gt;   printf("\nf[1] = %d", f[1]);&lt;br /&gt;   printf("\nf[2] = %d", f[2]);&lt;br /&gt;&lt;br /&gt;   for (int i = 3; i &lt;= n; i++)&lt;br /&gt;   {&lt;br /&gt;       f[i] = f[i-1] + f[i-2];&lt;br /&gt;       printf("\nf[%d] = [%d]",i,f[i]);&lt;br /&gt;   }&lt;br /&gt;   return f[n];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is a recursive way to generate fibonacci numbers.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int fib(int n)&lt;br /&gt;{&lt;br /&gt;  if (n &lt;= 2) return 1&lt;br /&gt;  else return fib(n-1) + fib(n-2)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is an iterative way to just compute and return the nth number (without storing the previous numbers).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int fib(int n)&lt;br /&gt;{&lt;br /&gt;  int a = 1, b = 1;&lt;br /&gt;  for (int i = 3; i &lt;= n; i++) &lt;br /&gt;  {&lt;br /&gt;     int c = a + b;&lt;br /&gt;     a = b;&lt;br /&gt;     b = c;&lt;br /&gt;  }           &lt;br /&gt;  return a;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are a few slick ways to generate fibonacci numbers, a few of them are listed below&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;If you know some basic math, its easy to see that&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         n&lt;br /&gt; [ 1 1 ]      =   [ F(n+1) F(n)   ]&lt;br /&gt; [ 1 0 ]          [ F(n)   F(n-1) ]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;or &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(f(n) f(n+1)) [ 0 1 ] = (f(n+1) f(n+2))&lt;br /&gt;              [ 1 1 ]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;                   n&lt;br /&gt;(f(0) f(1)) [ 0 1 ]  = (f(n) f(n+1))&lt;br /&gt;            [ 1 1 ]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The n-th power of the 2 by 2 matrix can be computed efficiently in O(log n) time. This implies an O(log n) algorithm for computing the n-th Fibonacci number.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the pseudocode for this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int Matrix[2][2] = {{1,0}{0,1}}&lt;br /&gt;&lt;br /&gt;int fib(int n)&lt;br /&gt;{&lt;br /&gt;    matrixpower(n-1);&lt;br /&gt;    return Matrix[0][0];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void matrixpower(int n)&lt;br /&gt;{&lt;br /&gt;   if (n &gt; 1) &lt;br /&gt;   {&lt;br /&gt;      matrixpower(n/2);&lt;br /&gt;      Matrix = Matrix * Matrix;&lt;br /&gt;   }&lt;br /&gt;   if (n is odd)&lt;br /&gt;   {&lt;br /&gt;      Matrix = Matrix * {{1,1}{1,0}}&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is a program in C which calculates fib(n)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt; &lt;br /&gt;  &lt;br /&gt;int M[2][2]={{1,0},{0,1}}; &lt;br /&gt;int A[2][2]={{1,1},{1,0}}; &lt;br /&gt;int C[2][2]={{0,0},{0,0}};  // Temporary matrix used for multiplication.&lt;br /&gt;  &lt;br /&gt;void matMul(int n); &lt;br /&gt;void mulM(int m); &lt;br /&gt;  &lt;br /&gt;int main() &lt;br /&gt;{ &lt;br /&gt;   int n; &lt;br /&gt;   n=6; &lt;br /&gt;      &lt;br /&gt;   matMul(n-1); &lt;br /&gt;&lt;br /&gt;   // The nth fibonacci will be stored in M[0][0]&lt;br /&gt;   printf("\n%dth Fibonaci number : [%d]\n\n", n, M[0][0]); &lt;br /&gt;   return(0); &lt;br /&gt;&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Recursive function with divide and conquer strategy&lt;br /&gt;&lt;br /&gt;void matMul(int n)&lt;br /&gt;{ &lt;br /&gt;   if(n &gt; 1) &lt;br /&gt;   { &lt;br /&gt;     matMul(n/2); &lt;br /&gt;     mulM(0);     // M * M&lt;br /&gt;   } &lt;br /&gt;   if(n%2!=0) &lt;br /&gt;   { &lt;br /&gt;    mulM(1);     //  M * {{1,1}{1,0}}&lt;br /&gt;   } &lt;br /&gt;} &lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;// Function which does some basic matrix multiplication.&lt;br /&gt;&lt;br /&gt;void mulM(int m) &lt;br /&gt;{ &lt;br /&gt;   int i,j,k; &lt;br /&gt;  &lt;br /&gt;   if(m==0) &lt;br /&gt;   { &lt;br /&gt;     // C = M * M&lt;br /&gt;&lt;br /&gt;     for(i=0;i &lt; 2;i++) &lt;br /&gt;      for(j=0;j &lt; 2;j++) &lt;br /&gt;      { &lt;br /&gt;        C[i][j]=0; &lt;br /&gt;        for(k=0;k &lt; 2;k++) &lt;br /&gt;           C[i][j]+=M[i][k]*M[k][j]; &lt;br /&gt;      } &lt;br /&gt;   } &lt;br /&gt;   else &lt;br /&gt;   { &lt;br /&gt;     // C = M *  {{1,1}{1,0}}&lt;br /&gt;&lt;br /&gt;     for(i=0;i &lt; 2;i++) &lt;br /&gt;      for(j=0;j &lt; 2;j++) &lt;br /&gt;      { &lt;br /&gt;        C[i][j]=0; &lt;br /&gt;        for(k=0;k &lt; 2;k++) &lt;br /&gt;         C[i][j]+=A[i][k]*M[k][j]; &lt;br /&gt;      } &lt;br /&gt;   } &lt;br /&gt;&lt;br /&gt;   // Copy back the temporary matrix in the original matrix M&lt;br /&gt;&lt;br /&gt;   for(i=0;i &lt; 2;i++) &lt;br /&gt;     for(j=0;j &lt; 2;j++) &lt;br /&gt;     { &lt;br /&gt;       M[i][j]=C[i][j]; &lt;br /&gt;     } &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;f(n) = (1/sqrt(5)) * (((1+sqrt(5))/2) ^ n - ((1-sqrt(5))/2) ^ n)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So now, how does one find out if a number is a fibonacci or not?.&lt;br /&gt;&lt;br /&gt;The cumbersome way is to generate fibonacci numbers till this number and see if this number is one of them. But there is another slick way to check if a number is a fibonacci number or not. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;N is a Fibonacci number if and only if (5*N*N + 4) or (5*N*N - 4) is a perfect square!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Dont believe me?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3 is a Fibonacci number since (5*3*3 + 4) is 49 which is 7*7&lt;br /&gt;5 is a Fibonacci number since (5*5*5 - 4) is 121 which is 11*11&lt;br /&gt;4 is not a Fibonacci number since neither (5*4*4 + 4) = 84 nor (5*4*4 - 4) = 76 are perfect squares.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To check if a number is a perfect square or not, one can take the square root, round it to the nearest integer and then square the result. If this is the same as the original whole number then the original was a perfect square.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-128937791092115837?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/128937791092115837/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=128937791092115837' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/128937791092115837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/128937791092115837'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-to-generate-fibonacci-numbers-how.html' title='How to generate fibonacci numbers? How to find out if a given number is a fibonacci number or not? Write C programs to do both.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3527909945937614123</id><published>2007-07-08T10:26:00.000-07:00</published><updated>2007-07-08T10:29:12.074-07:00</updated><title type='text'>How do you calculate the maximum subarray of a list of numbers?</title><content type='html'>This is a very popular question&lt;br /&gt;&lt;br /&gt;You are given a large array X of integers (both positive and negative) and you need to find the maximum sum found in any contiguous subarray of X.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example X = [11, -12, 15, -3, 8, -9, 1, 8, 10, -2]&lt;br /&gt;Answer is 30.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are various methods to solve this problem, some are listed below&lt;br /&gt;&lt;br /&gt;Brute force&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;maxSum = 0&lt;br /&gt;for L = 1 to N&lt;br /&gt;{&lt;br /&gt;  for R = L to N&lt;br /&gt;  { &lt;br /&gt;     sum = 0&lt;br /&gt;     for i = L to R&lt;br /&gt;     {&lt;br /&gt;        sum = sum + X[i]&lt;br /&gt;     }&lt;br /&gt;     maxSum = max(maxSum, sum)&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;O(N^3)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Quadratic&lt;br /&gt;&lt;br /&gt;Note that sum of [L..R] can be calculated from sum of [L..R-1] very easily.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;maxSum = 0&lt;br /&gt;for L = 1 to N&lt;br /&gt;{&lt;br /&gt;  sum = 0&lt;br /&gt;  for R = L to N&lt;br /&gt;  {&lt;br /&gt;    sum = sum + X[R]&lt;br /&gt;    maxSum = max(maxSum, sum) &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Using divide-and-conquer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;O(N log(N))&lt;br /&gt;&lt;br /&gt;maxSum(L, R)&lt;br /&gt;{&lt;br /&gt;  if L &gt; R then&lt;br /&gt;    return 0&lt;br /&gt; &lt;br /&gt;  if L = R then&lt;br /&gt;    return max(0, X[L])&lt;br /&gt;&lt;br /&gt;  M = (L + R)/2&lt;br /&gt;&lt;br /&gt;  sum = 0; maxToLeft = 0&lt;br /&gt;  for i = M downto L do&lt;br /&gt;  {&lt;br /&gt;     sum = sum + X[i]&lt;br /&gt;     maxToLeft = max(maxToLeft, sum)&lt;br /&gt;  } &lt;br /&gt; &lt;br /&gt;  sum = 0; maxToRight = 0&lt;br /&gt;  for i = M to R do&lt;br /&gt;  {&lt;br /&gt;     sum = sum + X[i]&lt;br /&gt;     maxToRight = max(maxToRight, sum)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  maxCrossing = maxLeft + maxRight&lt;br /&gt;  maxInA = maxSum(L,M)&lt;br /&gt;  maxInB = maxSum(M+1,R)&lt;br /&gt;  return max(maxCrossing, maxInA, maxInB)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is working C code for all the above cases&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#define N 10&lt;br /&gt;int maxSubSum(int left, int right);&lt;br /&gt;int list[N] = {11, -12, 15, -3, 8, -9, 1, 8, 10, -2};&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  int i,j,k;&lt;br /&gt;  int maxSum, sum;&lt;br /&gt;&lt;br /&gt;  /*---------------------------------------&lt;br /&gt;  * CUBIC - O(n*n*n)&lt;br /&gt;  *---------------------------------------*/&lt;br /&gt;&lt;br /&gt;  maxSum = 0;&lt;br /&gt;  for(i=0; i &lt; N; i++)&lt;br /&gt;  {&lt;br /&gt;    for(j=i; j &lt; N; j++)&lt;br /&gt;    {&lt;br /&gt;      sum = 0;&lt;br /&gt;      for(k=i ; k &lt; j; k++)&lt;br /&gt;      {&lt;br /&gt;        sum = sum + list[k];&lt;br /&gt;      }&lt;br /&gt;      maxSum = (maxSum &gt; sum)?maxSum:sum;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\nmaxSum = [%d]\n", maxSum);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   /*-------------------------------------&lt;br /&gt;    * Quadratic - O(n*n)&lt;br /&gt;    * ------------------------------------ */&lt;br /&gt;&lt;br /&gt;   maxSum = 0;&lt;br /&gt;   for(i=0; i &lt; N; i++)&lt;br /&gt;   {&lt;br /&gt;      sum=0;&lt;br /&gt;      for(j=i; j &lt; N ;j++)&lt;br /&gt;      {&lt;br /&gt;         sum = sum + list[j];&lt;br /&gt;         maxSum = (maxSum &gt; sum)?maxSum:sum;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\nmaxSum = [%d]\n", maxSum);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   /*----------------------------------------&lt;br /&gt;    * Divide and Conquer - O(nlog(n))&lt;br /&gt;    * -------------------------------------- */&lt;br /&gt;&lt;br /&gt;   printf("\nmaxSum : [%d]\n", maxSubSum(0,9));&lt;br /&gt;&lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int maxSubSum(int left, int right)&lt;br /&gt;{&lt;br /&gt;  int mid, sum, maxToLeft, maxToRight, maxCrossing, maxInA, maxInB;&lt;br /&gt;  int i;&lt;br /&gt;&lt;br /&gt;  if(left &gt; right){return 0;}&lt;br /&gt;  if(left==right){return((0 &gt; list[left])?0:list[left]);}&lt;br /&gt;  mid = (left + right)/2;&lt;br /&gt;&lt;br /&gt;  sum=0;&lt;br /&gt;  maxToLeft=0;&lt;br /&gt;  for(i=mid; i &gt;= left; i--)&lt;br /&gt;  {&lt;br /&gt;     sum = sum + list[i];&lt;br /&gt;     maxToLeft = (maxToLeft &gt; sum)?maxToLeft:sum;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  sum=0;&lt;br /&gt;  maxToRight=0;&lt;br /&gt;  for(i=mid+1; i &lt;= right; i++)&lt;br /&gt;  {&lt;br /&gt;      sum = sum + list[i];&lt;br /&gt;      maxToRight = (maxToRight &gt; sum)?maxToRight:sum;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  maxCrossing = maxToLeft + maxToRight;&lt;br /&gt;  maxInA = maxSubSum(left,mid);&lt;br /&gt;  maxInB = maxSubSum(mid+1,right);&lt;br /&gt;  return(((maxCrossing &gt; maxInA)?maxCrossing:maxInA) &gt; maxInB?((maxCrossing&gt;maxInA)?maxCrossing:maxInA):maxInB);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that, if the array has all negative numbers, then this code will return 0. This is wrong because it should return the maximum sum, which is the least negative integer in the array. This happens because we are setting maxSum to 0 initially. A small change in this code can be used to handle such cases.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3527909945937614123?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3527909945937614123/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3527909945937614123' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3527909945937614123'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3527909945937614123'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/how-do-you-calculate-maximum-subarray.html' title='How do you calculate the maximum subarray of a list of numbers?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-9199493900443624521</id><published>2007-07-08T03:53:00.000-07:00</published><updated>2007-07-08T03:54:33.292-07:00</updated><title type='text'>Write a C program which does wildcard pattern matching algorithm.</title><content type='html'>Here is an example C program...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#define TRUE 1&lt;br /&gt;#define FALSE 0&lt;br /&gt;&lt;br /&gt;int wildcard(char *string, char *pattern);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  char *string = "hereheroherr";&lt;br /&gt;  char *pattern = "*hero*";&lt;br /&gt;&lt;br /&gt;  if(wildcard(string, pattern)==TRUE)&lt;br /&gt;  {&lt;br /&gt;    printf("\nMatch Found!\n");&lt;br /&gt;  }&lt;br /&gt;  else &lt;br /&gt;  {&lt;br /&gt;    printf("\nMatch not found!\n");&lt;br /&gt;  }&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int wildcard(char *string, char *pattern)&lt;br /&gt;{&lt;br /&gt;  while(*string) &lt;br /&gt;  {&lt;br /&gt;     switch(*pattern)&lt;br /&gt;     {&lt;br /&gt;        case '*': do {++pattern;}while(*pattern == '*');&lt;br /&gt;                  if(!*pattern) return(TRUE);&lt;br /&gt;                  while(*string){if(wildcard(pattern,string++)==TRUE)return(TRUE);}&lt;br /&gt;                  return(FALSE);&lt;br /&gt;        default : if(*string!=*pattern)return(FALSE); break;&lt;br /&gt;     }&lt;br /&gt;     ++pattern;&lt;br /&gt;     ++string;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   while (*pattern == '*') ++pattern;&lt;br /&gt;   return !*pattern;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-9199493900443624521?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/9199493900443624521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=9199493900443624521' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/9199493900443624521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/9199493900443624521'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-which-does-wildcard.html' title='Write a C program which does wildcard pattern matching algorithm.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1393770220255522834</id><published>2007-07-08T03:48:00.000-07:00</published><updated>2007-07-08T03:49:34.207-07:00</updated><title type='text'>Write a C program to calculate pow(x,n)?</title><content type='html'>There are again different methods to do this in C&lt;br /&gt;&lt;br /&gt;Brute force C program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int pow(int x, int y)&lt;br /&gt;{&lt;br /&gt;  if(y == 1) return x ;&lt;br /&gt;  return x * pow(x, y-1) ;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Divide and Conquer C program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;int main(int argc, char*argv[])&lt;br /&gt;{&lt;br /&gt;  printf("\n[%d]\n",pow(5,4));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int pow(int x, int n)&lt;br /&gt;{&lt;br /&gt;  if(n==0)return(1);&lt;br /&gt;  else if(n%2==0)&lt;br /&gt;  {&lt;br /&gt;    return(pow(x,n/2)*pow(x,(n/2)));&lt;br /&gt;  }&lt;br /&gt;  else &lt;br /&gt;  {&lt;br /&gt;    return(x*pow(x,n/2)*pow(x,(n/2)));&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Also, the code above can be optimized still by calculating pow(z, (n/2)) only one time (instead of twice) and using its value in the two return() expressions above.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1393770220255522834?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1393770220255522834/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1393770220255522834' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1393770220255522834'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1393770220255522834'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-calculate-powxn.html' title='Write a C program to calculate pow(x,n)?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1368818009180132333</id><published>2007-07-08T03:44:00.000-07:00</published><updated>2007-07-08T03:46:11.529-07:00</updated><title type='text'>Write a C program generate permutations.</title><content type='html'>Iterative C program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#define SIZE 3&lt;br /&gt;int main(char *argv[],int argc)&lt;br /&gt;{&lt;br /&gt;  char list[3]={'a','b','c'};&lt;br /&gt;  int i,j,k;&lt;br /&gt;&lt;br /&gt;  for(i=0;i &lt; SIZE;i++)&lt;br /&gt;    for(j=0;j &lt; SIZE;j++)&lt;br /&gt;      for(k=0;k &lt; SIZE;k++)&lt;br /&gt;        if(i!=j &amp;&amp; j!=k &amp;&amp; i!=k)&lt;br /&gt;          printf("%c%c%c\n",list[i],list[j],list[k]);&lt;br /&gt;&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Recursive C program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt; stdio.h &gt;&lt;br /&gt;#define N  5&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main(char *argv[],int argc)&lt;br /&gt;{&lt;br /&gt;  char list[5]={'a','b','c','d','e'};&lt;br /&gt;  permute(list,0,N);&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void permute(char list[],int k, int m)&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;  char temp;&lt;br /&gt;&lt;br /&gt;  if(k==m)&lt;br /&gt;  {&lt;br /&gt;    /* PRINT A FROM k to m! */&lt;br /&gt;    for(i=0;i &lt; N;i++){printf("%c",list[i]);}&lt;br /&gt;    printf("\n");&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;     for(i=k;i &lt; m;i++)&lt;br /&gt;     {&lt;br /&gt;        /* swap(a[i],a[m-1]); */ &lt;br /&gt;        temp=list[i];&lt;br /&gt;        list[i]=list[m-1];&lt;br /&gt;        list[m-1]=temp;&lt;br /&gt;&lt;br /&gt;        permute(list,k,m-1);&lt;br /&gt;&lt;br /&gt;        /* swap(a[m-1],a[i]); */ &lt;br /&gt;&lt;br /&gt;        temp=list[m-1];&lt;br /&gt;        list[m-1]=list[i];&lt;br /&gt;        list[i]=temp;&lt;br /&gt;       }&lt;br /&gt;  }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1368818009180132333?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1368818009180132333/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1368818009180132333' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1368818009180132333'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1368818009180132333'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-generate-permutations.html' title='Write a C program generate permutations.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5733127707938129427</id><published>2007-07-08T03:12:00.000-07:00</published><updated>2007-07-08T03:26:30.455-07:00</updated><title type='text'>Write a C program to reverse the words in a sentence in place.</title><content type='html'>That is, given a sentence like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I am a good boy&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The in place reverse would be&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;boy good a am I&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;First reverse the whole string and then individually reverse the words&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I am a good boy&lt;br /&gt;&lt;-------------&gt;&lt;br /&gt;&lt;br /&gt;yob doog  a   ma   I&lt;br /&gt;&lt;-&gt; &lt;--&gt; &lt;-&gt;  &lt;-&gt; &lt;-&gt;&lt;br /&gt;&lt;br /&gt;boy good a am I&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some C code to do the same ....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;  Algorithm..&lt;br /&gt;  &lt;br /&gt;  1. Reverse whole sentence first.&lt;br /&gt;  2. Reverse each word individually.&lt;br /&gt;&lt;br /&gt;  All the reversing happens in-place.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;void rev(char *l, char *r);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt;   char buf[] = "the world will go on forever";&lt;br /&gt;   char *end, *x, *y;&lt;br /&gt;      &lt;br /&gt;   // Reverse the whole sentence first..&lt;br /&gt;   for(end=buf; *end; end++);&lt;br /&gt;   rev(buf,end-1); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   // Now swap each word within sentence...&lt;br /&gt;   x = buf-1; &lt;br /&gt;   y = buf; &lt;br /&gt;&lt;br /&gt;   while(x++ &lt; end)&lt;br /&gt;   {&lt;br /&gt;      if(*x == '\0' || *x == ' ')&lt;br /&gt;      {&lt;br /&gt;        rev(y,x-1);&lt;br /&gt;        y = x+1;&lt;br /&gt;      }&lt;br /&gt;   }   &lt;br /&gt;   &lt;br /&gt;   // Now print the final string....&lt;br /&gt;   printf("%s\n",buf);&lt;br /&gt;   &lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to reverse a string in place...&lt;br /&gt;void rev(char *l,char *r) &lt;br /&gt;{&lt;br /&gt;   char t;&lt;br /&gt;   while(l &lt; r) &lt;br /&gt;   { &lt;br /&gt;      t    = *l; &lt;br /&gt;      *l++ = *r; &lt;br /&gt;      *r-- = t; &lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;Another way to do it is, allocate as much memory as the input for the final output. Start from the right of the string and copy the words one by one to the output. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Input  : I am a good boy&lt;br /&gt;                    &lt; --&lt;br /&gt;               &lt; -------&lt;br /&gt;             &lt; ---------&lt;br /&gt;          &lt; ------------&lt;br /&gt;        &lt; --------------&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;Output : boy&lt;br /&gt;       : boy good&lt;br /&gt;       : boy good a &lt;br /&gt;       : boy good a am &lt;br /&gt;       : boy good a am I &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The only problem to this solution is the extra space required for the output and one has to write this code really well as we are traversing the string in the reverse direction and there is no null at the start of the string to know we have reached the start of the string!. One can use the strtok() function to breakup the string into multiple words and rearrange them in the reverse order later.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;Create a linked list like&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;| I | -&gt; | &lt; spaces &gt; | -&gt; | am | -&gt; | &lt; spaces &gt; | -&gt; | a | -&gt; | &lt; spaces &gt; | -&gt; | good | -&gt; | &lt; spaces &gt; | -&gt; | boy | -&gt; | NULL | &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now its a simple question of reversing the linked list!. There are plenty of algorithms to reverse a linked list easily. This also keeps track of the number of spaces between the words. Note that the linked list algorithm, though inefficient, handles multiple spaces between the words really well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5733127707938129427?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5733127707938129427/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5733127707938129427' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5733127707938129427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5733127707938129427'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-reverse-words-in.html' title='Write a C program to reverse the words in a sentence in place.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-74685744934653758</id><published>2007-07-07T15:34:00.000-07:00</published><updated>2007-07-07T15:35:53.727-07:00</updated><title type='text'>Write a C program to reverse a string.</title><content type='html'>There are a number of ways one can reverse strings. Here are a few of them. These should be enough to impress the interviewer! The methods span from recursive to non-recursive (iterative).&lt;br /&gt;&lt;br /&gt;Also note that there is a similar question about reversing the words in a sentence, but still keeping the words in place. That is&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I am a good boy&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;would become&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;boy good a am I&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is dealt with in another question. Here I only concentrate on reversing strings. That is&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I am a good boy&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;would become&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;yob doog a ma I&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here are some sample C programs to do the same&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1 (Recursive)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;static char str[]="STRING TO REVERSE";&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv)&lt;br /&gt;{&lt;br /&gt;   printf("\nOriginal string : [%s]", str);&lt;br /&gt;&lt;br /&gt;   // Call the recursion function&lt;br /&gt;   reverse(0);&lt;br /&gt;&lt;br /&gt;   printf("\nReversed string : [%s]", str);&lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int reverse(int pos)&lt;br /&gt;{&lt;br /&gt;   // Here I am calculating strlen(str) everytime. &lt;br /&gt;   // This can be avoided by doing this computation&lt;br /&gt;   // earlier and storing it somewhere for later use.&lt;br /&gt;&lt;br /&gt;   if(pos&lt;(strlen(str)/2))&lt;br /&gt;   {&lt;br /&gt;      char ch;&lt;br /&gt;&lt;br /&gt;      // Swap str[pos] and str[strlen(str)-pos-1]&lt;br /&gt;      ch = str[pos];&lt;br /&gt;      str[pos]=str[strlen(str)-pos-1];&lt;br /&gt;      str[strlen(str)-pos-1]=ch;&lt;br /&gt;       &lt;br /&gt;      // Now recurse!&lt;br /&gt;      reverse(pos+1);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt; &lt;br /&gt;#include &lt;malloc.h&gt; &lt;br /&gt;#include &lt;string.h&gt; &lt;br /&gt;&lt;br /&gt;void ReverseStr ( char *buff, int start, int end ) &lt;br /&gt;{ &lt;br /&gt;   char tmp ; &lt;br /&gt;&lt;br /&gt;   if ( start &gt;= end ) &lt;br /&gt;   { &lt;br /&gt;      printf ( "\n%s\n", buff ); &lt;br /&gt;      return; &lt;br /&gt;   } &lt;br /&gt;&lt;br /&gt;   tmp = *(buff + start); &lt;br /&gt;   *(buff + start) = *(buff + end); &lt;br /&gt;   *(buff + end) = tmp ; &lt;br /&gt;&lt;br /&gt;   ReverseStr (buff, ++start, --end ); &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main() &lt;br /&gt;{ &lt;br /&gt;   char buffer[]="This is Test"; &lt;br /&gt;   ReverseStr(buffer,0,strlen(buffer)-1); &lt;br /&gt;   return 0; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static String reverse(String s) &lt;br /&gt;{    &lt;br /&gt;    int N = s.length();    &lt;br /&gt;    if (N &lt;= 1) return s;    &lt;br /&gt;    String left = s.substring(0, N/2);    &lt;br /&gt;    String right = s.substring(N/2, N);    &lt;br /&gt;    return reverse(right) + reverse(left);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method4&lt;br /&gt;&lt;br /&gt;for(int i = 0, j = reversed.Length - 1; i &lt; j; i++, j--) &lt;br /&gt;{ &lt;br /&gt;    char temp = reversed[i]; &lt;br /&gt;    reversed[i] = reversed[j]; &lt;br /&gt;    reversed[j] = temp; &lt;br /&gt;} &lt;br /&gt;return new String(reversed); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method5&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static String reverse(String s) &lt;br /&gt;{   &lt;br /&gt;   int N = s.length();   &lt;br /&gt;   String reverse = "";   &lt;br /&gt;   for (int i = 0; i &lt; N; i++)      &lt;br /&gt;        reverse = s.charAt(i) + reverse;   &lt;br /&gt;   return reverse;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method6&lt;br /&gt;&lt;br /&gt;public static String reverse(String s) &lt;br /&gt;{    &lt;br /&gt;   int N = s.length();    &lt;br /&gt;   char[] a = new char[N];    &lt;br /&gt;   for (int i = 0; i &lt; N; i++)       &lt;br /&gt;       a[i] = s.charAt(N-i-1);    &lt;br /&gt;   String reverse = new String(a);    &lt;br /&gt;   return reverse;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Isn't that enough?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-74685744934653758?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/74685744934653758/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=74685744934653758' title='29 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/74685744934653758'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/74685744934653758'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-reverse-string.html' title='Write a C program to reverse a string.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>29</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3467876670279350877</id><published>2007-07-07T15:26:00.000-07:00</published><updated>2007-07-07T15:33:32.834-07:00</updated><title type='text'>Write a C program to print a square matrix helically.</title><content type='html'>Here is a C program to print a matrix helically. Printing a matrix helically means printing it in this spiral fashion.&lt;br /&gt;&lt;br /&gt;This is a simple program to print a matrix helically.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* HELICAL MATRIX */&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;int arr[][4] = { {1,2,3,4},&lt;br /&gt;{5,6,7,8},&lt;br /&gt;{9,10,11,12},&lt;br /&gt;{13, 14, 15, 16}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int i, j, k,middle,size;&lt;br /&gt;printf("\n\n");&lt;br /&gt;size = 4;&lt;br /&gt;&lt;br /&gt;for(i=size-1, j=0; i &gt; 0; i--, j++)&lt;br /&gt;{&lt;br /&gt;for(k=j; k &lt; i; k++) printf("%d ", arr[j][k]);&lt;br /&gt;for(k=j; k &lt; i; k++) printf("%d ", arr[k][i]);             &lt;br /&gt;for(k=i; k &gt; j; k--) printf("%d ", arr[i][k]);             &lt;br /&gt;for(k=i; k &gt; j; k--) printf("%d ", arr[k][j]);&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt; middle = (size-1)/2;      &lt;br /&gt;if (size % 2 == 1) printf("%d", arr[middle][middle]);      &lt;br /&gt;printf("\n\n");      &lt;br /&gt;return 1;&lt;br /&gt;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3467876670279350877?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3467876670279350877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3467876670279350877' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3467876670279350877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3467876670279350877'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-print-square-matrix.html' title='Write a C program to print a square matrix helically.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1130790584298219941</id><published>2007-07-07T15:24:00.000-07:00</published><updated>2007-07-07T15:25:07.486-07:00</updated><title type='text'>What is the 8 queens problem? Write a C program to solve it.</title><content type='html'>The 8 queens problem is a classic problem using the chess board. This problem is to place 8 queens on the chess board so that they do not attack each other horizontally, vertically or diagonally. It turns out that there are 12 essentially distinct solutions to this problem. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Suppose we have an array t[8] which keeps track of which column is occupied in which row of the chess board. That is, if t[0]==5, then it means that the queen has been placed in the fifth column of the first row. We need to couple the backtracking algorithm with a procedure that checks whether the tuple is completable or not, i.e. to check that the next placed queen 'i' is not menaced by any of the already placed 'j' (j &lt; i):&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two queens are in the same column         if t[i]=t[j]&lt;br /&gt;Two queens are in the same major diagonal if (t[i]-t[j])=(i-j)&lt;br /&gt;two queens are in the same minor diagonal if (t[j]-t[i])=(i-j)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some working C code to solve this problem using backtracking&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;static int t[10]={-1};&lt;br /&gt;void queens(int i);&lt;br /&gt;int empty(int i);&lt;br /&gt;&lt;br /&gt;void print_solution();&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  queens(1);&lt;br /&gt;  print_solution();&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void queens(int i)&lt;br /&gt;{&lt;br /&gt;  for(t[i]=1;t[i]&lt;=8;t[i]++)&lt;br /&gt;  {&lt;br /&gt;    if(empty(i))&lt;br /&gt;    {&lt;br /&gt;       if(i==8)&lt;br /&gt;       {&lt;br /&gt;          print_solution();&lt;br /&gt;          /* If this exit is commented, it will show ALL possible combinations */&lt;br /&gt;          exit(0);&lt;br /&gt;       }&lt;br /&gt;       else&lt;br /&gt;       {&lt;br /&gt;          // Recurse!&lt;br /&gt;          queens(i+1);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;    }// if&lt;br /&gt;&lt;br /&gt;  }// for&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int empty(int i)&lt;br /&gt;{&lt;br /&gt;  int j;&lt;br /&gt;  j=1;&lt;br /&gt;&lt;br /&gt;  while(t[i]!=t[j] &amp;&amp; abs(t[i]-t[j])!=(i-j) &amp;&amp;j&lt;8)j++;&lt;br /&gt;&lt;br /&gt;  return((i==j)?1:0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void print_solution()&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;  for(i=1;i&lt;=8;i++)printf("\nt[%d] = [%d]",i,t[i]);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is one of the possible solutions&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;t[1] = [1] // This means the first square of the first row.&lt;br /&gt;t[2] = [5] // This means the fifth square of the second row.&lt;br /&gt;t[3] = [8] ..&lt;br /&gt;t[4] = [6] ..&lt;br /&gt;t[5] = [3] ..&lt;br /&gt;t[6] = [7] ..&lt;br /&gt;t[7] = [2] ..&lt;br /&gt;t[8] = [4] // This means the fourth square of the last row.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1130790584298219941?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1130790584298219941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1130790584298219941' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1130790584298219941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1130790584298219941'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/what-is-8-queens-problem-write-c.html' title='What is the 8 queens problem? Write a C program to solve it.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-165217257049975079</id><published>2007-07-07T15:22:00.000-07:00</published><updated>2007-07-07T15:23:26.325-07:00</updated><title type='text'>Write a C program to swap two variables without using a temporary variable.</title><content type='html'>This questions is asked almost always in every interview. &lt;br /&gt;&lt;br /&gt;The best way to swap two variables is to use a temporary variable. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int a,b,t;&lt;br /&gt;&lt;br /&gt;t = a;&lt;br /&gt;a = b;&lt;br /&gt;b = t;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is no way better than this as you will find out soon. There are a few slick expressions that do swap variables without using temporary storage. But they come with their own set of problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1 (The XOR trick)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a ^= b ^= a ^= b;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Although the code above works fine for most of the cases, it tries to modify variable 'a' two times between sequence points, so the behavior is undefined. What this means is it wont work in all the cases. This will also not work for floating-point values. Also, think of a scenario where you have written your code like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int *a, int *b)&lt;br /&gt;{&lt;br /&gt;  *a ^= *b ^= *a ^= *b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, if suppose, by mistake, your code passes the pointer to the same variable to this function. Guess what happens? Since Xor'ing an element with itself sets the variable to zero, this routine will end up setting the variable to zero (ideally it should have swapped the variable with itself). This scenario is quite possible in sorting algorithms which sometimes try to swap a variable with itself (maybe due to some small, but not so fatal coding error). One solution to this problem is to check if the numbers to be swapped are already equal to each other.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int *a, int *b)&lt;br /&gt;{&lt;br /&gt;  if(*a!=*b)&lt;br /&gt;  {&lt;br /&gt;    *a ^= *b ^= *a ^= *b;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;This method is also quite popular&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; a=a+b;&lt;br /&gt; b=a-b;&lt;br /&gt; a=a-b;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But, note that here also, if a and b are big and their addition is bigger than the size of an int, even this might end up giving you wrong results. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;One can also swap two variables using a macro. However, it would be required to pass the type of the variable to the macro. Also, there is an interesting problem using macros. Suppose you have a swap macro which looks something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#define swap(type,a,b) type temp;temp=a;a=b;b=temp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, think what happens if you pass in something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int,temp,a) //You have a variable called "temp" (which is quite possible).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how it gets replaced by the macro&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int temp;&lt;br /&gt;temp=temp;&lt;br /&gt;temp=b;&lt;br /&gt;b=temp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Which means it sets the value of "b" to both the variables!. It never swapped them! Scary, isn't it?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So the moral of the story is, dont try to be smart when writing code to swap variables. Use a temporary variable. Its not only fool proof, but also easier to understand and maintain.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-165217257049975079?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/165217257049975079/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=165217257049975079' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/165217257049975079'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/165217257049975079'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-swap-two-variables.html' title='Write a C program to swap two variables without using a temporary variable.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2025751141588697695</id><published>2007-07-07T14:37:00.000-07:00</published><updated>2007-07-07T14:38:04.093-07:00</updated><title type='text'>Write your own strcat() function.</title><content type='html'>Here is a C function which implements the strcat() function...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/* Function to concatenate string t to end of s; return s */&lt;br /&gt;char *myStrcat(char *s, const char *t)&lt;br /&gt;{&lt;br /&gt;    char *p = s;&lt;br /&gt;&lt;br /&gt;    if (s == NULL || t == NULL)&lt;br /&gt;        return s;   /* we need not have to do anything */&lt;br /&gt;&lt;br /&gt;    while (*s)&lt;br /&gt;        s++;&lt;br /&gt;&lt;br /&gt;    while (*s++ = *t++)&lt;br /&gt;        ;&lt;br /&gt;&lt;br /&gt;    return p;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2025751141588697695?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2025751141588697695/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2025751141588697695' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2025751141588697695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2025751141588697695'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-your-own-strcat-function.html' title='Write your own strcat() function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7920962156964348514</id><published>2007-07-07T14:35:00.000-07:00</published><updated>2007-07-07T14:36:36.631-07:00</updated><title type='text'>Write a C program to implement the strlen() function.</title><content type='html'>The prototype of the strlen() function is...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;size_t strlen(const char *string);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some C code which implements the strlen() function....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int my_strlen(char *string)&lt;br /&gt;{&lt;br /&gt;  int length;&lt;br /&gt;  for (length = 0; *string != '\0', string++)&lt;br /&gt;  {&lt;br /&gt;    length++;&lt;br /&gt;  }&lt;br /&gt;  return(length);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Also, see another example&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int my_strlen(char *s)&lt;br /&gt;{&lt;br /&gt;  char *p=s;&lt;br /&gt;&lt;br /&gt;  while(*p!='\0')&lt;br /&gt;    p++;&lt;br /&gt;&lt;br /&gt;  return(p-s);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7920962156964348514?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7920962156964348514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7920962156964348514' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7920962156964348514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7920962156964348514'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-implement-strlen.html' title='Write a C program to implement the strlen() function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2411526366936388504</id><published>2007-07-07T14:32:00.000-07:00</published><updated>2007-07-07T14:33:40.257-07:00</updated><title type='text'>Write a C program to implement your own strdup() function.</title><content type='html'>Here is a C program to implement the strdup() function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *mystrdup(char *s)&lt;br /&gt;{&lt;br /&gt;    char *result = (char*)malloc(strlen(s) + 1);&lt;br /&gt;    if (result == (char*)0){return (char*)0;}&lt;br /&gt;    strcpy(result, s);&lt;br /&gt;    return result;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2411526366936388504?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2411526366936388504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2411526366936388504' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2411526366936388504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2411526366936388504'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-implement-your-own.html' title='Write a C program to implement your own strdup() function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-429234155708754155</id><published>2007-07-07T14:29:00.000-07:00</published><updated>2007-07-07T14:31:18.311-07:00</updated><title type='text'>Write C programs to implement the toupper() and the isupper() functions.</title><content type='html'>toUpper()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int toUpper(int ch)&lt;br /&gt;{&lt;br /&gt;    if(ch&gt;='a' &amp;&amp; c&lt;='z')&lt;br /&gt;        return('A' + ch - 'a');&lt;br /&gt;    else&lt;br /&gt;        return(ch);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;isUpper()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int isUpper(int ch)&lt;br /&gt;{&lt;br /&gt;    if(ch&gt;='A' &amp;&amp; ch &lt;='Z')&lt;br /&gt;       return(1); //Yes, its upper!&lt;br /&gt;    else &lt;br /&gt;       return(0); // No, its lower!&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Its important to know that the upper and lower case alphabets have corresponding integer values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A-Z - 65-90&lt;br /&gt;a-z - 97-122&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Another way to do this conversion is to maintain a correspondance between the upper and lower case alphabets. The program below does that. This frees us from the fact that these alphabets have a corresponding integer values. I dont know what one should do for non-english alphabets. Do other languages have upper and lower case letters in the first place :) !&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;#define UPPER   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"&lt;br /&gt;#define LOWER   "abcdefghijklmnopqrstuvwxyz"&lt;br /&gt;&lt;br /&gt;int toUpper(int c) &lt;br /&gt;{&lt;br /&gt;    const char *upper;&lt;br /&gt;    const char *const lower = LOWER;&lt;br /&gt;        &lt;br /&gt;    // Get the position of the lower case alphabet in the LOWER string using the strchr() function ..&lt;br /&gt;    upper = ( ((CHAR_MAX &gt;= c)&amp;&amp;(c &gt; '\0')) ? strchr(lower, c) : NULL);&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;    // Now return the corresponding alphabet at that position in the UPPER string ..&lt;br /&gt;    return((upper != NULL)?UPPER[upper - lower] : c);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that these routines dont have much error handling incorporated in them. Its really easy to add error handling to these routines or just leave it out (as I like it). This site consciously leaves out error handling for most of the programs to prevent unwanted clutter and present the core logic first.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-429234155708754155?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/429234155708754155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=429234155708754155' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/429234155708754155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/429234155708754155'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-programs-to-implement-toupper.html' title='Write C programs to implement the toupper() and the isupper() functions.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-621053829987924840</id><published>2007-07-07T14:27:00.000-07:00</published><updated>2007-07-07T14:28:04.992-07:00</updated><title type='text'>Write your own copy() function.</title><content type='html'>Here is some C code that simulates a file copy action.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;              /* standard I/O routines. */&lt;br /&gt;#define MAX_LINE_LEN 1000 /* maximum line length supported. */&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;    char* file_path_from;&lt;br /&gt;    char* file_path_to;&lt;br /&gt;    FILE* f_from;&lt;br /&gt;    FILE* f_to;&lt;br /&gt;    char buf[MAX_LINE_LEN+1];&lt;br /&gt;&lt;br /&gt;    file_path_from = "&lt;something&gt;";&lt;br /&gt;    file_path_to   = "&lt;something_else&gt;";&lt;br /&gt;&lt;br /&gt;    f_from = fopen(file_path_from, "r");&lt;br /&gt;    if (!f_from) {exit(1);}&lt;br /&gt;&lt;br /&gt;    f_to = fopen(file_path_to, "w+");&lt;br /&gt;    if (!f_to) {exit(1);}&lt;br /&gt;&lt;br /&gt;    /* Copy source to target, line by line. */&lt;br /&gt;    while (fgets(buf, MAX_LINE_LEN+1, f_from)) &lt;br /&gt;    {&lt;br /&gt;        if (fputs(buf, f_to) == EOF){exit(1);}&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (!feof(f_from)){exit(1);}&lt;br /&gt;&lt;br /&gt;    if (fclose(f_from) == EOF) {exit(1);}&lt;br /&gt;    if (fclose(f_to) == EOF)   {exit(1);}&lt;br /&gt;     &lt;br /&gt;    return(0);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-621053829987924840?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/621053829987924840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=621053829987924840' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/621053829987924840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/621053829987924840'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-your-own-copy-function.html' title='Write your own copy() function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3185636511237135028</id><published>2007-07-07T14:25:00.000-07:00</published><updated>2007-07-07T14:26:15.457-07:00</updated><title type='text'>Implement the substr() function in C.</title><content type='html'>Here is a C program which implements the substr() function in C.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  char str1[] = "India";&lt;br /&gt;  char str2[25];&lt;br /&gt;&lt;br /&gt;  substr(str2, str1, 1, 3);&lt;br /&gt;  printf("\nstr2 : [%s]", str2);&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;substr(char *dest, char *src, int position, int length)&lt;br /&gt;{&lt;br /&gt;    dest[0]='\0';&lt;br /&gt;    strncat(dest, (src + position), length);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is another C program to do the same...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;conio.h&gt;&lt;br /&gt;&lt;br /&gt;void mySubstr(char *dest, char *src, int position, int length);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; char subStr[100];&lt;br /&gt; char str[]="My Name Is Sweet";&lt;br /&gt;&lt;br /&gt; mySubstr(subStr, str, 1, 5);&lt;br /&gt; printf("\nstr    = [%s]"&lt;br /&gt;        "\nsubStr = [%s]\n\n",&lt;br /&gt;        str, subStr);&lt;br /&gt; getch();&lt;br /&gt; return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void mySubstr(char *dest, char *src, int position, int length)&lt;br /&gt;{&lt;br /&gt;  while(length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    *dest = *(src+position);&lt;br /&gt;    dest++;&lt;br /&gt;    src++;&lt;br /&gt;    length--;&lt;br /&gt;  }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3185636511237135028?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3185636511237135028/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3185636511237135028' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3185636511237135028'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3185636511237135028'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/implement-substr-function-in-c.html' title='Implement the substr() function in C.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3764803759851525671</id><published>2007-07-07T14:24:00.001-07:00</published><updated>2007-07-07T14:24:42.455-07:00</updated><title type='text'>Implement the strcmp(str1, str2) function.</title><content type='html'>There are many ways one can implement the strcmp() function. Note that strcmp(str1,str2) returns a -ve number if str1 is alphabetically above str2, 0 if both are equal and +ve if str2 is alphabetically above str1. &lt;br /&gt;&lt;br /&gt;Here are some C programs which implement the strcmp() function. This is also one of the most frequently asked interview questions. The prototype of strcmp() is&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int strcmp( const char *string1, const char *string2 );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some C code..&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int mystrcmp(const char *s1, const char *s2);&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  printf("\nstrcmp() = [%d]\n", mystrcmp("A","A"));&lt;br /&gt;  printf("\nstrcmp() = [%d]\n", mystrcmp("A","B"));&lt;br /&gt;  printf("\nstrcmp() = [%d]\n", mystrcmp("B","A"));&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int mystrcmp(const char *s1, const char *s2)&lt;br /&gt;{&lt;br /&gt;    while (*s1==*s2)&lt;br /&gt;    {&lt;br /&gt;        if(*s1=='\0')&lt;br /&gt;           return(0);&lt;br /&gt;        s1++;&lt;br /&gt;        s2++;&lt;br /&gt;    }&lt;br /&gt;    return(*s1-*s2);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;strcmp() = [0]&lt;br /&gt;strcmp() = [-1]&lt;br /&gt;strcmp() = [1]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3764803759851525671?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3764803759851525671/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3764803759851525671' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3764803759851525671'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3764803759851525671'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/implement-strcmpstr1-str2-function.html' title='Implement the strcmp(str1, str2) function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3565556421652696393</id><published>2007-07-07T14:22:00.000-07:00</published><updated>2007-07-07T14:23:09.117-07:00</updated><title type='text'>Implement the strcpy() function.</title><content type='html'>Here are some C programs which implement the strcpy() function. This is one of the most frequently asked C interview questions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *mystrcpy(char *dst, const char *src)&lt;br /&gt;{&lt;br /&gt;  char *ptr;&lt;br /&gt;  ptr = dst;&lt;br /&gt;  while(*dst++=*src++);&lt;br /&gt;  return(ptr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The strcpy function copies src, including the terminating null character, to the location specified by dst. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap. It returns the destination string. No return value is reserved to indicate an error. &lt;br /&gt;&lt;br /&gt;Note that the prototype of strcpy as per the C standards is &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *strcpy(char *dst, const char *src);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Notice the const for the source, which signifies that the function must not change the source string in anyway!. &lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char *my_strcpy(char dest[], const char source[])&lt;br /&gt;{&lt;br /&gt;  int i = 0;&lt;br /&gt;  while (source[i] != '\0')&lt;br /&gt;  {&lt;br /&gt;    dest[i] = source[i];&lt;br /&gt;    i++;&lt;br /&gt;  }&lt;br /&gt;  dest[i] = '\0';&lt;br /&gt;  return(dest);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Simple, isn't it?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3565556421652696393?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3565556421652696393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3565556421652696393' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3565556421652696393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3565556421652696393'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/implement-strcpy-function.html' title='Implement the strcpy() function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-848275668711000088</id><published>2007-07-07T14:20:00.000-07:00</published><updated>2007-07-07T14:21:27.421-07:00</updated><title type='text'>Write your own printf() function in C.</title><content type='html'>This is again one of the most frequently asked interview questions. Here is a C program which implements a basic version of printf(). This is a really, really simplified version of printf(). Note carefully how floating point and other compilcated support has been left out. Also, note how we use low level puts() and putchar(). Dont make a fool of yourself by using printf() within the implementation of printf()!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;#include&lt;stdarg.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;    void myprintf(char *,...);&lt;br /&gt;    char * convert(unsigned int, int);&lt;br /&gt;    int i=65;&lt;br /&gt;    char str[]="This is my string";&lt;br /&gt;    myprintf("\nMessage = %s%d%x",str,i,i);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void myprintf(char * frmt,...)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    char *p;&lt;br /&gt;    int i;&lt;br /&gt;    unsigned u;&lt;br /&gt;    char *s;&lt;br /&gt;    va_list argp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    va_start(argp, fmt);&lt;br /&gt;&lt;br /&gt;    p=fmt;&lt;br /&gt;    for(p=fmt; *p!='\0';p++)&lt;br /&gt;    {&lt;br /&gt;        if(*p=='%')&lt;br /&gt;        {&lt;br /&gt;            putchar(*p);continue;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        p++;&lt;br /&gt;&lt;br /&gt;        switch(*p)&lt;br /&gt;        {&lt;br /&gt;            case 'c' : i=va_arg(argp,int);putchar(i);break;&lt;br /&gt;            case 'd' : i=va_arg(argp,int);&lt;br /&gt;                            if(i&lt;0){i=-i;putchar('-');}puts(convert(i,10));break;&lt;br /&gt;            case 'o': i=va_arg(argp,unsigned int); puts(convert(i,8));break;&lt;br /&gt;            case 's': s=va_arg(argp,char *); puts(s); break;&lt;br /&gt;            case 'u': u=va_arg(argp,argp, unsigned int); puts(convert(u,10));break;&lt;br /&gt;            case 'x': u=va_arg(argp,argp, unsigned int); puts(convert(u,16));break;&lt;br /&gt;            case '%': putchar('%');break;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    va_end(argp);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char *convert(unsigned int, int)&lt;br /&gt;{&lt;br /&gt;    static char buf[33];&lt;br /&gt;    char *ptr;&lt;br /&gt;    &lt;br /&gt;    ptr=&amp;buf[sizeof(buff)-1];&lt;br /&gt;    *ptr='\0';&lt;br /&gt;    do&lt;br /&gt;    {&lt;br /&gt;        *--ptr="0123456789abcdef"[num%base];&lt;br /&gt;        num/=base;&lt;br /&gt;    }while(num!=0);&lt;br /&gt;    return(ptr);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-848275668711000088?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/848275668711000088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=848275668711000088' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/848275668711000088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/848275668711000088'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-your-own-printf-function-in-c.html' title='Write your own printf() function in C.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4339021150382653937</id><published>2007-07-07T14:17:00.000-07:00</published><updated>2007-07-07T14:19:38.700-07:00</updated><title type='text'>Write C code to implement the strstr() (search for a substring) function.</title><content type='html'>This is also one of the most frequently asked interview questions. Its asked almost 99% of the times. Here are a few C programs to implement your own strstr() function.&lt;br /&gt;&lt;br /&gt;There are a number of ways to find a string inside another string. Its important to be aware of these algorithms than to memorize them. Some of the fastest algorithms are quite tough to understand!.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1&lt;br /&gt;&lt;br /&gt;The first method is the classic Brute force method. The Brute Force algorithm checks, at all positions in the text between 0 and (n-m), if an occurrence of the pattern starts at that position or not. Then, after each successfull or unsuccessful attempt, it shifts the pattern exactly one position to the right. The time complexity of this searching phase is O(mn). The expected number of text character comparisons is 2n. &lt;br /&gt;&lt;br /&gt;Here 'n' is the size of the string in which the substring of size 'm' is being searched for.&lt;br /&gt;&lt;br /&gt;Here is some code (which works!)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;void BruteForce(char *x /* pattern */, &lt;br /&gt;                int m   /* length of the pattern */, &lt;br /&gt;                char *y /* actual string being searched */, &lt;br /&gt;                int n   /* length of this string */)&lt;br /&gt;{&lt;br /&gt;   int i, j;&lt;br /&gt;   printf("\nstring    : [%s]"&lt;br /&gt;          "\nlength    : [%d]"&lt;br /&gt;          "\npattern   : [%s]"&lt;br /&gt;          "\nlength    : [%d]\n\n", y,n,x,m);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   /* Searching */&lt;br /&gt;   for (j = 0; j &lt;= (n - m); ++j)&lt;br /&gt;   {&lt;br /&gt;      for (i = 0; i &lt; m &amp;&amp; x[i] == y[i + j]; ++i);&lt;br /&gt;        if (i &gt;= m) {printf("\nMatch found at\n\n-&gt;[%d]\n-&gt;[%s]\n",j,y+j);}&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  char *string  = "hereroheroero";&lt;br /&gt;  char *pattern = "hero";&lt;br /&gt;&lt;br /&gt;  BF(pattern,strlen(pattern),string,strlen(string));&lt;br /&gt;  printf("\n\n");&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how the comparison happens visually&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt; !&lt;br /&gt; hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;  !&lt;br /&gt;  hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;   hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;    !&lt;br /&gt;    hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;     !&lt;br /&gt;     hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;      |||| ----&gt; Match!&lt;br /&gt;      hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;       !&lt;br /&gt;       hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;        ! &lt;br /&gt;        hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;         !&lt;br /&gt;         hero&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;The second method is called the Rabin-Karp method. &lt;br /&gt;&lt;br /&gt;Instead of checking at each position of the text if the pattern occurs or not, it is better to check first if the contents of the current string "window" looks like the pattern or not. In order to check the resemblance between these two patterns, a hashing function is used. Hashing a string involves computing a numerical value from the value of its characters using a hash function. &lt;br /&gt;&lt;br /&gt;The Rabin-Karp method uses the rule that if two strings are equal, their hash values must also be equal. Note that the converse of this statement is not always true, but a good hash function tries to reduce the number of such hash collisions. Rabin-Karp computes hash value of the pattern, and then goes through the string computing hash values of all of its substrings and checking if the pattern's hash value is equal to the substring hash value, and advancing by 1 character every time. If the two hash values are the same, then the algorithm verifies if the two string really are equal, rather than this being a fluke of the hashing scheme. It uses regular string comparison for this final check. Rabin-Karp is an algorithm of choice for multiple pattern search. If we want to find any of a large number, say k, fixed length patterns in a text, a variant Rabin-Karp that uses a hash table to check whether the hash of a given string belongs to a set of hash values of patterns we are looking for. Other algorithms can search for a single pattern in time order O(n), hence they will search for k patterns in time order O(n*k). The variant Rabin-Karp will still work in time order O(n) in the best and average case because a hash table allows to check whether or not substring hash equals any of the pattern hashes in time order of O(1).&lt;br /&gt;&lt;br /&gt;Here is some code (not working though!)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;hashing_function()&lt;br /&gt;{&lt;br /&gt;  // A hashing function to compute the hash values of the strings.&lt;br /&gt;  ....&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void KarpRabinR(char *x, int m, char *y, int n) &lt;br /&gt;{&lt;br /&gt;   int hx, hy, i, j;&lt;br /&gt;&lt;br /&gt;   printf("\nstring    : [%s]"&lt;br /&gt;          "\nlength    : [%d]"&lt;br /&gt;          "\npattern   : [%s]"&lt;br /&gt;          "\nlength    : [%d]\n\n", y,n,x,m);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   /* Preprocessing  phase */&lt;br /&gt;   Do preprocessing here..&lt;br /&gt;&lt;br /&gt;   /* Searching */&lt;br /&gt;   j = 0;&lt;br /&gt;   while (j &lt;= n-m) &lt;br /&gt;   {&lt;br /&gt;      if (hx == hy &amp;&amp; memcmp(x, y + j, m) == 0)&lt;br /&gt;      {&lt;br /&gt;         // Hashes match and so do the actual strings!&lt;br /&gt;         printf("\nMatch found at : [%d]\n",j);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      hy = hashing_function(y[j], y[j + m], hy);&lt;br /&gt;      ++j;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;  char *string="hereroheroero";&lt;br /&gt;  char *pattern="hero";&lt;br /&gt;&lt;br /&gt;  KarpRabin(pattern,strlen(pattern),string,strlen(string));&lt;br /&gt;&lt;br /&gt;  printf("\n\n");&lt;br /&gt;  return(0);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how the comparison happens visually&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt; !&lt;br /&gt; hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;  !&lt;br /&gt;  hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;   hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;    !&lt;br /&gt;    hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;     !&lt;br /&gt;     hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;      |||| ----&gt; Hash values match, so do the strings!&lt;br /&gt;      hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;       !&lt;br /&gt;       hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;        ! &lt;br /&gt;        hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;         !&lt;br /&gt;         hero&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;The Knuth-Morris-Pratt or the Morris-Pratt algorithms are extensions of the basic Brute Force algorithm. They use precomputed data to skip forward not by 1 character, but by as many as possible for the search to succeed. &lt;br /&gt;&lt;br /&gt;Here is some code&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void preComputeData(char *x, int m, int Next[]) &lt;br /&gt;{&lt;br /&gt;   int i, j;&lt;br /&gt;   i = 0;&lt;br /&gt;   j = Next[0] = -1;&lt;br /&gt;&lt;br /&gt;   while (i &lt; m) &lt;br /&gt;   {&lt;br /&gt;      while (j &gt; -1 &amp;&amp; x[i] != x[j])&lt;br /&gt;         j = Next[j];&lt;br /&gt;      Next[++i] = ++j;&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void MorrisPrat(char *x, int m, char *y, int n) &lt;br /&gt;{&lt;br /&gt;   int i, j, Next[1000];&lt;br /&gt;&lt;br /&gt;   /* Preprocessing */&lt;br /&gt;   preComputeData(x, m, Next);&lt;br /&gt;&lt;br /&gt;   /* Searching */&lt;br /&gt;   i = j = 0;&lt;br /&gt;   while (j &lt; n) &lt;br /&gt;   {&lt;br /&gt;      while (i &gt; -1 &amp;&amp; x[i] != y[j])&lt;br /&gt;         i = Next[i];&lt;br /&gt;      i++;&lt;br /&gt;      j++;&lt;br /&gt;      if (i &gt;= m) &lt;br /&gt;      {&lt;br /&gt;         printf("\nMatch found at : [%d]\n",j - i);&lt;br /&gt;         i = Next[i];&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  char *string="hereroheroero";&lt;br /&gt;  char *pattern="hero";&lt;br /&gt;&lt;br /&gt;  MorrisPrat(pattern,strlen(pattern),string,strlen(string));&lt;br /&gt;&lt;br /&gt;  printf("\n\n");&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how the comparison happens visually&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;   !&lt;br /&gt;   hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;    !&lt;br /&gt;    hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;     !&lt;br /&gt;     hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;      |||| ----&gt; Match found!&lt;br /&gt;      hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hereroheroero&lt;br /&gt;          !&lt;br /&gt;          hero&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method4&lt;br /&gt;&lt;br /&gt;The Boyer Moore algorithm is the fastest string searching algorithm. Most editors use this algorithm.&lt;br /&gt;&lt;br /&gt;It compares the pattern with the actual string from right to left. Most other algorithms compare from left to right. If the character that is compared with the rightmost pattern symbol does not occur in the pattern at all, then the pattern can be shifted by m positions behind this text symbol. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The following example illustrates this situation. &lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 5 6 7 8 9 ... &lt;br /&gt;a b b a d a b a c b a  &lt;br /&gt;        | |&lt;br /&gt;b a b a c |       &lt;br /&gt;  &lt;------ |&lt;br /&gt;          |&lt;br /&gt;          b a b a c  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The comparison of "d" with "c" at position 4 does not match. "d" does not occur in the pattern. Therefore, the pattern cannot match at any of the positions 0,1,2,3,4, since all corresponding windows contain a "d". The pattern can be shifted to position 5. The best case for the Boyer-Moore algorithm happens if, at each search attempt the first compared character does not occur in the pattern. Then the algorithm requires only O(n/m) comparisons . &lt;br /&gt;&lt;br /&gt;Bad character heuristics&lt;br /&gt;&lt;br /&gt;This method is called bad character heuristics. It can also be applied if the bad character (the character that causes a mismatch), occurs somewhere else in the pattern. Then the pattern can be shifted so that it is aligned to this text symbol. The next example illustrates this situation. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 5 6 7 8 9 ... &lt;br /&gt;a b b a b a b a c b a &lt;br /&gt;        | &lt;br /&gt;b a b a c  &lt;br /&gt;    &lt;----&lt;br /&gt;    |      &lt;br /&gt;    b a b a c     &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Comparison between "b" and "c" causes a mismatch. The character "b" occurs in the pattern at positions 0 and 2. The pattern can be shifted so that the rightmost "b" in the pattern is aligned to "b". &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Good suffix heuristics&lt;br /&gt;&lt;br /&gt;Sometimes the bad character heuristics fails. In the following situation the comparison between "a" and "b" causes a mismatch. An alignment of the rightmost occurence of the pattern symbol a with the text symbol a would produce a negative shift. Instead, a shift by 1 would be possible. However, in this case it is better to derive the maximum possible shift distance from the structure of the pattern. This method is called good suffix heuristics. &lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 5 6 7 8 9 ... &lt;br /&gt;a b a a b a b a c b a  &lt;br /&gt;    | | |&lt;br /&gt;c a b a b&lt;br /&gt;    &lt;----&lt;br /&gt;    | | |        &lt;br /&gt;    c a b a b     &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The suffix "ab" has matched. The pattern can be shifted until the next occurence of ab in the pattern is aligned to the text symbols ab, i.e. to position 2. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the following situation the suffix "ab" has matched. There is no other occurence of "ab" in the pattern.Therefore, the pattern can be shifted behind "ab", i.e. to position 5. &lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 5 6 7 8 9 ... &lt;br /&gt;a b c a b a b a c b a  &lt;br /&gt;    | | |&lt;br /&gt;c b a a b        &lt;br /&gt;          c b a a b  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the following situation the suffix "bab" has matched. There is no other occurence of "bab" in the pattern. But in this case the pattern cannot be shifted to position 5 as before, but only to position 3, since a prefix of the pattern "ab" matches the end of "bab". We refer to this situation as case 2 of the good suffix heuristics. &lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 5 6 7 8 9 ... &lt;br /&gt;a a b a b a b a c b a  &lt;br /&gt;  | | | |&lt;br /&gt;a b b a b        &lt;br /&gt;      a b b a b    &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The pattern is shifted by the longest of the two distances that are given by the bad character and the good suffix heuristics. &lt;br /&gt;&lt;br /&gt;The Boyer-Moore algorithm uses two different heuristics for determining the maximum possible shift distance in case of a mismatch: the "bad character" and the "good suffix" heuristics. Both heuristics can lead to a shift distance of m. For the bad character heuristics this is the case, if the first comparison causes a mismatch and the corresponding text symbol does not occur in the pattern at all. For the good suffix heuristics this is the case, if only the first comparison was a match, but that symbol does not occur elsewhere in the pattern.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4339021150382653937?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4339021150382653937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4339021150382653937' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4339021150382653937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4339021150382653937'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-c-code-to-implement-strstr-search.html' title='Write C code to implement the strstr() (search for a substring) function.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5707596925185105243</id><published>2007-07-07T14:14:00.000-07:00</published><updated>2007-07-07T14:16:00.070-07:00</updated><title type='text'>Implement the memmove() function. What is the difference between the memmove() and memcpy() function?</title><content type='html'>One more most frequently asked interview question!.&lt;br /&gt;&lt;br /&gt;memmove() offers guaranteed behavior if the source and destination arguments overlap. memcpy() makes no such guarantee, and may therefore be more efficient to implement. It's always safer to use memmove(). &lt;br /&gt;&lt;br /&gt;Note that the prototype of memmove() is ...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void *memmove(void *dest, const void *src, size_t count);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is an implementation..&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;void *mymemmove(void *dest, const void *src, size_t count);&lt;br /&gt;&lt;br /&gt;int main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;  char *p1, *p2;&lt;br /&gt;  char *p3, *p4;&lt;br /&gt;  int  size;&lt;br /&gt;  &lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;  &lt;br /&gt;  /* ----------------------------------------&lt;br /&gt;   * &lt;br /&gt;   * CASE 1 : From (SRC) &lt; To (DEST) &lt;br /&gt;   *&lt;br /&gt;   *     +--+---------------------+--+&lt;br /&gt;   *     |  |                     |  |&lt;br /&gt;   *     +--+---------------------+--+&lt;br /&gt;   *     ^  ^&lt;br /&gt;   *     |  |&lt;br /&gt;   *   From To&lt;br /&gt;   *&lt;br /&gt;   * --------------------------------------- */&lt;br /&gt; &lt;br /&gt;  p1 = (char *) malloc(12);&lt;br /&gt;  memset(p1,12,'\0');&lt;br /&gt;  size=10;&lt;br /&gt;&lt;br /&gt;  strcpy(p1,"ABCDEFGHI");&lt;br /&gt;  &lt;br /&gt;  p2 = p1 + 2;&lt;br /&gt;&lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;  printf("\nFrom (before) = [%s]",p1);&lt;br /&gt;  printf("\nTo (before)   = [%s]",p2);&lt;br /&gt;&lt;br /&gt;  mymemmove(p2,p1,size);&lt;br /&gt;&lt;br /&gt;  printf("\n\nFrom (after) = [%s]",p1);&lt;br /&gt;  printf("\nTo (after)   = [%s]",p2);&lt;br /&gt;&lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  /* ----------------------------------------&lt;br /&gt;   * &lt;br /&gt;   * CASE 2 : From (SRC) &gt; To (DEST) &lt;br /&gt;   *&lt;br /&gt;   *     +--+---------------------+--+&lt;br /&gt;   *     |  |                     |  |&lt;br /&gt;   *     +--+---------------------+--+&lt;br /&gt;   *     ^  ^&lt;br /&gt;   *     |  |&lt;br /&gt;   *    To From&lt;br /&gt;   *&lt;br /&gt;   * --------------------------------------- */&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  p3 = (char *) malloc(12);&lt;br /&gt;  memset(p3,12,'\0');&lt;br /&gt;  p4 = p3 + 2;&lt;br /&gt;&lt;br /&gt;  strcpy(p4, "ABCDEFGHI");&lt;br /&gt;&lt;br /&gt;  printf("\nFrom (before) = [%s]",p4);&lt;br /&gt;  printf("\nTo (before)   = [%s]",p3);&lt;br /&gt;&lt;br /&gt;  mymemmove(p3, p4, size);&lt;br /&gt;&lt;br /&gt;  printf("\n\nFrom (after) = [%s]",p4);&lt;br /&gt;  printf("\nTo (after)   = [%s]",p3);&lt;br /&gt;&lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  /* ----------------------------------------&lt;br /&gt;   * &lt;br /&gt;   * CASE 3 : No overlap&lt;br /&gt;   *&lt;br /&gt;   * --------------------------------------- */&lt;br /&gt; &lt;br /&gt;  p1 = (char *) malloc(30);&lt;br /&gt;  memset(p1,30,'\0');&lt;br /&gt;  size=10;&lt;br /&gt;&lt;br /&gt;  strcpy(p1,"ABCDEFGHI");&lt;br /&gt;  &lt;br /&gt;  p2 = p1 + 15;&lt;br /&gt;&lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;  printf("\nFrom (before) = [%s]",p1);&lt;br /&gt;  printf("\nTo (before)   = [%s]",p2);&lt;br /&gt;&lt;br /&gt;  mymemmove(p2,p1,size);&lt;br /&gt;&lt;br /&gt;  printf("\n\nFrom (after) = [%s]",p1);&lt;br /&gt;  printf("\nTo (after)   = [%s]",p2);&lt;br /&gt;&lt;br /&gt;  printf("\n--------------------------------\n");&lt;br /&gt;&lt;br /&gt;  printf("\n\n");&lt;br /&gt;  &lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void *mymemmove(void *to, const void *from, size_t size)&lt;br /&gt;{&lt;br /&gt;    unsigned char *p1;      &lt;br /&gt;    const unsigned char *p2;&lt;br /&gt;&lt;br /&gt;    p1 = (unsigned char *) to;&lt;br /&gt;    p2 = (const unsigned char *) from;&lt;br /&gt;&lt;br /&gt;    p2 = p2 + size;&lt;br /&gt;&lt;br /&gt;    // Check if there is an overlap or not.&lt;br /&gt;    while (p2 != from &amp;&amp; --p2 != to);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    if (p2 != from) &lt;br /&gt;    {&lt;br /&gt;        // Overlap detected!&lt;br /&gt;&lt;br /&gt;        p2  = (const unsigned char *) from;&lt;br /&gt;        p2  = p2 + size;&lt;br /&gt;        p1  = p1 + size;&lt;br /&gt;&lt;br /&gt;        while (size-- != 0) &lt;br /&gt;        {&lt;br /&gt;            *--p1 = *--p2;&lt;br /&gt;        } &lt;br /&gt;    } &lt;br /&gt;    else &lt;br /&gt;    {&lt;br /&gt;        // No overlap OR they overlap as CASE 2 above.&lt;br /&gt;        // memcopy() would have done this directly.&lt;br /&gt; &lt;br /&gt;        while (size-- != 0) &lt;br /&gt;        {&lt;br /&gt;            *p1++ = *p2++;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return(to);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And here is the output&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------&lt;br /&gt;&lt;br /&gt;From (before) = [ABCDEFGHI]&lt;br /&gt;To   (before) = [CDEFGHI]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;From (after) = [ABABCDEFGHI]&lt;br /&gt;To   (after) = [ABCDEFGHI]&lt;br /&gt;&lt;br /&gt;--------------------------------&lt;br /&gt;&lt;br /&gt;From (before) = [ABCDEFGHI]&lt;br /&gt;To   (before) = [α╙ABCDEFGHI]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;From (after) = [CDEFGHI]&lt;br /&gt;To   (after) = [ABCDEFGHI]&lt;br /&gt;&lt;br /&gt;--------------------------------&lt;br /&gt;&lt;br /&gt;From (before) = [ABCDEFGHI]&lt;br /&gt;To   (before) = [FEδ‼&amp;:F]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;From (after) = [ABCDEFGHI]&lt;br /&gt;To (after)   = [ABCDEFGHI]&lt;br /&gt;&lt;br /&gt;--------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So then, whats the difference between the implementation of memmove() and memcpy(). Its just that memcpy() will not care if the memories overlap and will either copy from left to right or right to left without checking which method to used depending on the type of the overlap. Also note that the C code proves that the results are the same irrespective of the Endian-ness of the machine.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5707596925185105243?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5707596925185105243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5707596925185105243' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5707596925185105243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5707596925185105243'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/implement-memmove-function-what-is.html' title='Implement the memmove() function. What is the difference between the memmove() and memcpy() function?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-6771941869403543519</id><published>2007-07-07T14:10:00.000-07:00</published><updated>2007-07-07T14:12:47.893-07:00</updated><title type='text'>Write your own C program to implement the atoi() function</title><content type='html'>The prototype of the atoi() function is ...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int atoi(const char *string);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is a C program which explains a different way of coding the atoi() function in the C language.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int myatoi(const char *string);&lt;br /&gt;&lt;br /&gt;int main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;  printf("\n%d\n", myatoi("1998")); &lt;br /&gt;  getch();&lt;br /&gt;  return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int myatoi(const char *string)&lt;br /&gt;{&lt;br /&gt;    int i;&lt;br /&gt;    i=0;&lt;br /&gt;    while(*string)&lt;br /&gt;    {&lt;br /&gt;        i=(i&lt;&lt;3) + (i&lt;&lt;1) + (*string - '0');&lt;br /&gt;        string++;&lt;br /&gt;&lt;br /&gt;        // Dont increment i!&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;    return(i);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Try working it out with a small string like "1998", you will find out it does work!.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-6771941869403543519?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/6771941869403543519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=6771941869403543519' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6771941869403543519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6771941869403543519'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/07/write-your-own-c-program-to-implement.html' title='Write your own C program to implement the atoi() function'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5264766143987673035</id><published>2007-05-26T03:45:00.000-07:00</published><updated>2007-05-26T03:46:11.278-07:00</updated><title type='text'>How can I search for data in a linked list?</title><content type='html'>The only way to search a linked list is with a linear search, because the only way a linked lists members can be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5264766143987673035?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5264766143987673035/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5264766143987673035' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5264766143987673035'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5264766143987673035'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-can-i-search-for-data-in-linked.html' title='How can I search for data in a linked list?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4726674719680587375</id><published>2007-05-26T03:43:00.001-07:00</published><updated>2007-05-26T03:45:04.541-07:00</updated><title type='text'>How to read a singly linked list backwards?</title><content type='html'>Use Recursion.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4726674719680587375?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4726674719680587375/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4726674719680587375' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4726674719680587375'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4726674719680587375'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-to-read-singly-linked-list_26.html' title='How to read a singly linked list backwards?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2677703386992006315</id><published>2007-05-26T03:43:00.000-07:00</published><updated>2007-05-26T03:44:35.760-07:00</updated><title type='text'>How to read a singly linked list backwards?</title><content type='html'>Use Recursion.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2677703386992006315?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2677703386992006315/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2677703386992006315' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2677703386992006315'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2677703386992006315'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-to-read-singly-linked-list.html' title='How to read a singly linked list backwards?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7418675706872448280</id><published>2007-05-26T03:41:00.001-07:00</published><updated>2007-05-26T03:42:58.159-07:00</updated><title type='text'>Write a C program to remove duplicates from a sorted linked list.</title><content type='html'>As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Remove duplicates from a sorted list&lt;br /&gt;void RemoveDuplicates(struct node* head) &lt;br /&gt;{&lt;br /&gt;  struct node* current = head;&lt;br /&gt;  if (current == NULL) return; // do nothing if the list is empty&lt;br /&gt;&lt;br /&gt;  // Compare current node with next node&lt;br /&gt;  while(current-&gt;next!=NULL) &lt;br /&gt;  {&lt;br /&gt;      if (current-&gt;data == current-&gt;next-&gt;data) &lt;br /&gt;      {&lt;br /&gt;         struct node* nextNext = current-&gt;next-&gt;next;&lt;br /&gt;         free(current-&gt;next);&lt;br /&gt;         current-&gt;next = nextNext;&lt;br /&gt;      }&lt;br /&gt;      else &lt;br /&gt;      {&lt;br /&gt;         current = current-&gt;next; // only advance if no deletion&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7418675706872448280?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7418675706872448280/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7418675706872448280' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7418675706872448280'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7418675706872448280'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/write-c-program-to-remove-duplicates_26.html' title='Write a C program to remove duplicates from a sorted linked list.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-502812014048915038</id><published>2007-05-26T03:41:00.000-07:00</published><updated>2007-05-26T03:42:16.087-07:00</updated><title type='text'>Write a C program to remove duplicates from a sorted linked list.</title><content type='html'>As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Remove duplicates from a sorted list&lt;br /&gt;void RemoveDuplicates(struct node* head) &lt;br /&gt;{&lt;br /&gt;  struct node* current = head;&lt;br /&gt;  if (current == NULL) return; // do nothing if the list is empty&lt;br /&gt;&lt;br /&gt;  // Compare current node with next node&lt;br /&gt;  while(current-&gt;next!=NULL) &lt;br /&gt;  {&lt;br /&gt;      if (current-&gt;data == current-&gt;next-&gt;data) &lt;br /&gt;      {&lt;br /&gt;         struct node* nextNext = current-&gt;next-&gt;next;&lt;br /&gt;         free(current-&gt;next);&lt;br /&gt;         current-&gt;next = nextNext;&lt;br /&gt;      }&lt;br /&gt;      else &lt;br /&gt;      {&lt;br /&gt;         current = current-&gt;next; // only advance if no deletion&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-502812014048915038?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/502812014048915038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=502812014048915038' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/502812014048915038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/502812014048915038'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/write-c-program-to-remove-duplicates.html' title='Write a C program to remove duplicates from a sorted linked list.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-4453672667996187357</id><published>2007-05-26T03:33:00.000-07:00</published><updated>2007-05-26T03:41:25.103-07:00</updated><title type='text'>Write a C program to insert nodes into a linked list in a sorted fashion</title><content type='html'>The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node. &lt;br /&gt;&lt;br /&gt;Note that we assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Special case code for the head end&lt;br /&gt;void linkedListInsertSorted(struct node** headReference, struct node* newNode) &lt;br /&gt;{&lt;br /&gt;  // Special case for the head end&lt;br /&gt;  if (*headReference == NULL || (*headReference)-&gt;data &gt;= newNode-&gt;data) &lt;br /&gt;  {&lt;br /&gt;     newNode-&gt;next = *headReference;&lt;br /&gt;     *headReference = newNode;&lt;br /&gt;  }&lt;br /&gt;  else &lt;br /&gt;  {&lt;br /&gt;     // Locate the node before which the insertion is to happen!&lt;br /&gt;     struct node* current = *headReference;&lt;br /&gt;     while (current-&gt;next!=NULL &amp;&amp; current-&gt;next-&gt;data &lt; newNode-&gt;data) &lt;br /&gt;     {&lt;br /&gt;        current = current-&gt;next;&lt;br /&gt;     }&lt;br /&gt;     newNode-&gt;next = current-&gt;next;&lt;br /&gt;     current-&gt;next = newNode;&lt;br /&gt;   }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-4453672667996187357?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/4453672667996187357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=4453672667996187357' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4453672667996187357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/4453672667996187357'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/write-c-program-to-insert-nodes-into.html' title='Write a C program to insert nodes into a linked list in a sorted fashion'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2224056718998951030</id><published>2007-05-25T20:48:00.000-07:00</published><updated>2007-05-25T20:49:01.776-07:00</updated><title type='text'>How would you find out if one of the pointers in a linked list is corrupted or not?</title><content type='html'>This is a really good interview question. The reason is that linked lists are used in a wide variety of scenarios and being able to detect and correct pointer corruptions might be a very valuable tool. For example, data blocks associated with files in a file system are usually stored as linked lists. Each data block points to the next data block. A single corrupt pointer can cause the entire file to be lost!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it). &lt;br /&gt;&lt;br /&gt;It is good programming practice to set the pointer value to NULL immediately after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required. &lt;br /&gt;&lt;br /&gt;Use a good debugger to see how the datastructures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily.&lt;br /&gt;&lt;br /&gt;Avoid global variables when traversing and manipulating linked lists. Imagine what would happen if a function which is only supposed to traverse a linked list using a global head pointer accidently sets the head pointer to NULL!. &lt;br /&gt;&lt;br /&gt;Its a good idea to check the addNode() and the deleteNode() routines and test them for all types of scenarios. This should include tests for inserting/deleting nodes at the front/middle/end of the linked list, working with an empty linked list, running out of memory when using malloc() when allocating memory for new nodes, writing through NULL pointers, writing more data into the node fields then they can hold (resulting in corrupting the (probably adjacent) "prev" and "next" pointer fields), make sure bug fixes and enhancements to the linked list code are reviewed and well tested (a lot of bugs come from quick and dirty bug fixing), log and handle all possible errors (this will help you a lot while debugging), add multiple levels of logging so that you can dig through the logs. The list is endless...&lt;br /&gt;&lt;br /&gt;Each node can have an extra field associated with it. This field indicates the number of nodes after this node in the linked list. This extra field needs to be kept up-to-date when we inserte or delete nodes in the linked list (It might become slightly complicated when insertion or deletion happens not at end, but anywhere in the linked list). Then, if for any node, p-&gt;field &gt; 0 and p-&gt;next == NULL, it surely points to a pointer corruption.&lt;br /&gt;&lt;br /&gt;You could also keep the count of the total number of nodes in a linked list and use it to check if the list is indeed having those many nodes or not.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The problem in detecting such pointer corruptions in C is that its only the programmer who knows that the pointer is corrupted. The program has no way of knowing that something is wrong. So the best way to fix these errors is check your logic and test your code to the maximum possible extent. I am not aware of ways in C to recover the lost nodes of a corrupted linked list. C does not track pointers so there is no good way to know if an arbitrary pointer has been corrupted or not. The platform may have a library service that checks if a pointer points to valid memory (for instance on Win32 there is a IsBadReadPtr, IsBadWritePtr API.) If you detect a cycle in the link list, it's definitely bad. If it's a doubly linked list you can verify, pNode-&gt;Next-&gt;Prev == pNode.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I have a hunch that interviewers who ask this question are probably hinting at something called Smart Pointers in C++. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. This topic is out of scope here, but you can find lots of material on the Internet for Smart Pointers.&lt;br /&gt;&lt;br /&gt;If you have better answers to this question, let me know!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2224056718998951030?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2224056718998951030/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2224056718998951030' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2224056718998951030'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2224056718998951030'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-would-you-find-out-if-one-of.html' title='How would you find out if one of the pointers in a linked list is corrupted or not?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-5003156596534962207</id><published>2007-05-25T20:47:00.000-07:00</published><updated>2007-05-25T20:48:03.310-07:00</updated><title type='text'>Write a C program to return the nth node from the end of a linked list.</title><content type='html'>Here is a solution which is often called as the solution that uses frames. &lt;br /&gt;&lt;br /&gt;Suppose one needs to get to the 6th node from the end in this LL. First, just keep on incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in this case)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;STEP 1    :   1(ptr1,ptr2) -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; 10&lt;br /&gt;&lt;br /&gt;STEP 2    :   1(ptr2) -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6(ptr1) -&gt; 7 -&gt; 8 -&gt; 9 -&gt; 10&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1) reaches the end of the LL.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;STEP 3    :   1 -&gt; 2 -&gt; 3 -&gt; 4(ptr2) -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; 10 (ptr1)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So here you have!, the 6th node from the end pointed to by ptr2! &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is some C code..&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;struct node&lt;br /&gt;{&lt;br /&gt;  int data;&lt;br /&gt;  struct node *next;&lt;br /&gt;}mynode;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)&lt;br /&gt;{&lt;br /&gt;  mynode *ptr1,*ptr2;&lt;br /&gt;  int count;&lt;br /&gt;&lt;br /&gt;  if(!head)&lt;br /&gt;  {&lt;br /&gt;    return(NULL);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ptr1  = head;&lt;br /&gt;  ptr2  = head;&lt;br /&gt;  count = 0;&lt;br /&gt;&lt;br /&gt;  while(count &lt; n)&lt;br /&gt;  {&lt;br /&gt;     count++;&lt;br /&gt;     if((ptr1=ptr1-&gt;next)==NULL)&lt;br /&gt;     {&lt;br /&gt;        //Length of the linked list less than n. Error.&lt;br /&gt;        return(NULL);&lt;br /&gt;     }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  while((ptr1=ptr1-&gt;next)!=NULL)&lt;br /&gt;  {&lt;br /&gt;    ptr2=ptr2-&gt;next;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return(ptr2);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-5003156596534962207?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/5003156596534962207/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=5003156596534962207' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5003156596534962207'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/5003156596534962207'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/write-c-program-to-return-nth-node-from.html' title='Write a C program to return the nth node from the end of a linked list.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3570224752353680995</id><published>2007-05-25T20:46:00.000-07:00</published><updated>2007-05-25T20:47:00.192-07:00</updated><title type='text'>Can we do a Binary search on a linked list?</title><content type='html'>Great C datastructure question!&lt;br /&gt;&lt;br /&gt;The answer is ofcourse, you can write a C program to do this. But, the question is, do you really think it will be as efficient as a C program which does a binary search on an array? &lt;br /&gt;&lt;br /&gt;Think hard, real hard.&lt;br /&gt;&lt;br /&gt;Do you know what exactly makes the binary search on an array so fast and efficient? Its the ability to access any element in the array in constant time. This is what makes it so fast. You can get to the middle of the array just by saying array[middle]!. Now, can you do the same with a linked list? The answer is No. You will have to write your own, possibly inefficient algorithm to get the value of the middle node of a linked list. In a linked list, you loosse the ability to get the value of any node in a constant time.&lt;br /&gt;&lt;br /&gt;One solution to the inefficiency of getting the middle of the linked list during a binary search is to have the first node contain one additional pointer that points to the node in the middle. Decide at the first node if you need to check the first or the second half of the linked list. Continue doing that with each half-list.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3570224752353680995?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3570224752353680995/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3570224752353680995' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3570224752353680995'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3570224752353680995'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/can-we-do-binary-search-on-linked-list.html' title='Can we do a Binary search on a linked list?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2338686616978969536</id><published>2007-05-25T20:42:00.000-07:00</published><updated>2007-05-25T20:43:47.441-07:00</updated><title type='text'>Write a C program to free the nodes of a linked list</title><content type='html'>Before looking at the answer, try writing a simple C program (with a for loop) to do this. Quite a few people get this wrong.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is the wrong way to do it&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;struct list *listptr, *nextptr;&lt;br /&gt;for(listptr = head; listptr != NULL; listptr = listptr-&gt;next) &lt;br /&gt;{&lt;br /&gt;  free(listptr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you are thinking why the above piece of code is wrong, note that once you free the listptr node, you cannot do something like listptr = listptr-&gt;next!. Since listptr is already freed, using it to get listptr-&gt;next is illegal and can cause unpredictable results!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is the right way to do it&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;struct list *listptr, *nextptr;&lt;br /&gt;for(listptr = head; listptr != NULL; listptr = nextptr) &lt;br /&gt;{&lt;br /&gt;  nextptr = listptr-&gt;next;&lt;br /&gt;  free(listptr);&lt;br /&gt;}&lt;br /&gt;head = NULL;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After doing this, make sure you also set the head pointer to NULL!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2338686616978969536?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2338686616978969536/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2338686616978969536' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2338686616978969536'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2338686616978969536'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/write-c-program-to-free-nodes-of-linked.html' title='Write a C program to free the nodes of a linked list'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-8218893363170847180</id><published>2007-05-25T20:38:00.000-07:00</published><updated>2007-05-25T20:39:49.015-07:00</updated><title type='text'>How to create a copy of a linked list? Write a C program to create a copy of a linked list.</title><content type='html'>Check out this C program which creates an exact copy of a linked list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;copy_linked_lists(struct node *q, struct node **s)&lt;br /&gt;{&lt;br /&gt;    if(q!=NULL)&lt;br /&gt;    {&lt;br /&gt;        *s=malloc(sizeof(struct node));&lt;br /&gt;        (*s)-&gt;data=q-&gt;data;&lt;br /&gt;        (*s)-&gt;link=NULL;&lt;br /&gt;        copy_linked_list(q-&gt;link, &amp;((*s)-&gt;link));&lt;br /&gt;    }&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-8218893363170847180?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/8218893363170847180/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=8218893363170847180' title='21 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8218893363170847180'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/8218893363170847180'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-to-create-copy-of-linked-list-write.html' title='How to create a copy of a linked list? Write a C program to create a copy of a linked list.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>21</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-6781983727424874839</id><published>2007-05-24T10:29:00.001-07:00</published><updated>2007-05-24T10:29:35.897-07:00</updated><title type='text'>How to compare two linked lists? Write a C program to compare two linked lists.</title><content type='html'>Here is a simple C program to accomplish the same. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int compare_linked_lists(struct node *q, struct node *r)&lt;br /&gt;{&lt;br /&gt;    static int flag;&lt;br /&gt;    &lt;br /&gt;    if((q==NULL ) &amp;&amp; (r==NULL))&lt;br /&gt;    {&lt;br /&gt;         flag=1;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        if(q==NULL || r==NULL)&lt;br /&gt;        {&lt;br /&gt;            flag=0;&lt;br /&gt;        }&lt;br /&gt;        if(q-&gt;data!=r-&gt;data)&lt;br /&gt;        {&lt;br /&gt;            flag=0;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;           compare_linked_lists(q-&gt;link,r-&gt;link);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return(flag);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Another way is to do it on similar lines as strcmp() compares two strings, character by character (here each node is like a character).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-6781983727424874839?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/6781983727424874839/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=6781983727424874839' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6781983727424874839'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6781983727424874839'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-to-compare-two-linked-lists-write-c.html' title='How to compare two linked lists? Write a C program to compare two linked lists.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-2389276280942926595</id><published>2007-05-24T10:23:00.000-07:00</published><updated>2007-05-24T10:24:29.537-07:00</updated><title type='text'>If you are using C language to implement the heterogeneous linked list, what pointer type will you use?</title><content type='html'>The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-2389276280942926595?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/2389276280942926595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=2389276280942926595' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2389276280942926595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/2389276280942926595'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/if-you-are-using-c-language-to.html' title='If you are using C language to implement the heterogeneous linked list, what pointer type will you use?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-832254493459239410</id><published>2007-05-24T10:21:00.001-07:00</published><updated>2007-05-24T10:22:27.327-07:00</updated><title type='text'>How do you find the middle of a linked list? Write a C program to return the middle of a linked list</title><content type='html'>Here are a few C program snippets to give you an idea of the possible solutions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1 (Uses one slow pointer and one fast pointer)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;#include&lt;ctype.h&gt;&lt;br /&gt;&lt;br /&gt;typedef struct node&lt;br /&gt;{&lt;br /&gt;  int value;&lt;br /&gt;  struct node *next;&lt;br /&gt;  struct node *prev;&lt;br /&gt;}mynode ;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void add_node(struct node **head, int value);&lt;br /&gt;void print_list(char *listName, struct node *head);&lt;br /&gt;void getTheMiddle(mynode *head);&lt;br /&gt;&lt;br /&gt;// The main function..&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   mynode *head;&lt;br /&gt;   head = (struct node *)NULL;&lt;br /&gt; &lt;br /&gt;   add_node(&amp;head, 1);&lt;br /&gt;   add_node(&amp;head, 10);&lt;br /&gt;   add_node(&amp;head, 5);&lt;br /&gt;   add_node(&amp;head, 70);&lt;br /&gt;   add_node(&amp;head, 9);&lt;br /&gt;   add_node(&amp;head, -99);&lt;br /&gt;   add_node(&amp;head, 0);&lt;br /&gt;   add_node(&amp;head, 555);&lt;br /&gt;   add_node(&amp;head, 55);&lt;br /&gt; &lt;br /&gt;   print_list("myList", head);&lt;br /&gt;   getTheMiddle(head);&lt;br /&gt;&lt;br /&gt;   getch();&lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// This function uses one slow and one fast&lt;br /&gt;// pointer to get to the middle of the LL.&lt;br /&gt;//&lt;br /&gt;// The slow pointer is advanced only by one node&lt;br /&gt;// and the fast pointer is advanced by two nodes!&lt;br /&gt;&lt;br /&gt;void getTheMiddle(mynode *head)&lt;br /&gt;{&lt;br /&gt;  mynode *p = head;&lt;br /&gt;  mynode *q = head;&lt;br /&gt;&lt;br /&gt;  if(q!=NULL)&lt;br /&gt;  {&lt;br /&gt;       while((q-&gt;next)!=NULL &amp;&amp; (q-&gt;next-&gt;next)!=NULL)&lt;br /&gt;       {&lt;br /&gt;          p=(p!=(mynode *)NULL?p-&gt;next:(mynode *)NULL);&lt;br /&gt;          q=(q!=(mynode *)NULL?q-&gt;next:(mynode *)NULL);&lt;br /&gt;          q=(q!=(mynode *)NULL?q-&gt;next:(mynode *)NULL);&lt;br /&gt;       }&lt;br /&gt;       printf("The middle element is [%d]",p-&gt;value);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to add a node&lt;br /&gt;void add_node(struct node **head, int value)&lt;br /&gt;{&lt;br /&gt;  mynode *temp, *cur;&lt;br /&gt;  temp = (mynode *)malloc(sizeof(mynode));&lt;br /&gt;  temp-&gt;next=NULL;&lt;br /&gt;  temp-&gt;prev=NULL;&lt;br /&gt;&lt;br /&gt;  if(*head == NULL)&lt;br /&gt;  {&lt;br /&gt;     *head=temp;&lt;br /&gt;     temp-&gt;value=value;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;   for(cur=*head;cur-&gt;next!=NULL;cur=cur-&gt;next);&lt;br /&gt;   cur-&gt;next=temp;&lt;br /&gt;   temp-&gt;prev=cur;&lt;br /&gt;   temp-&gt;value=value;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to print the linked list...&lt;br /&gt;void print_list(char *listName, struct node *head)&lt;br /&gt;{&lt;br /&gt;  mynode *temp;&lt;br /&gt;&lt;br /&gt;  printf("\n[%s] -&gt; ", listName);&lt;br /&gt;  for(temp=head;temp!=NULL;temp=temp-&gt;next)&lt;br /&gt;  {&lt;br /&gt;    printf("[%d]-&gt;",temp-&gt;value);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  printf("NULL\n");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here p moves one step, where as q moves two steps, when q reaches end, p will be at the middle of the linked list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2(Uses a counter)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;#include&lt;ctype.h&gt;&lt;br /&gt;&lt;br /&gt;typedef struct node&lt;br /&gt;{&lt;br /&gt;  int value;&lt;br /&gt;  struct node *next;&lt;br /&gt;  struct node *prev;&lt;br /&gt;}mynode ;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void add_node(struct node **head, int value);&lt;br /&gt;void print_list(char *listName, struct node *head);&lt;br /&gt;mynode *getTheMiddle(mynode *head);&lt;br /&gt;&lt;br /&gt;// The main function..&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   mynode *head, *middle;&lt;br /&gt;   head = (struct node *)NULL;&lt;br /&gt; &lt;br /&gt;   add_node(&amp;head, 1);&lt;br /&gt;   add_node(&amp;head, 10);&lt;br /&gt;   add_node(&amp;head, 5);&lt;br /&gt;   add_node(&amp;head, 70);&lt;br /&gt;   add_node(&amp;head, 9);&lt;br /&gt;   add_node(&amp;head, -99);&lt;br /&gt;   add_node(&amp;head, 0);&lt;br /&gt;   add_node(&amp;head, 555);&lt;br /&gt;   add_node(&amp;head, 55);&lt;br /&gt; &lt;br /&gt;   print_list("myList", head);&lt;br /&gt;   middle = getTheMiddle(head);&lt;br /&gt;   printf("\nMiddle node -&gt; [%d]\n\n", middle-&gt;value);&lt;br /&gt;&lt;br /&gt;   getch();&lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to get to the middle of the LL&lt;br /&gt;mynode *getTheMiddle(mynode *head)&lt;br /&gt;{&lt;br /&gt;  mynode *middle = (mynode *)NULL;&lt;br /&gt;  int i;&lt;br /&gt; &lt;br /&gt;  for(i=1; head!=(mynode *)NULL; head=head-&gt;next,i++)&lt;br /&gt;  {&lt;br /&gt;     if(i==1)&lt;br /&gt;        middle=head;&lt;br /&gt;     else if ((i%2)==1)&lt;br /&gt;        middle=middle-&gt;next;&lt;br /&gt;   }&lt;br /&gt;   &lt;br /&gt;   return middle;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to add a new node to the LL&lt;br /&gt;void add_node(struct node **head, int value)&lt;br /&gt;{&lt;br /&gt;  mynode *temp, *cur;&lt;br /&gt;  temp = (mynode *)malloc(sizeof(mynode));&lt;br /&gt;  temp-&gt;next=NULL;&lt;br /&gt;  temp-&gt;prev=NULL;&lt;br /&gt;&lt;br /&gt;  if(*head == NULL)&lt;br /&gt;  {&lt;br /&gt;     *head=temp;&lt;br /&gt;     temp-&gt;value=value;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;   for(cur=*head;cur-&gt;next!=NULL;cur=cur-&gt;next);&lt;br /&gt;   cur-&gt;next=temp;&lt;br /&gt;   temp-&gt;prev=cur;&lt;br /&gt;   temp-&gt;value=value;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Function to print the LL&lt;br /&gt;void print_list(char *listName, struct node *head)&lt;br /&gt;{&lt;br /&gt;  mynode *temp;&lt;br /&gt;&lt;br /&gt;  printf("\n[%s] -&gt; ", listName);&lt;br /&gt;  for(temp=head;temp!=NULL;temp=temp-&gt;next)&lt;br /&gt;  {&lt;br /&gt;    printf("[%d]-&gt;",temp-&gt;value);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  printf("NULL\n");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In a similar way, we can find the 1/3 th node of linked list by changing (i%2==1) to (i%3==1) and in the same way we can find nth node of list by changing (i%2==1) to (i%n==1) but make sure ur (n&lt;=i).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-832254493459239410?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/832254493459239410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=832254493459239410' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/832254493459239410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/832254493459239410'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-do-you-find-middle-of-linked-list.html' title='How do you find the middle of a linked list? Write a C program to return the middle of a linked list'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-3433508826434796788</id><published>2007-05-24T10:10:00.000-07:00</published><updated>2007-05-24T10:18:24.088-07:00</updated><title type='text'>How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.</title><content type='html'>&lt;p&gt;This is also one of the classic interview questions&lt;br /&gt;There are multiple answers to this problem. Here are a few C programs to attack this problem.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Brute force method&lt;br /&gt;&lt;/strong&gt;Have a double loop, where you check the node pointed to by the outer loop, with every node of the inner loop.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;typedef struct node&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt; void *data;  &lt;/p&gt;&lt;p&gt;struct node *next;&lt;/p&gt;&lt;p&gt;}mynode; &lt;/p&gt;&lt;p&gt;&lt;br /&gt;mynode * find_loop(NODE * head)&lt;/p&gt;&lt;p&gt;{  &lt;/p&gt;&lt;p&gt;mynode *current = head;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;while(current-&gt;next != NULL)  &lt;/p&gt;&lt;p&gt;{    &lt;/p&gt;&lt;p&gt;mynode *temp = head;    &lt;/p&gt;&lt;p&gt;while(temp-&gt;next != NULL &amp;&amp;amp; temp != current)    &lt;/p&gt;&lt;p&gt;{      &lt;/p&gt;&lt;p&gt;if(current-&gt;next == temp)      &lt;/p&gt;&lt;p&gt;{        &lt;/p&gt;&lt;p&gt;printf("\nFound a loop.");&lt;/p&gt;&lt;p&gt;return current;     &lt;/p&gt;&lt;p&gt; }      &lt;/p&gt;&lt;p&gt;temp = temp-&gt;next;    &lt;/p&gt;&lt;p&gt;}    &lt;/p&gt;&lt;p&gt;current = current-&gt;next;  &lt;/p&gt;&lt;p&gt;}  &lt;/p&gt;&lt;p&gt;return NULL;&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Visited flag&lt;br /&gt;&lt;/strong&gt;Have a visited flag in each node of the linked list. Flag it as visited when you reach the node. When you reach a node and the flag is already flagged as visited, then you know there is a loop in the linked list.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Fastest method&lt;br /&gt;&lt;/strong&gt;Have 2 pointers to start of the linked list. Increment one pointer by 1 node and the other by 2 nodes. If there's a loop, the 2nd pointer will meet the 1st pointer somewhere. If it does, then you know there's one.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Here is some code&lt;br /&gt;p=head;&lt;/p&gt;&lt;p&gt;q=head-&gt;next;&lt;br /&gt;while(p!=NULL &amp;&amp;amp; q!=NULL)&lt;/p&gt;&lt;p&gt;{  &lt;/p&gt;&lt;p&gt;if(p==q)  {    //Loop detected!    exit(0);  }  &lt;/p&gt;&lt;p&gt;p=p-&gt;next;  &lt;/p&gt;&lt;p&gt;q=(q-&gt;next)?(q-&gt;next-&gt;next):q-&gt;next;&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;// No loop.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-3433508826434796788?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/3433508826434796788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=3433508826434796788' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3433508826434796788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/3433508826434796788'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-would-you-detect-loop-in-linked.html' title='How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-7646386090936860904</id><published>2007-05-24T10:08:00.000-07:00</published><updated>2007-05-24T10:09:05.228-07:00</updated><title type='text'>How do you reverse a linked list without using any C pointers?</title><content type='html'>One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the "next" pointer fields to traverse through the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-7646386090936860904?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/7646386090936860904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=7646386090936860904' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7646386090936860904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/7646386090936860904'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-do-you-reverse-linked-list-without.html' title='How do you reverse a linked list without using any C pointers?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-128119443443961030</id><published>2007-05-20T01:09:00.000-07:00</published><updated>2007-05-20T01:11:17.825-07:00</updated><title type='text'>Generic Link List</title><content type='html'>&lt;TR&gt;&lt;br /&gt;&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=90%%&gt;&lt;center&gt;&amp;nbsp;&lt;/center&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;font class=announceLink&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--THE QUESTION--&gt;&lt;br /&gt;&lt;br /&gt;Write a C program to implement a Generic Linked List. &lt;BR&gt;&lt;br /&gt;&lt;!--END--&gt;&lt;br /&gt;&lt;BR&gt;&lt;/font&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;font class=normal&gt;&lt;br /&gt;&lt;!--THE ANSWER--&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--META:START--&gt;&lt;!--interview,C,linked list,generic,void--&gt;&lt;!--META:END--&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Here is a C program which implements a generic linked list. This is also one of the very &lt;font class=announcelink&gt;popular&lt;/font&gt; interview questions thrown around. The crux of the solution is to use the &lt;font class=announcelink&gt;void&lt;/font&gt; C pointer to make it &lt;font class=announcelink&gt;generic&lt;/font&gt;. Also notice how we use &lt;font class=announcelink&gt;function pointers&lt;/font&gt; to pass the address of different functions to print the different &lt;font class=announcelink&gt;generic&lt;/font&gt; data.&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;nbsp;&amp;lt;stdio.h&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;nbsp;&amp;lt;stdlib.h&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;nbsp;&amp;lt;string.h&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;list&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&amp;nbsp;*data;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;list&amp;nbsp;*next;&lt;BR&gt;&lt;br /&gt;}&amp;nbsp;List;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;check&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&amp;nbsp;i;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char&amp;nbsp;c;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double&amp;nbsp;d;&lt;BR&gt;&lt;br /&gt;}&amp;nbsp;chk[]&amp;nbsp;=&amp;nbsp;{&amp;nbsp;{&amp;nbsp;1,&amp;nbsp;'a',&amp;nbsp;1.1&amp;nbsp;},&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;2,&amp;nbsp;'b',&amp;nbsp;2.2&amp;nbsp;},&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;3,&amp;nbsp;'c',&amp;nbsp;3.3&amp;nbsp;}&amp;nbsp;};&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;insert(List&amp;nbsp;**,&amp;nbsp;void&amp;nbsp;*,&amp;nbsp;unsigned&amp;nbsp;int);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print(List&amp;nbsp;*,&amp;nbsp;void&amp;nbsp;(*)(void&amp;nbsp;*));&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printstr(void&amp;nbsp;*);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printint(void&amp;nbsp;*);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printchar(void&amp;nbsp;*);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printcomp(void&amp;nbsp;*);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;List&amp;nbsp;*list1,&amp;nbsp;*list2,&amp;nbsp;*list3,&amp;nbsp;*list4;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;int&amp;nbsp;main(void)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char&amp;nbsp;c[]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;{&amp;nbsp;'a',&amp;nbsp;'b',&amp;nbsp;'c',&amp;nbsp;'d'&amp;nbsp;};&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&amp;nbsp;i[]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;{&amp;nbsp;1,&amp;nbsp;2,&amp;nbsp;3,&amp;nbsp;4&amp;nbsp;};&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char&amp;nbsp;*str[]&amp;nbsp;=&amp;nbsp;{&amp;nbsp;"hello1",&amp;nbsp;"hello2",&amp;nbsp;"hello3",&amp;nbsp;"hello4"&amp;nbsp;};&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;list1&amp;nbsp;=&amp;nbsp;list2&amp;nbsp;=&amp;nbsp;list3&amp;nbsp;=&amp;nbsp;list4&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list1,&amp;nbsp;&amp;c[0],&amp;nbsp;sizeof(char));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list1,&amp;nbsp;&amp;c[1],&amp;nbsp;sizeof(char));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list1,&amp;nbsp;&amp;c[2],&amp;nbsp;sizeof(char));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list1,&amp;nbsp;&amp;c[3],&amp;nbsp;sizeof(char));&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list2,&amp;nbsp;&amp;i[0],&amp;nbsp;sizeof(int));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list2,&amp;nbsp;&amp;i[1],&amp;nbsp;sizeof(int));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list2,&amp;nbsp;&amp;i[2],&amp;nbsp;sizeof(int));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list2,&amp;nbsp;&amp;i[3],&amp;nbsp;sizeof(int));&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list3,&amp;nbsp;str[0],&amp;nbsp;strlen(str[0])+1);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list3,&amp;nbsp;str[1],&amp;nbsp;strlen(str[0])+1);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list3,&amp;nbsp;str[2],&amp;nbsp;strlen(str[0])+1);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list3,&amp;nbsp;str[3],&amp;nbsp;strlen(str[0])+1);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list4,&amp;nbsp;&amp;chk[0],&amp;nbsp;sizeof&amp;nbsp;chk[0]);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list4,&amp;nbsp;&amp;chk[1],&amp;nbsp;sizeof&amp;nbsp;chk[1]);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insert(&amp;list4,&amp;nbsp;&amp;chk[2],&amp;nbsp;sizeof&amp;nbsp;chk[2]);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("Printing&amp;nbsp;characters:");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print(list1,&amp;nbsp;printchar);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;:&amp;nbsp;done\n\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("Printing&amp;nbsp;integers:");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print(list2,&amp;nbsp;printint);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;:&amp;nbsp;done\n\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("Printing&amp;nbsp;strings:");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print(list3,&amp;nbsp;printstr);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;:&amp;nbsp;done\n\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("Printing&amp;nbsp;composite:");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print(list4,&amp;nbsp;printcomp);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;:&amp;nbsp;done\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;0;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;insert(List&amp;nbsp;**p,&amp;nbsp;void&amp;nbsp;*data,&amp;nbsp;unsigned&amp;nbsp;int&amp;nbsp;n)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;nbsp;*temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&amp;nbsp;i;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&amp;nbsp;Error&amp;nbsp;check&amp;nbsp;is&amp;nbsp;ignored&amp;nbsp;*/&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp&amp;nbsp;=&amp;nbsp;malloc(sizeof(List));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;data&amp;nbsp;=&amp;nbsp;malloc(n);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&lt;&amp;nbsp;n;&amp;nbsp;i++)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*(char&amp;nbsp;*)(temp-&gt;data&amp;nbsp;+&amp;nbsp;i)&amp;nbsp;=&amp;nbsp;*(char&amp;nbsp;*)(data&amp;nbsp;+&amp;nbsp;i);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;next&amp;nbsp;=&amp;nbsp;*p;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*p&amp;nbsp;=&amp;nbsp;temp;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print(List&amp;nbsp;*p,&amp;nbsp;void&amp;nbsp;(*f)(void&amp;nbsp;*))&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(p)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(*f)(p-&gt;data);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p&amp;nbsp;=&amp;nbsp;p-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printstr(void&amp;nbsp;*str)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;\"%s\"",&amp;nbsp;(char&amp;nbsp;*)str);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printint(void&amp;nbsp;*n)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;%d",&amp;nbsp;*(int&amp;nbsp;*)n);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printchar(void&amp;nbsp;*c)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;%c",&amp;nbsp;*(char&amp;nbsp;*)c);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;printcomp(void&amp;nbsp;*comp)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;check&amp;nbsp;temp&amp;nbsp;=&amp;nbsp;*(struct&amp;nbsp;check&amp;nbsp;*)comp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("&amp;nbsp;'%d:%c:%f",&amp;nbsp;temp.i,&amp;nbsp;temp.c,&amp;nbsp;temp.d);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt; &lt;br /&gt;&lt;BR&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--END--&gt;&lt;br /&gt;&lt;/font&gt;&lt;BR&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-128119443443961030?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/128119443443961030/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=128119443443961030' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/128119443443961030'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/128119443443961030'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/generic-link-list.html' title='Generic Link List'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-230052514987333909</id><published>2007-05-20T01:06:00.000-07:00</published><updated>2007-05-20T01:07:59.467-07:00</updated><title type='text'>Declare a structure of a linked list</title><content type='html'>&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=90%%&gt;&lt;center&gt;&amp;nbsp;&lt;/center&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;font class=announceLink&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--THE QUESTION--&gt;&lt;br /&gt;&lt;br /&gt;How to declare a structure of a linked list? &lt;BR&gt;&lt;br /&gt;&lt;!--END--&gt;&lt;br /&gt;&lt;BR&gt;&lt;/font&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;font class=normal&gt;&lt;br /&gt;&lt;!--THE ANSWER--&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--META:START--&gt;&lt;!--interview, structure,linked list,pointer,C,program,typedef--&gt;&lt;!--META:END--&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;The right way of declaring a structure for a linked list in a C program is&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;node&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*next;&lt;BR&gt;&lt;br /&gt;};&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*mynode;&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Note that the following are &lt;font class=announcelink&gt;not&lt;/font&gt; correct&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;next;&lt;BR&gt;&lt;br /&gt;}&amp;nbsp;*mynode;&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;The typedef is not defined at the point where the "next" field is declared.&lt;/font&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;node&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;next;&lt;BR&gt;&lt;br /&gt;};&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;mynode;&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;You can only have pointer to structures, not the structure itself as its recursive!&lt;/font&gt; &lt;br /&gt;&lt;BR&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--END--&gt;&lt;br /&gt;&lt;/font&gt;&lt;BR&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-230052514987333909?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/230052514987333909/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=230052514987333909' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/230052514987333909'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/230052514987333909'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/declare-structure-of-linked-list.html' title='Declare a structure of a linked list'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-1681483751154146028</id><published>2007-05-20T00:56:00.000-07:00</published><updated>2007-05-20T01:04:13.746-07:00</updated><title type='text'>Sorting a linked list</title><content type='html'>&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=90%%&gt;&lt;center&gt;&amp;nbsp;&lt;/center&gt;&lt;/TD&gt;&lt;br /&gt;&lt;TD WIDTH=5%%&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;font class=announceLink&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--THE QUESTION--&gt;&lt;br /&gt;&lt;br /&gt;How do you sort a linked list? Write a C program to sort a linked list. &lt;BR&gt;&lt;br /&gt;&lt;!--END--&gt;&lt;br /&gt;&lt;BR&gt;&lt;/font&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;br /&gt;&lt;TD COLSPAN=3 width=700 align=left&gt;&lt;br /&gt;&lt;!--THE ANSWER--&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--META:START--&gt;&lt;!--interview,C,sort,linked list--&gt;&lt;!--META:END--&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;!--UPDATED--&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;This is a very popular interview question&lt;/font&gt;, which most people go wrong. The ideal solution to this problem is to keep the linked list sorted &lt;font class=announcelink&gt;as you build it&lt;/font&gt;. Another question on this website teaches you how to insert elements into a linked list in the sorted order. This really saves a lot of time which would have been required to sort it. &lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;However, you need to &lt;font class=announcelink&gt;Get That Job&lt;/font&gt;....&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;Method1 (Usual method)&lt;/font&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;The general idea is to decide upon a sorting algorithm (say bubble sort). Then, one needs to come up with different scenarios to swap two nodes in the linked list when they are not in the required order. The different scenarios would be something like&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;1.&amp;nbsp;When&amp;nbsp;the&amp;nbsp;nodes&amp;nbsp;being&amp;nbsp;compared&amp;nbsp;are&amp;nbsp;not&amp;nbsp;adjacent&amp;nbsp;and&amp;nbsp;one&amp;nbsp;of&amp;nbsp;them&amp;nbsp;is&amp;nbsp;the&amp;nbsp;first&amp;nbsp;node.&lt;BR&gt;&lt;br /&gt;2.&amp;nbsp;When&amp;nbsp;the&amp;nbsp;nodes&amp;nbsp;being&amp;nbsp;compared&amp;nbsp;are&amp;nbsp;not&amp;nbsp;adjacent&amp;nbsp;and&amp;nbsp;none&amp;nbsp;of&amp;nbsp;them&amp;nbsp;is&amp;nbsp;the&amp;nbsp;first&amp;nbsp;node&lt;BR&gt;&lt;br /&gt;3.&amp;nbsp;When&amp;nbsp;the&amp;nbsp;nodes&amp;nbsp;being&amp;nbsp;compared&amp;nbsp;are&amp;nbsp;adjacent&amp;nbsp;and&amp;nbsp;one&amp;nbsp;of&amp;nbsp;them&amp;nbsp;is&amp;nbsp;the&amp;nbsp;first&amp;nbsp;node.&lt;BR&gt;&lt;br /&gt;4.&amp;nbsp;When&amp;nbsp;the&amp;nbsp;nodes&amp;nbsp;being&amp;nbsp;compared&amp;nbsp;are&amp;nbsp;adjacent&amp;nbsp;and&amp;nbsp;none&amp;nbsp;of&amp;nbsp;them&amp;nbsp;is&amp;nbsp;the&amp;nbsp;first&amp;nbsp;node.&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;One example bubble sort for a linked list goes like this (working C code!)....&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;lt;stdio.h&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;lt;ctype.h&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;node&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*prev;&lt;BR&gt;&lt;br /&gt;}mynode&amp;nbsp;;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;add_node(struct&amp;nbsp;node&amp;nbsp;**head,&amp;nbsp;int&amp;nbsp;*count,&amp;nbsp;int&amp;nbsp;value);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print_list(char&amp;nbsp;*listName,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head);&lt;BR&gt;&lt;br /&gt;mynode&amp;nbsp;*bubbleSort(mynode&amp;nbsp;*head,&amp;nbsp;int&amp;nbsp;count);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;int&amp;nbsp;main()&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;mynode&amp;nbsp;*head;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;int&amp;nbsp;count&amp;nbsp;=&amp;nbsp;0;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;head&amp;nbsp;=&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;add_node(&amp;head,&amp;nbsp;&amp;count,&amp;nbsp;100);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;add_node(&amp;head,&amp;nbsp;&amp;count,&amp;nbsp;3);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;add_node(&amp;head,&amp;nbsp;&amp;count,&amp;nbsp;90);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;add_node(&amp;head,&amp;nbsp;&amp;count,&amp;nbsp;7);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;add_node(&amp;head,&amp;nbsp;&amp;count,&amp;nbsp;9);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;print_list("myList(BEFORE)",&amp;nbsp;head);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;head&amp;nbsp;=&amp;nbsp;bubbleSort(head,&amp;nbsp;count);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;print_list("myList(AFTER)&amp;nbsp;",&amp;nbsp;head);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;getch();&lt;BR&gt;&lt;br /&gt;&amp;nbsp;return(0);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;mynode&amp;nbsp;*bubbleSort(mynode&amp;nbsp;*head,&amp;nbsp;int&amp;nbsp;count)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;i,&amp;nbsp;j;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*p0,&amp;nbsp;*p1,&amp;nbsp;*p2,&amp;nbsp;*p3;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(i&amp;nbsp;=&amp;nbsp;1;&amp;nbsp;i&amp;nbsp;&lt;&amp;nbsp;count;&amp;nbsp;i++)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p0&amp;nbsp;=&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p1&amp;nbsp;=&amp;nbsp;head;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p2&amp;nbsp;=&amp;nbsp;head-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p3&amp;nbsp;=&amp;nbsp;p2-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(j&amp;nbsp;=&amp;nbsp;1;&amp;nbsp;j&amp;nbsp;&amp;lt;=&amp;nbsp;(count&amp;nbsp;-&amp;nbsp;i);&amp;nbsp;j++)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(p1-&gt;value&amp;nbsp;&gt;&amp;nbsp;p2-&gt;value)&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Adjust&amp;nbsp;the&amp;nbsp;pointers...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p1-&gt;next&amp;nbsp;=&amp;nbsp;p3;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p2-&gt;next&amp;nbsp;=&amp;nbsp;p1;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(p0)p0-&gt;next=p2;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Set&amp;nbsp;the&amp;nbsp;head&amp;nbsp;pointer&amp;nbsp;if&amp;nbsp;it&amp;nbsp;was&amp;nbsp;changed...&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(head&amp;nbsp;==&amp;nbsp;p1)head=p2;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Progress&amp;nbsp;the&amp;nbsp;pointers&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p0&amp;nbsp;=&amp;nbsp;p2;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p2&amp;nbsp;=&amp;nbsp;p1-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p3&amp;nbsp;=&amp;nbsp;p3-&gt;next!=(struct&amp;nbsp;node&amp;nbsp;*)NULL?p3-&gt;next:&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Nothing&amp;nbsp;to&amp;nbsp;swap,&amp;nbsp;just&amp;nbsp;progress&amp;nbsp;the&amp;nbsp;pointers...&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p0&amp;nbsp;=&amp;nbsp;p1;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p1&amp;nbsp;=&amp;nbsp;p2;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p2&amp;nbsp;=&amp;nbsp;p3;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p3&amp;nbsp;=&amp;nbsp;p3-&gt;next!=(struct&amp;nbsp;node&amp;nbsp;*)NULL?p3-&gt;next:&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;return(head);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;add_node(struct&amp;nbsp;node&amp;nbsp;**head,&amp;nbsp;int&amp;nbsp;*count,&amp;nbsp;int&amp;nbsp;value)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*temp,&amp;nbsp;*cur;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp&amp;nbsp;=&amp;nbsp;(mynode&amp;nbsp;*)malloc(sizeof(mynode));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp-&gt;next=NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp-&gt;prev=NULL;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if(*head&amp;nbsp;==&amp;nbsp;NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*head=temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;value=value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(cur=*head;cur-&gt;next!=NULL;cur=cur-&gt;next);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;cur-&gt;next=temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;prev=cur;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;value=value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;*count&amp;nbsp;=&amp;nbsp;*count&amp;nbsp;+&amp;nbsp;1;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print_list(char&amp;nbsp;*listName,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*temp;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;printf("\n[%s]&amp;nbsp;-&gt;&amp;nbsp;",&amp;nbsp;listName);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(temp=head;temp!=NULL;temp=temp-&gt;next)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("[%d]-&gt;",temp-&gt;value);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;printf("NULL\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;As you can see, the code becomes quite &lt;font class=announcelink&gt;messy&lt;/font&gt; because of the pointer logic. Thats why I have not elaborated too much on the code, nor on variations such as sorting a doubly linked list. You have to do it yourself once to understand it.&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;Method2 (Divide and Conquer using Merge Sort)&lt;/font&gt; &lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Here is some cool working C code...&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;lt;stdio.h&gt;&lt;BR&gt;&lt;br /&gt;#include&amp;lt;ctype.h&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;typedef&amp;nbsp;struct&amp;nbsp;node&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*prev;&lt;BR&gt;&lt;br /&gt;}mynode&amp;nbsp;;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;add_node(struct&amp;nbsp;node&amp;nbsp;**head,&amp;nbsp;int&amp;nbsp;value);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print_list(char&amp;nbsp;*listName,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;mergeSort(struct&amp;nbsp;node**&amp;nbsp;headRef);&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;node&amp;nbsp;*merge2SortedLLs(struct&amp;nbsp;node&amp;nbsp;*head1,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head2);&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;splitLLInto2(struct&amp;nbsp;node*&amp;nbsp;source,&amp;nbsp;struct&amp;nbsp;node**&amp;nbsp;frontRef,&amp;nbsp;struct&amp;nbsp;node**&amp;nbsp;backRef);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;The&amp;nbsp;main&amp;nbsp;function..&lt;BR&gt;&lt;br /&gt;int&amp;nbsp;main()&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*head;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;head&amp;nbsp;=&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;1);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;10);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;5);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;70);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;9);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;-99);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;add_node(&amp;head,&amp;nbsp;0);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print_list("myList",&amp;nbsp;head);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;mergeSort(&amp;head);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print_list("myList",&amp;nbsp;head);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;getch();&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return(0);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;This&amp;nbsp;is&amp;nbsp;a&amp;nbsp;recursive&amp;nbsp;mergeSort&amp;nbsp;function...&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;mergeSort(struct&amp;nbsp;node**&amp;nbsp;headRef)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;head&amp;nbsp;=&amp;nbsp;*headRef;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;b;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Base&amp;nbsp;case&amp;nbsp;--&amp;nbsp;length&amp;nbsp;0&amp;nbsp;or&amp;nbsp;1&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;((head&amp;nbsp;==&amp;nbsp;NULL)&amp;nbsp;||&amp;nbsp;(head-&gt;next&amp;nbsp;==&amp;nbsp;NULL))&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Split&amp;nbsp;head&amp;nbsp;into&amp;nbsp;'a'&amp;nbsp;and&amp;nbsp;'b'&amp;nbsp;sublists&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;splitLLInto2(head,&amp;nbsp;&amp;a,&amp;nbsp;&amp;b);&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Recursively&amp;nbsp;sort&amp;nbsp;the&amp;nbsp;sublists&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mergeSort(&amp;a);&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mergeSort(&amp;b);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Merge&amp;nbsp;the&amp;nbsp;two&amp;nbsp;sorted&amp;nbsp;lists&amp;nbsp;together&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;*headRef&amp;nbsp;=&amp;nbsp;merge2SortedLLs(a,&amp;nbsp;b);&amp;nbsp;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;This&amp;nbsp;is&amp;nbsp;an&amp;nbsp;iterative&amp;nbsp;function&amp;nbsp;that&amp;nbsp;joins&amp;nbsp;two&amp;nbsp;already&amp;nbsp;sorted&amp;nbsp;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;Linked&amp;nbsp;lists...&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;node&amp;nbsp;*merge2SortedLLs(struct&amp;nbsp;node&amp;nbsp;*head1,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head2)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*a,&amp;nbsp;*b,&amp;nbsp;*c,&amp;nbsp;*newHead,&amp;nbsp;*temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;a&amp;nbsp;=&amp;nbsp;head1;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;b&amp;nbsp;=&amp;nbsp;head2;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;(struct&amp;nbsp;node&amp;nbsp;*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;newHead&amp;nbsp;=&amp;nbsp;(struct&amp;nbsp;node*)NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(a==NULL)return(b);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if(b==NULL)return(a);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;while(a!=NULL&amp;nbsp;&amp;&amp;&amp;nbsp;b!=NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(a-&gt;value&amp;nbsp;&lt;&amp;nbsp;b-&gt;value)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//printf("\na-&gt;value&amp;nbsp;&lt;&amp;nbsp;b-&gt;value\n");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(c==NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c-&gt;next&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;=&amp;nbsp;c-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a&amp;nbsp;&amp;nbsp;=&amp;nbsp;a-&gt;next;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if(a-&gt;value&amp;nbsp;&gt;&amp;nbsp;b-&gt;value)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//printf("\na-&gt;value&amp;nbsp;&gt;&amp;nbsp;b-&gt;value\n");&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(c==NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;&amp;nbsp;=&amp;nbsp;b;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c-&gt;next&amp;nbsp;=&amp;nbsp;b;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;=&amp;nbsp;c-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b&amp;nbsp;&amp;nbsp;=&amp;nbsp;b-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Both&amp;nbsp;are&amp;nbsp;equal.&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Arbitraritly&amp;nbsp;chose&amp;nbsp;to&amp;nbsp;add&amp;nbsp;one&amp;nbsp;of&amp;nbsp;them&amp;nbsp;and&amp;nbsp;make&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;sure&amp;nbsp;you&amp;nbsp;skip&amp;nbsp;both!&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(c&amp;nbsp;==&amp;nbsp;NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c-&gt;next&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;=&amp;nbsp;c-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a&amp;nbsp;&amp;nbsp;=&amp;nbsp;a-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b&amp;nbsp;&amp;nbsp;=&amp;nbsp;b-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Make&amp;nbsp;sure&amp;nbsp;the&amp;nbsp;new&amp;nbsp;head&amp;nbsp;is&amp;nbsp;set...&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(newHead&amp;nbsp;==&amp;nbsp;NULL)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;newHead&amp;nbsp;=&amp;nbsp;c;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(a==NULL&amp;nbsp;&amp;&amp;&amp;nbsp;b==NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return(newHead);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(a==NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c-&gt;next&amp;nbsp;=&amp;nbsp;b;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if(b==NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c-&gt;next&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;return(newHead);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;Uses&amp;nbsp;the&amp;nbsp;fast/slow&amp;nbsp;pointer&amp;nbsp;strategy&lt;BR&gt;&lt;br /&gt;//&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;This&amp;nbsp;efficient&amp;nbsp;code&amp;nbsp;splits&amp;nbsp;a&amp;nbsp;linked&amp;nbsp;list&amp;nbsp;into&amp;nbsp;two&amp;nbsp;using&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;the&amp;nbsp;same&amp;nbsp;technique&amp;nbsp;as&amp;nbsp;the&amp;nbsp;one&amp;nbsp;used&amp;nbsp;to&amp;nbsp;find&amp;nbsp;the&amp;nbsp;&lt;BR&gt;&lt;br /&gt;//&amp;nbsp;middle&amp;nbsp;of&amp;nbsp;a&amp;nbsp;linked&amp;nbsp;list!&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;splitLLInto2(struct&amp;nbsp;node*&amp;nbsp;source,&amp;nbsp;struct&amp;nbsp;node**&amp;nbsp;frontRef,&amp;nbsp;struct&amp;nbsp;node**&amp;nbsp;backRef)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;fast;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;slow;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(source==NULL&amp;nbsp;||&amp;nbsp;source-&gt;next==NULL)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;length&amp;nbsp;&lt;&amp;nbsp;2&amp;nbsp;cases&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*frontRef&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*backRef&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;slow&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fast&amp;nbsp;=&amp;nbsp;source-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Advance&amp;nbsp;'fast'&amp;nbsp;two&amp;nbsp;nodes,&amp;nbsp;and&amp;nbsp;advance&amp;nbsp;'slow'&amp;nbsp;one&amp;nbsp;node&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(fast&amp;nbsp;!=&amp;nbsp;NULL)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fast&amp;nbsp;=&amp;nbsp;fast-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(fast&amp;nbsp;!=&amp;nbsp;NULL)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;slow&amp;nbsp;=&amp;nbsp;slow-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fast&amp;nbsp;=&amp;nbsp;fast-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;'slow'&amp;nbsp;is&amp;nbsp;before&amp;nbsp;the&amp;nbsp;midpoint&amp;nbsp;in&amp;nbsp;the&amp;nbsp;list,&amp;nbsp;so&amp;nbsp;split&amp;nbsp;it&amp;nbsp;in&amp;nbsp;two&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;at&amp;nbsp;that&amp;nbsp;point.&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*frontRef&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*backRef&amp;nbsp;=&amp;nbsp;slow-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;slow-&gt;next&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;add_node(struct&amp;nbsp;node&amp;nbsp;**head,&amp;nbsp;int&amp;nbsp;value)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*temp,&amp;nbsp;*cur;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp&amp;nbsp;=&amp;nbsp;(mynode&amp;nbsp;*)malloc(sizeof(mynode));&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp-&gt;next=NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;temp-&gt;prev=NULL;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if(*head&amp;nbsp;==&amp;nbsp;NULL)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*head=temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;value=value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(cur=*head;cur-&gt;next!=NULL;cur=cur-&gt;next);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;cur-&gt;next=temp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;prev=cur;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp-&gt;value=value;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;print_list(char&amp;nbsp;*listName,&amp;nbsp;struct&amp;nbsp;node&amp;nbsp;*head)&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;mynode&amp;nbsp;*temp;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;printf("\n[%s]&amp;nbsp;-&gt;&amp;nbsp;",&amp;nbsp;listName);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(temp=head;temp!=NULL;temp=temp-&gt;next)&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("[%d]-&gt;",temp-&gt;value);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;printf("NULL\n");&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt; &lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;The code to merge &lt;font class=announcelink&gt;two already sorted&lt;/font&gt; sub-linked lists into a sorted linked list could be either &lt;font class=announcelink&gt;iterative or recursive&lt;/font&gt;. You already saw the iterative version above. Here is a &lt;font class=announcelink&gt;recursive&lt;/font&gt; version of the same...&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;font class=announcelink&gt;Recursive solution to merge two already sorted linked lists into a single linked list&lt;/font&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;struct&amp;nbsp;node*&amp;nbsp;sortedMergeRecursive(struct&amp;nbsp;node*&amp;nbsp;a,&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;b)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;result&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(a==NULL)&amp;nbsp;return(b);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(b==NULL)&amp;nbsp;return(a);&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Pick&amp;nbsp;either&amp;nbsp;a&amp;nbsp;or&amp;nbsp;b,&amp;nbsp;and&amp;nbsp;recur&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(a-&gt;data&amp;nbsp;&amp;lt;=&amp;nbsp;b-&gt;data)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result&amp;nbsp;=&amp;nbsp;a;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result-&gt;next&amp;nbsp;=&amp;nbsp;sortedMergeRecursive(a-&gt;next,&amp;nbsp;b);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result&amp;nbsp;=&amp;nbsp;b;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result-&gt;next&amp;nbsp;=&amp;nbsp;sortedMergeRecursive(a,&amp;nbsp;b-&gt;next);&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;return(result);&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Also, see how the &lt;font class=announcelink&gt;splitLLInto2()&lt;/font&gt; function uses the same technique used to find the middle of a linked list to split a linked list into two &lt;font class=announcelink&gt;without&lt;/font&gt; having to keep a count of the number of nodes in the linkes list!&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Here is another solution (not that great, though) to split a linked list into two. It used the count of the number of nodes to decide where to split&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;CODE&gt;&lt;BR&gt;&lt;br /&gt;void&amp;nbsp;splitLLInto2(struct&amp;nbsp;node*&amp;nbsp;source,struct&amp;nbsp;node**&amp;nbsp;frontRef,&amp;nbsp;struct&amp;nbsp;node**&amp;nbsp;backRef)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;len&amp;nbsp;=&amp;nbsp;Length(source);&amp;nbsp;//Get&amp;nbsp;the&amp;nbsp;length&amp;nbsp;of&amp;nbsp;the&amp;nbsp;original&amp;nbsp;LL..&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int&amp;nbsp;i;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct&amp;nbsp;node*&amp;nbsp;current&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(len&amp;nbsp;&lt;&amp;nbsp;2)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*frontRef&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*backRef&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;else&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&amp;nbsp;hopCount&amp;nbsp;=&amp;nbsp;(len-1)/2;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;lt;hopCount;&amp;nbsp;i++)&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;current&amp;nbsp;=&amp;nbsp;current-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Now&amp;nbsp;cut&amp;nbsp;at&amp;nbsp;current&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*frontRef&amp;nbsp;=&amp;nbsp;source;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*backRef&amp;nbsp;=&amp;nbsp;current-&gt;next;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;current-&gt;next&amp;nbsp;=&amp;nbsp;NULL;&lt;BR&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;br /&gt;}&lt;BR&gt;&lt;br /&gt;&lt;/CODE&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;&lt;BR&gt;&lt;br /&gt;Using recursive stack space proportional to the length of a list is not recommended. However, the recursion in this case is ok ? it uses stack space which is proportional to the log of the length of the list. For a 1000 node list, the recursion will only go about 10 deep. For a 2000 node list, it will go 11 deep. If you think about it, you can see that doubling the size of the list only increases the depth by 1.&lt;BR&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-1681483751154146028?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/1681483751154146028/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=1681483751154146028' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1681483751154146028'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/1681483751154146028'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/sorting-linked-list.html' title='Sorting a linked list'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-6560155802908473299</id><published>2007-05-18T11:16:00.000-07:00</published><updated>2007-05-18T11:17:55.849-07:00</updated><title type='text'>Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?</title><content type='html'>This is a very good interview question&lt;br /&gt;The solution to this is to copy the data from the next node into this node and delete the next node!. Ofcourse this wont work if the node to be deleted is the last node. Mark it as dummy in that case. If you have a Circular linked list, then this might be all the more interesting. Try writing your own C program to solve this problem. Having a doubly linked list is always better.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-6560155802908473299?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vijayinterviewquestions.blogspot.com/feeds/6560155802908473299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7662544687317968079&amp;postID=6560155802908473299' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6560155802908473299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/6560155802908473299'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/given-only-pointer-to-node-to-be.html' title='Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7662544687317968079.post-892409905745454069</id><published>2007-05-18T08:13:00.000-07:00</published><updated>2007-05-18T11:13:24.879-07:00</updated><title type='text'>How do you reverse a singly linked list? How do you reverse a doubly linked list? Write a C program to do the same.</title><content type='html'>&lt;p&gt;This is THE most frequently asked interview question. The most!.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Singly linked lists&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Here are a few C programs to reverse a singly linked list.&lt;br /&gt;&lt;br /&gt;Method1 (Iterative)&lt;/p&gt;&lt;p&gt;&lt;br /&gt;#include  "stdio.h"&lt;stdio.h&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;// Variables&lt;/p&gt;&lt;p&gt;typedef struct node { &lt;/p&gt;&lt;p&gt;        int value; &lt;/p&gt;&lt;p&gt;        struct node *next; &lt;/p&gt;&lt;p&gt;}mynode;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;// Globals (not required, though).&lt;/p&gt;&lt;p&gt;mynode *head, *tail, *temp;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;// Functions&lt;/p&gt;&lt;p&gt;void add(int value);void iterative_reverse();void print_list();&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;// The main() function&lt;/p&gt;&lt;p&gt;int main()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         head=(mynode *)0; // Construct the linked list. &lt;/p&gt;&lt;p&gt;         add(1); &lt;/p&gt;&lt;p&gt;         add(2); &lt;/p&gt;&lt;p&gt;         add(3); &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;        //Print it &lt;/p&gt;&lt;p&gt;        print_list(); &lt;/p&gt;&lt;br /&gt;&lt;p&gt;        // Reverse it. &lt;/p&gt;&lt;p&gt;        iterative_reverse();&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;        //Print it again &lt;/p&gt;&lt;p&gt;        print_list(); &lt;/p&gt;&lt;br /&gt;&lt;p&gt;        return(0);&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// The reverse function&lt;/p&gt;&lt;p&gt;void iterative_reverse()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;        mynode *p, *q, *r; &lt;/p&gt;&lt;p&gt;        if(head == (mynode *)0) &lt;/p&gt;&lt;p&gt;        { &lt;/p&gt;&lt;p&gt;                return;&lt;/p&gt;&lt;p&gt;        }&lt;br /&gt;        p = head; q = p-&gt;next; &lt;/p&gt;&lt;p&gt;        p-&gt;next = (mynode *)0;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;       while (q != (mynode *)0) &lt;/p&gt;&lt;p&gt;       { &lt;/p&gt;&lt;p&gt;              r = q-&gt;next; &lt;/p&gt;&lt;p&gt;             q-&gt;next = p;&lt;/p&gt;&lt;p&gt;             p = q; q = r; &lt;/p&gt;&lt;p&gt;             } &lt;/p&gt;&lt;p&gt;             head = p;&lt;/p&gt;&lt;p&gt;} &lt;/p&gt;&lt;p&gt;&lt;br /&gt;// Function to add new nodes to the linked list&lt;/p&gt;&lt;p&gt;void add(int value)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;          temp = (mynode *) malloc(sizeof(struct node)); &lt;/p&gt;&lt;p&gt;          temp-&gt;next=(mynode *)0; &lt;/p&gt;&lt;p&gt;          temp-&gt;value=value; &lt;/p&gt;&lt;p&gt;          if(head==(mynode *)0) &lt;/p&gt;&lt;p&gt;          {&lt;/p&gt;&lt;p&gt;                 head=temp; &lt;/p&gt;&lt;p&gt;                 tail=temp; &lt;/p&gt;&lt;p&gt;           } &lt;/p&gt;&lt;p&gt;          else &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;           tail-&gt;next=temp; &lt;/p&gt;&lt;p&gt;           tail=temp; &lt;/p&gt;&lt;p&gt;           }&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Function to print the linked list.&lt;/p&gt;&lt;p&gt;void print_list()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;          printf("\n\n"); &lt;/p&gt;&lt;p&gt;          for(temp=head; temp!=(mynode *)0; temp=temp-&gt;next) &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;                 printf("[%d]-&gt;",(temp-&gt;value));&lt;/p&gt;&lt;p&gt;          } &lt;/p&gt;&lt;p&gt;          printf("[NULL]\n\n");&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Method2 (Recursive, without using any temporary variable)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;#include "stdio.h"&lt;stdio.h&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Variable&lt;/p&gt;&lt;p&gt;stypedef struct node &lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         int value; &lt;/p&gt;&lt;p&gt;         struct node *next; &lt;/p&gt;&lt;p&gt;} mynode;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Globals.&lt;/p&gt;&lt;p&gt;mynode *head, *tail, *temp;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Functions&lt;/p&gt;&lt;p&gt;void add(int value);&lt;/p&gt;&lt;p&gt;mynode* reverse_recurse(mynode *root);&lt;/p&gt;&lt;p&gt;void print_list();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// The main() function&lt;/p&gt;&lt;p&gt;int main()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         head=(mynode *)0; &lt;/p&gt;&lt;p&gt;         // Construct the linked list. &lt;/p&gt;&lt;p&gt;         add(1); &lt;/p&gt;&lt;p&gt;         add(2); &lt;/p&gt;&lt;p&gt;         add(3); &lt;/p&gt;&lt;p&gt;        //Print it &lt;/p&gt;&lt;p&gt;        print_list();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;         // Reverse it. &lt;/p&gt;&lt;p&gt;        if(head != (mynode *)0) &lt;/p&gt;&lt;p&gt;        { &lt;/p&gt;&lt;p&gt;               temp = reverse_recurse(head); &lt;/p&gt;&lt;p&gt;               temp-&gt;next = (mynode *)0; &lt;/p&gt;&lt;p&gt;         } &lt;/p&gt;&lt;p&gt;        //Print it again &lt;/p&gt;&lt;p&gt;        print_list(); &lt;/p&gt;&lt;p&gt;        return(0);&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Reverse the linked list recursively//&lt;br /&gt;// This function uses the power of the stack to make this// *magical* assignment//// node-&gt;next-&gt;next=node; //// :)&lt;br /&gt;mynode* reverse_recurse(mynode *root)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         if(root-&gt;next!=(mynode *)0) &lt;/p&gt;&lt;p&gt;         { &lt;/p&gt;&lt;p&gt;               reverse_recurse(root-&gt;next); &lt;/p&gt;&lt;p&gt;               root-&gt;next-&gt;next=root; &lt;/p&gt;&lt;p&gt;               return(root); &lt;/p&gt;&lt;p&gt;          } &lt;/p&gt;&lt;p&gt;          else &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;              head=root; &lt;/p&gt;&lt;p&gt;          } &lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;// Function to add new nodes to the linked list.&lt;/p&gt;&lt;p&gt;void add(int value)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;           temp = (mynode *) malloc(sizeof(struct node)); &lt;/p&gt;&lt;p&gt;           temp-&gt;next=(mynode *)0; &lt;/p&gt;&lt;p&gt;           temp-&gt;value=value; &lt;/p&gt;&lt;p&gt;           if(head==(mynode *)0) &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;                 head=temp; &lt;/p&gt;&lt;p&gt;                 tail=temp; &lt;/p&gt;&lt;p&gt;           } &lt;/p&gt;&lt;p&gt;           else &lt;/p&gt;&lt;p&gt;           { &lt;/p&gt;&lt;p&gt;                 tail-&gt;next=temp; &lt;/p&gt;&lt;p&gt;                 tail=temp; &lt;/p&gt;&lt;p&gt;            }&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Function to print the linked list.&lt;/p&gt;&lt;p&gt;void print_list()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;          printf("\n\n"); &lt;/p&gt;&lt;p&gt;          for(temp=head; temp!=(mynode *)0; temp=temp-&gt;next) &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;                 printf("[%d]-&gt;",(temp-&gt;value)); &lt;/p&gt;&lt;p&gt;           } printf("[NULL]\n\n");&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3 (Recursive, but without ANY global variables. Slightly messy!)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;#include "stdio.h"&lt;stdio.h&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Variablestypedef &lt;/p&gt;&lt;p&gt;struct node &lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;           int value; &lt;/p&gt;&lt;p&gt;           struct node *next; &lt;/p&gt;&lt;p&gt;}mynode;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Functions&lt;/p&gt;&lt;p&gt;void add(mynode **head, mynode **tail, int value);&lt;/p&gt;&lt;p&gt;mynode* reverse_recurse(mynode *current, mynode *next);&lt;/p&gt;&lt;p&gt;void print_list(mynode *);&lt;br /&gt;&lt;/p&gt;&lt;p&gt;int main()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;          mynode *head, *tail;&lt;/p&gt;&lt;p&gt;          head=(mynode *)0; // Construct the linked list. &lt;/p&gt;&lt;p&gt;          add(&amp;head, &amp;amp;amp;tail, 1); &lt;/p&gt;&lt;p&gt;          add(&amp;head, &amp;amp;tail, 2); &lt;/p&gt;&lt;p&gt;          add(&amp;head, &amp;amp;tail, 3); &lt;/p&gt;&lt;p&gt;          //Print it &lt;/p&gt;&lt;p&gt;          print_list(head);&lt;br /&gt;&lt;/p&gt;&lt;p&gt;          // Reverse it. &lt;/p&gt;&lt;p&gt;          head = reverse_recurse(head, (mynode *)0); &lt;/p&gt;&lt;p&gt;          //Print it again &lt;/p&gt;&lt;p&gt;          print_list(head); &lt;/p&gt;&lt;p&gt;          getch(); &lt;/p&gt;&lt;p&gt;          return(0);&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Reverse the linked list recursively&lt;/p&gt;&lt;p&gt;mynode* reverse_recurse(mynode *current, mynode *next)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;            mynode *ret; &lt;/p&gt;&lt;p&gt;            if(current==(mynode *)0) &lt;/p&gt;&lt;p&gt;            { &lt;/p&gt;&lt;p&gt;                  return((mynode *)0);&lt;/p&gt;&lt;p&gt;            } &lt;/p&gt;&lt;p&gt;            ret = (mynode *)0; &lt;/p&gt;&lt;p&gt;            if (current-&gt;next != (mynode *)0) &lt;/p&gt;&lt;p&gt;           { &lt;/p&gt;&lt;p&gt;                 ret = reverse_recurse(current-&gt;next, current); &lt;/p&gt;&lt;p&gt;           } &lt;/p&gt;&lt;p&gt;          else &lt;/p&gt;&lt;p&gt;          { &lt;/p&gt;&lt;p&gt;                ret = current; &lt;/p&gt;&lt;p&gt;          } &lt;/p&gt;&lt;p&gt;          current-&gt;next = next; &lt;/p&gt;&lt;p&gt;          return ret;&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Function to add new nodes to the linked list.&lt;/p&gt;&lt;p&gt;// Takes pointers to pointers to maintain the // *actual* head and tail pointers (which are local to main()). &lt;/p&gt;&lt;p&gt;&lt;br /&gt;void add(mynode **head, mynode **tail, int value)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;        mynode *temp1, *temp2; &lt;/p&gt;&lt;p&gt;        temp1 = (mynode *) malloc(sizeof(struct node)); &lt;/p&gt;&lt;p&gt;        temp1-&gt;next=(mynode *)0; &lt;/p&gt;&lt;p&gt;        temp1-&gt;value=value; &lt;/p&gt;&lt;p&gt;        if(*head==(mynode *)0) &lt;/p&gt;&lt;p&gt;        {&lt;/p&gt;&lt;p&gt;              *head=temp1; &lt;/p&gt;&lt;p&gt;              *tail=temp1; &lt;/p&gt;&lt;p&gt;         } &lt;/p&gt;&lt;p&gt;        else &lt;/p&gt;&lt;p&gt;        { &lt;/p&gt;&lt;p&gt;              for(temp2 = *head; temp2-&gt;next!= (mynode *)0; temp2=temp2-&gt;next);&lt;/p&gt;&lt;p&gt;              temp2-&gt;next = temp1; *tail=temp1; &lt;/p&gt;&lt;p&gt;        }&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;// Function to print the linked list.&lt;/p&gt;&lt;p&gt;void print_list(mynode *head)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;          mynode *temp; &lt;/p&gt;&lt;p&gt;          printf("\n\n"); &lt;/p&gt;&lt;p&gt;          for(temp=head; temp!=(mynode *)0; temp=temp-&gt;next) &lt;/p&gt;&lt;p&gt;         { &lt;/p&gt;&lt;p&gt;               printf("[%d]-&gt;",(temp-&gt;value)); &lt;/p&gt;&lt;p&gt;         } &lt;/p&gt;&lt;p&gt;         printf("[NULL]\n\n");&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Doubly linked lists&lt;br /&gt;&lt;/p&gt;&lt;p&gt;This is really easy, just keep swapping the prev and next pointers and at the end swap the head and the tail:) &lt;/p&gt;&lt;p&gt;&lt;br /&gt;#include "stdio.h"&lt;/p&gt;&lt;p&gt;&lt;stdio.h&gt;#include&lt;ctype.h&gt; "ctype.h"&lt;/p&gt;&lt;p&gt;&lt;br /&gt;typedef struct node&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         int value; &lt;/p&gt;&lt;p&gt;         struct node *next; &lt;/p&gt;&lt;p&gt;         struct node *prev;&lt;/p&gt;&lt;p&gt;} mynode ;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;mynode *head, *tail;&lt;/p&gt;&lt;p&gt;void add_node(int value);&lt;/p&gt;&lt;p&gt;void print_list();&lt;/p&gt;&lt;p&gt;void reverse();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;int main()&lt;/p&gt;&lt;p&gt;{&lt;br /&gt;&lt;br /&gt;         head=NULL; &lt;/p&gt;&lt;p&gt;         tail=NULL;&lt;br /&gt;         &lt;/p&gt;&lt;p&gt;         add_node(1); &lt;/p&gt;&lt;p&gt;         add_node(2); &lt;/p&gt;&lt;p&gt;         add_node(3); &lt;/p&gt;&lt;p&gt;         add_node(4); &lt;/p&gt;&lt;p&gt;         add_node(5); &lt;/p&gt;&lt;p&gt;&lt;br /&gt;         print_list();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;         reverse();&lt;br /&gt;         &lt;/p&gt;&lt;p&gt;         print_list();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;         return(1);&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;void add_node(int value)&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;        mynode *temp, *cur; &lt;/p&gt;&lt;p&gt;        temp = (mynode *)malloc(sizeof(mynode)); &lt;/p&gt;&lt;p&gt;        temp-&gt;next=NULL; &lt;/p&gt;&lt;p&gt;        temp-&gt;prev=NULL;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;        if(head == NULL) &lt;/p&gt;&lt;p&gt;        {&lt;br /&gt;         printf("\nAdding a head pointer\n"); &lt;/p&gt;&lt;p&gt;         head=temp; &lt;/p&gt;&lt;p&gt;         tail=temp; &lt;/p&gt;&lt;p&gt;         temp-&gt;value=value;&lt;br /&gt;        } &lt;/p&gt;&lt;p&gt;        else &lt;/p&gt;&lt;p&gt;       {&lt;br /&gt;&lt;br /&gt;            for(cur=head;cur-&gt;next!=NULL;cur=cur-&gt;next); &lt;/p&gt;&lt;p&gt;            cur-&gt;next=temp; &lt;/p&gt;&lt;p&gt;            temp-&gt;prev=cur; &lt;/p&gt;&lt;p&gt;            temp-&gt;value=value; &lt;/p&gt;&lt;p&gt;            tail=temp;&lt;br /&gt;       }&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;void print_list()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;       mynode *temp;&lt;br /&gt;       printf("\n--------------------------------\n"); &lt;/p&gt;&lt;p&gt;       for(temp=head;temp!=NULL;temp=temp-&gt;next) &lt;/p&gt;&lt;p&gt;       { &lt;/p&gt;&lt;p&gt;              printf("\n[%d]\n",temp-&gt;value); &lt;/p&gt;&lt;p&gt;       }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;void reverse()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;         mynode *cur, *temp, *save_next; &lt;/p&gt;&lt;p&gt;         if(head==tail)return; &lt;/p&gt;&lt;p&gt;         if(head==NULL  tail==NULL)return; &lt;/p&gt;&lt;p&gt;         for(cur=head;cur!=NULL;) &lt;/p&gt;&lt;p&gt;         { &lt;/p&gt;&lt;p&gt;                printf("\ncur-&gt;value : [%d]\n",cur-&gt;value); &lt;/p&gt;&lt;p&gt;                temp=cur-&gt;next; &lt;/p&gt;&lt;p&gt;                save_next=cur-&gt;next; &lt;/p&gt;&lt;p&gt;                cur-&gt;next=cur-&gt;prev; &lt;/p&gt;&lt;p&gt;                cur-&gt;prev=temp; &lt;/p&gt;&lt;p&gt;                cur=save_next; &lt;/p&gt;&lt;p&gt;         }&lt;br /&gt;         temp=head; &lt;/p&gt;&lt;p&gt;         head=tail; &lt;/p&gt;&lt;p&gt;         tail=temp;&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;Having shown all these different methods, if someone can mail me a really, really good practical application of reversing a linked list (singly or doubly linked list), I would be really thankful to them. I have not found one good application of this. All I see is an urge to understand how well the candidate handles the pointer manipulation&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7662544687317968079-892409905745454069?l=vijayinterviewquestions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/892409905745454069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7662544687317968079/posts/default/892409905745454069'/><link rel='alternate' type='text/html' href='http://vijayinterviewquestions.blogspot.com/2007/05/how-do-you-reverse-singly-linked-list.html' title='How do you reverse a singly linked list? How do you reverse a doubly linked list? Write a C program to do the same.'/><author><name>Vijay Agrawal</name><uri>http://www.blogger.com/profile/16856170291286959451</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
