Sunday, December 13, 2015

Check Palindrome

#include<stdio.h>
#include<string.h>

int check_palindrome( char s[])
{
char reverse[100], temp ;
int i, j ;
strcpy(reverse, s);

printf("\n  string = %s", reverse) ;

for ( i = 0, j = strlen(reverse) -1; i<j; i++, j--)
{

temp = reverse[i];
reverse[i] = reverse[j];
reverse[j] = temp;
}

printf("\n Reverse string = %s", reverse) ;

if( !strcmp(s, reverse) )
return 1;
else
return 0;

}


int main()
{
char str[100], temp, reverse[100];
int i, j = 0, res =0;

printf("\n Enter string \n");
gets(str);

res = check_palindrome(str);
printf("\n res = %d", res);


return 0;
}

Saturday, December 12, 2015

Linked List

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct test_struct
{
int val;
struct test_struct * next ;
};

struct test_struct * head = NULL;
struct test_struct * curr = NULL;


struct test_struct * create_list( int val )
{
struct test_struct * ptr = (struct test_struct*) malloc( sizeof (struct test_struct ));

if ( NULL == ptr )
        {
printf("\n Node creation failed ");
return NULL;
        }
ptr -> val = val;
ptr -> next = NULL;

head = curr = ptr;
return ptr;
}

struct test_struct * add_list(int val, bool add_to_end )
{
if ( NULL == head )
{
head = create_list(val);
return head;
}

struct test_struct * ptr = ( struct test_struct *)malloc( sizeof(struct test_struct ));
 
if ( ptr == NULL )
{
printf("\n  node  failed ");
return NULL;
}


ptr -> val = val;
ptr -> next = NULL;

if( add_to_end )
{
curr -> next = ptr;
curr = ptr;
}
else
{
ptr -> next = head;
head = ptr;
}
return ptr;
}

void print_list ()
{
struct test_struct * ptr = head;
        printf("\n\n Printing the list ");
while ( ptr != NULL )
{
printf("\n  %d ", ptr->val);
ptr = ptr-> next;
}
}

int main()
{
int i=0, ret = 0;
struct test_struct * ptr = NULL;

//print_list();
printf("\n Adding to list ");
  for ( i = 10; i< 20; i++)
add_list(i, true);
 
print_list();

printf("\n Adding to list ");
  for ( i = 0 ; i< 30; i++)
add_list(i, false );

print_list();
}

Binary Tree

#include<stdio.h>
#include<stdlib.h>


struct btree
{
int data;
struct btree * right;
struct btree * left;
};


struct btree * head = NULL;


struct btree * getNode(int data)
{
struct btree * temp =NULL;
temp = (struct btree*)malloc( sizeof(struct btree) );
temp -> left = NULL;
temp -> right = NULL;
        temp -> data = data ;
printf("\n getNode :: temp -> data = %d", temp -> data );
return temp;

}

struct btree * insert(struct btree * root, int data)
{

if ( root == NULL )
{
head = getNode(data);
return  head ;
}
else
{
printf("\n insert:: root is not null");

if ( data <= ( (root) -> data ) )
{
printf("\n  LESS THAN ");
root -> left = insert(  ( (root) -> left ), data ) ;
}
if ( data > ( (root) -> data ) )
{
printf("\n  MORE THAN ");
root -> right =  insert( ( (root) ->right ) , data  ) ;
}
return root ;
}
}


void preorder( struct btree * r  )
{
if ( r != NULL  )
{
printf("\t  %d ", r-> data );
preorder( r -> left );
preorder( r -> right );
}

}


void postorder( struct btree * root )
{
if ( root != NULL )
{
postorder(root -> left) ;
postorder(root -> right);
printf("\t %d", root -> data );
}

}

void inorder(struct btree * root)
{
if (  root != NULL )
{
postorder( root -> left );
printf("\t  %d", root -> data );
postorder( root -> right ) ;
}
}

int main()
{
struct btree * root = NULL;
printf("\n In main ");

printf("\n  main: Adding nodes ");
root = insert(head, 9) ;

root = insert(root, 4);
root = insert(root, 15);
root = insert(root, 6);
root = insert(root, 12);
root = insert(root, 17);
root = insert(root, 2);

printf("\n main:: nodes added ");

printf("\n main:: root -> data  = %d", root  -> data);
printf("\n main:: Pre order display");
printf("\n main:: head -> data  = %d", head -> data);
printf("\n ");
preorder( root );

printf("\n");

printf("\n Inorder display");
inorder( root );


printf("\n");
printf("\n post order display");
postorder( root );


return 0;
}