Friday, October 16, 2009

Binary Search tree with insert delete, print option C

#include
#include
struct node{
        int val;
        struct node *left;
        struct node *right;
};

void insert(struct node**,int);
void insert_init(struct node**);
void inorder(struct node**);
void preorder(struct node**);
void postorder(struct node**);
void search_init(struct node**,struct node **,struct node **);
void search(struct node **,int,struct node**,struct node **);
void delete_init(struct node **,struct node**,struct node **);
void delete(struct node **,struct node **,struct node**);


int main()
{

        struct node *root=NULL;
        struct node *parent=NULL;
        struct node *curnode=NULL;
       int opt=0;

         while(1){
                printf("\n Select an option \n 1.Insert 2.Inorder  3. Preorder 4. Postorder 5.Seatch 6.delete 7.Exit\n");
                scanf("%d",&opt);
                switch(opt){
                        case 1:{ insert_init(&root);break; }
                        case 2:{ printf("\nInorder Traversal\n"); inorder(&root);break; }
                        case 3:{ printf("\nPreorder Traversal\n");preorder(&root);break; }
                        case 4:{ printf("\nPostorder Traversal\n");postorder(&root);break; }
                        case 5:{ search_init(&root,&parent,&curnode);break; }
                        case 6:{ delete_init(&root,&parent,&curnode);break; }
                        case 7:{ exit(0);}
                }
        }

}

void insert_init(struct node **root)                                             //function toread insert value
{                                                                                //input: address of root
        int val;

        printf("Enter an integer value\n");
        scanf("%d",&val);
        insert(root,val);

}
void insert(struct node **root,int val)                                         //function for insert
{                                                                                //input address of root and input value
        if(*root == NULL){
                *root=(struct node *)malloc(sizeof(struct node));
                (*root)->val=val;
                (*root)->left=NULL;
                (*root)->right=NULL;
                return;
        }
        if((*root)->val==val){
                printf("\nValue Already exists in the tree\n");
                return;
        }
        if((*root)->val > val)
                insert(&(*root)->left,val);
        else
                insert(&(*root)->right,val);

}
void inorder(struct node **root)                                                //function to perform inorder traversal
{                                                                               // input to function ,address of root
        if(*root == NULL)return;
        inorder(&(*root)->left);
        printf(" %d",(*root)->val);
        inorder(&(*root)->right);
}
void preorder(struct node **root)                                               //function to perform preorder traversal
{
        if(*root == NULL)return;
        printf(" %d",(*root)->val);
        preorder(&(*root)->left);
        preorder(&(*root)->right);

}
void postorder(struct node **root)                                              //function to perform postorder traversal
{
        if(*root == NULL)return;
        preorder(&(*root)->left);
        preorder(&(*root)->right);
        printf(" %d",(*root)->val);
}
void search_init(struct node **root,struct node **parent,struct node **curnode)
{
        int searchitem;

        *parent==NULL;
        printf("Enter the search item\n");
        scanf("%d",&searchitem);
        search(root,searchitem,parent,curnode);
}
void search(struct node **root,int searchitem,struct node **parent,struct node **curnode) //function to perform search operation
{                                                                               //inputs: root address, search item parent
        if(*root == NULL) {
                printf("\nItem not Found\n");
                *curnode=*root;
                return;
        }
        if(searchitem == (*root)->val) {
                printf("\nItem found in the tree\n");
                *curnode=*root;
                return;
        }
        *parent=*root;
        if(searchitem>(*root)->val) {
                search(&(*root)->right,searchitem,parent,curnode);
        } else {
                search(&(*root)->left,searchitem,parent,curnode);
        }
}
void delete_init(struct node **root,struct node **parent,struct node **curnode)       //function to read delete item
{
        int item;
        struct node *temp;

        printf("\n Enter the item to delete");
        scanf("%d",&item);
        search(root,item,parent,curnode);
        if(curnode==NULL)return;
        delete(root,curnode,parent);
}
void delete(struct node **root,struct node **nodetodel,struct node **parent)           //function to delete a node
{

         struct node *tempparent;
         struct node *swapnode;

         if(((*nodetodel)->right == NULL )&&((*nodetodel)->left == NULL)) {
                if(*parent==NULL) {
                 *root=NULL;
                  free(*nodetodel);
                }else {
                  if((*parent)->val > (*nodetodel)->val) {
                       (*parent)->left=NULL;
                }else {
                        (*parent)->right=NULL;
                }
                   free(*nodetodel);
                }
                return;
         }

        if((*nodetodel)->right!=NULL) {

                swapnode=*nodetodel;
                tempparent=swapnode;
                swapnode=swapnode->right;
                while(swapnode->left != NULL) {                                  //traverse to leftmost node
                        tempparent=swapnode;
                        swapnode=swapnode->left;
                }
                (*nodetodel)->val=swapnode->val;
                *parent=tempparent;
                if( (*parent)->val > swapnode->val ) {

                        (*parent)->left=NULL;
                } else {
                        (*parent)->right=NULL;
                }
                if(swapnode->right != NULL)
                        (*parent)->left=swapnode->right;                //new condition(for debug)
                free(swapnode);
                 return;
         }
   if((*nodetodel)->left != NULL) {
                swapnode=*nodetodel;
                tempparent=swapnode;
                swapnode=swapnode->left;

                while(swapnode->right != NULL) {                         //traverse to right most node
                        tempparent=swapnode;
                        swapnode=swapnode->right;
                }
               (*nodetodel)->val=swapnode->val;
               *parent=tempparent;
                if( (*parent)->val > swapnode->val ) {
                        (*parent)->left=NULL;
                } else {
                        (*parent)->right=NULL;
                }
                if(swapnode->left != NULL)
                        (*parent)->right=swapnode->left;                //new condition (for debug)

                 free(swapnode);
                 return;
        }

}

