Friday, September 28, 2012

Sort a linked list. Write a C program.



//sorting in descending order
struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
{
node* first,second,temp;
first= Head;
while(first!=null)
{
second=first->NEXT;
while(second!=null)
{
if(first->value < second->value)
{
temp = new node();
temp->value=first->value;
first->value=second->value;
second->value=temp->value;
delete temp;
}
second=second->NEXT;
}

first=first->NEXT;
}
}

Write a C program to find the depth or height of a tree.


   
tree_height(mynode *p) {
   if(p==NULL)return(0);
   if(p->left){h1=tree_height(p->left);}
   if(p=>right){h2=tree_height(p->right);}
   return(max(h1,h2)+1); }



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 > 0, has at least h and at most (2^h -1) elements in it. The height of a binary tree that contains n, n>0, elements is at most n and atleast log(n+1) to the base 2.

Log(n+1) to the base 2 = h

n = (2^h - 1) 

How do you reverse a linked list without using any C pointers?

 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).

How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.


typedef struct node 
void *data; 
struct node *next;
}mynode; 

mynode * find_loop(NODE * head)
mynode *current = head; 
while(current->next != NULL) 
mynode *temp = head; 
while(temp->next != NULL && temp != current)
if(current->next == temp) 
printf("\nFound a loop."); 
return current; 
temp = temp->next;
current = current->next;
return NULL; 
}

How to compare two linked lists? Write a C program to compare two linked lists.

int compare_linked_lists(struct node *q, struct node *r) {
    static int flag;
    
    if((q==NULL ) && (r==NULL))
    {
         flag=1;
    }
    else
    {
        if(q==NULL || r==NULL)
        {
            flag=0;
        }
        if(q->data!=r->data)
        {
            flag=0;
        }
        else
        {
           compare_linked_lists(q->link,r->link);
        }
    }
    return(flag); }

How to create a copy of a linked list? Write a C program to create a copy of a linked list.

copy_linked_lists(struct node *q, struct node **s) {
    if(q!=NULL)
    {
        *s=malloc(sizeof(struct node));
        (*s)->data=q->data;
        (*s)->link=NULL;
        copy_linked_list(q->link, &((*s)->link));
    } }

Ref: http://technical-interview.com

Friday, May 11, 2012

Directory and sub directory file searching using c language



#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include .h>
/* limits.h defines "PATH_MAX". */
#include .h>
void listdir (const char *path){
    DIR *pdir = NULL;
    pdir = opendir (path);
    struct dirent *pent = NULL;
    if (pdir == NULL) {
        return; // exit the function
    } // end if
    while (pent = readdir (pdir)) {
        if (pent == NULL) {
            return;
        }
        //printf ("%s\n", pent->d_name);
        if (strcmp (pent->d_name, "..") != 0 &&
                strcmp (pent->d_name, ".") != 0) {
                int path_length;
                char newpath[PATH_MAX];
                path_length = snprintf (newpath, PATH_MAX, "%s/%s", path, pent->d_name);
                printf ("%s\n", newpath);
             
                if (path_length >= PATH_MAX) {
                    fprintf (stderr, "Path length has got too long.\n");
                    exit (EXIT_FAILURE);
                }
                listdir(newpath);
        }
    }
    closedir (pdir);
}
int main(int argc, char *argv[])
{
  listdir ("EMS");
  system("PAUSE");  
  return 0;
}