Simple Linked Stack

#include
#include
struct node{
        int val;
        struct node *next;
};

void push(struct node**);
void pop(struct node**);

int main()
{
  struct node *head=NULL;
  int opt=0;

   while(1){
          printf("Select an option \n 1.Push 2.Pop 3.Exit\n");
          scanf("%d",&opt);
          switch(opt){
          case 1:{push(&head); break; }
          case 2:{pop(&head);break;}
          case 3:{ exit(0); }
        }
     }
}
void push(struct node **head)          //to insert at begining
{                                                         //input is address of head
   struct node *ptr;

   ptr=(struct node*)malloc(sizeof(struct node));
   if(ptr==NULL){
      printf("Allocation failed\n");
      return;
   }
    printf("Enter an integer value\n");
    scanf("%d",&ptr->val);
    ptr->next=*head;
    *head=ptr;

}


void pop(struct node **head)              //to pop an element out
{                                                         //input is address of head    if(*head==NULL){
        printf("\n Stack Empty\n");
        return;
    }
    struct node *ptr;
    ptr=*head;
    printf("the poped value is%d\n",ptr->val);
    *head=ptr->next;
     free(ptr);
}

Simple Linked list

#include
#include
struct node{
        int val;
        struct node *next;        // to store the next value
};

void insert(struct node**);
void delete(struct node**);
void traverse(struct node**);
int main()
{
  struct node *head=NULL;
  int opt=0;

   while(1){
          printf("Select an option \n 1.Insert 2.Delete 3.Exit 4. Traverse\n");
          scanf("%d",&opt);
          switch(opt){
          case 1:{insert(&head); break; }
          case 2:{delete(&head);break;}
          case 3:{ exit(0);break; }
          case 4:{traverse(&head);}
          default:{printf("Invalid no");}
        }
     }
}


void traverse(struct node **head)   // function to traverse
{                                   // input Address of head
    printf("\n");
    struct node *ptr;
    if(*head==NULL){
       printf("List Empty");
        return;
        }
    ptr=*head;
    while(ptr!=NULL){
       printf(" %d",ptr->val);
       ptr=ptr->next;
    }
   printf("\n");

}

void insert(struct node **head)        // function to insert new node
{                                      // input address of head
   struct node *ptr,*temp;

   ptr=(struct node*)malloc(sizeof(struct node));
   if(ptr==NULL){
      printf("Allocation failed\n");
      return;
    }
    printf("Enter an integer value\n");
    scanf("%d",&ptr->val);
    temp=*head;
    if(temp==NULL){
      ptr->next=NULL;
      *head=ptr;
       return;
     }
    while(temp->next!=NULL)temp=temp->next;
    temp->next=ptr;
}

void delete(struct node **head)          // function to delete a node
{                                        // input address of head
    struct node *ptr=*head;
    struct node *prevptr=NULL;
    int val;
    if(*head==NULL){
        printf("List empty");
        return;
    }
   printf("Enter the value to delete\n");
   scanf("%d",&val);
    while((ptr->val!=val)&&(ptr->next!=NULL)){
        prevptr=ptr;
        ptr=ptr->next;
    }

     if(ptr->val!=val){
          printf("Value not found\n");
          return;
     }
    if(prevptr==NULL){                  //if selected element has no previous element
        *head=ptr->next;
         free(ptr);
         return;

     }
     prevptr->next=ptr->next;
     traverse(head);
     free(ptr);
}

c search replace string example

#include
#include
int main()
{
char replaceitem[100];
char str[300];
char newstr[300];
char searchitem[100];
int flag=0;
int i=0;
int index=0;
int j=0;
int k=0;

printf("Enter String\n");
fgets(str,300,stdin);

printf("Enter Search item\n");
fgets(searchitem,100,stdin);

printf("Enter replacement item\n");
fgets(replaceitem,100,stdin);

for( i = 0;i < strlen(str); i++) {
for(j=0;searchitem[j]==str[i+j];j++){
if(searchitem[j+1]=='\0')
break;
}
if((j==strlen(searchitem)-1)){
for(k=0;k newstr[index++]=replaceitem[k];
flag=1;
}
i+=strlen(searchitem)-2;
}
if(flag==1){
flag=0;
continue;
}
newstr[index++]=str[i];
}
newstr[index]='\0';
puts(newstr);
return 0;
}

combine 2, 32 bit no to create 64 bit no C

#define shift(val)val=val<<32
#define concat(val,num2)val=val|num2
#define call(val,num2) shift(val);concat(val,num2)
int main()
{
int num1;
int num2;
unsigned long long val;

scanf("%d",&num1);
scanf("%d",&num2);
val=(long long)num1;
call(val,num2);
printf("%lld",val);

}

Tuesday, April 7, 2009

How to change color of BOX2D objects

Well for changing box2d color you need to edit file b2World.as in BOX2D/Dynamics

In about 1060 line number


for (s = b.GetShapeList(); s; s = s.m_next)
{
if (b.IsStatic())
{
DrawShape(s, xf, new b2Color(1, 1,1), core);
}
else if (b.IsSleeping())
{
DrawShape(s, xf, new b2Color(0.0, 0.0, 0.9), core);
}
else
{
DrawShape(s, xf, new b2Color(0.8, 0.7, 0.5), core);
}
}

change the values in new b2Color(0.4, 0.4,1) to change the color

Sunday, March 1, 2009

How access dll from flex

As far as i know there is no direct method through which you can
access dll files from flex.How ever one thing you can do is to
create webservice in asp .net and call that webservice from flex program.
You can do the same with webservice, httpservice, Remoting (using remote objects)


The best example i can find in adobes site here
http://www.adobe.com/devnet/flex/articles/communicating_flex_dotnet.html
communicating_flex_dotnet_06.html

you can use fluorinefx http://www.fluorinefx.com/ for flex .net remoting
in addition you can use weborg

http://www.themidnightcoders.com
example :

http://www.adobe.com/devnet/flex/articles/communicating_flex_dotnet_06.html
httpservice (For simple data transmission)
webservice (Datain Xml Format)
Remoting (Pass objects)

Another method through which an swf can call a dll is through External interfaces.
You can load the swf into your .net program. Then using external interface swf can communicate with c#.
I found a good Example here

Here is an another method using ActiveX component by srinivas

here

ttp://livedocs.adobe.com/flex/3/html/help.html?content=19_External_Interface_10.html