Saturday, August 21, 2010

SEARCH Liner

/*liner search*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,item,loc=-1;
clrscr();
printf("\nEnter the number of element:");
scanf("%d",&n);
printf("Enter the number:\n");
for(i=0;i<=n-1;i++)
   {
     scanf("%d",&a[i]);
   }
printf("Enter the no. to be search\n");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
   {
    if(item==a[i])
      {
       loc=i;
       break;
     }
   }
if(loc>=0)

   printf("\n%dis found in position%d",item,loc+1);
   else
   printf("\nItem does not exits");
   getch();
     }

/*output of the program*/

Enter the number of element:5
Enter the number:
1
2
5
6
3
Enter the no. to be search
3

3is found in position5

GRAPH

GXTechno Tags: , , ,

#include <stdio.h>
#define Black 2
# define max 20
typedef struct node_type
    {
        int vertex;
        struct node_type *link;
    }    node;

    typedef struct queue_type
    {
        int info;
        struct queue_type *next;
    }    queue;
    void main (void)
    {
        node*adj[Max];
        int n,s;
        clrscr() ;
        printf("Enter No. of Nodes:");
        scanf ("%d", & n);
        create_graph (adj, n);
        input_graph(adj, n);
        clrscr();
        printf ("enter source vcr tax");
        scanf ("%d" , &s);
        printf ("bfs from vertax")
        bfs (adj, n,s);
        getch();
    }
    void create_graph (node * adj[], int num)
    {
        int i ;
        for (i=1; i<=num; i++)
            adj[i] =Null;
    }
    void bfs(node * adj[], int n, int s)
    {
        node *ptr;
        queue*front, *rear;
        int i, color(max);
        create_empty_Queue (&front, &rear);
        for (i=1; i<=n; i++) color[i]=white;
        color(s)=Gray;
        enqueue(&front, &rear, s);
        while (!empty(front))
        {
            v=head[front);
            ptr=adj[u];
            v=ptr->vertex;
            if (color[v]=white)
            {
                color[v] =gray;
                enqueue (&front, &rear,v);
            }
                ptr= ptr->link;
        }
            v=dequeue(&front, &rear);
            printf("%d", u);
            color[u]= Black;
    }

TREE with Depth of it.

#include<stdio.h>
#include<conio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void depth(struct rec *tree);
struct rec *temp;
int count=1,total1,total2,dep;
void main()
{
struct rec *tree=NULL;
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1: puts("Enter integers: To quit enter0");
    scanf("%ld",&digit);
    while(digit!=0)
    {
    tree=insert(tree,digit);
    scanf("%ld",&digit);
    }continue;
case 2: depth(tree);
    if(total1>total2)
      {
        dep=total1-1;
        printf("Depth=%d\n",dep);
      }
      else
      dep=total2-1;
      printf("Depth=%d\n",dep);
      total1=total2=dep=0;
      continue;
case 3: puts("END");exit(0);
}
}while(choice!=3);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node");
puts("Enter 2: Display depth");
puts("Enter 3: END");
puts("Eter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>3))
{
puts("Wrong choice: Try again");
getchar();
}
}while((selection<1)||(selection>3));
return selection;
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;
}
else
if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return(tree);
}
void depth(struct rec *tree)
{
if(tree!=NULL)
{
if(tree->left!=NULL)
{
total1++;
depth(tree->left);
}
if(tree->right!=NULL)
{
total2++;
depth(tree->right);
}
}
}

/*output of the program*/
Enter 1: Insert a node
Enter 2: Display depth
Enter 3: END
Eter your choice
1
Enter integers: To quit enter0
1
2
3
5
4
0
Enter 1: Insert a node
Enter 2: Display depth
Enter 3: END
Eter your choice
2
Depth=3
Enter 1: Insert a node
Enter 2: Display depth
Enter 3: END
Eter your choice
3

TREE Traversing

GXTechno Tags: , , , ,

#include<stdio.h>
#include<conio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree,*second,*head;
struct rec *insert(struct rec *tree,long num);
struct rec *copy(struct rec *tree);
void inorder(struct rec *tree);
main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1:puts("Enter integers:To quit enter 0");
    scanf("%ld",&digit);
    while(digit!=0)
    {
    tree=insert(tree,digit);
    scanf("%ld",&digit);
    }continue;
case 2: copy(tree);continue;
case 3: puts("Inorder traversing TREE");
    inorder(tree);continue;
case 4: puts("END");exit(0);
}
}while(choice!=4);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BST");
puts("Enter 2: Copy a tree to another BST");
puts("Enter 3: Display(inorder)the BST");
puts("Enter 4: END");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>4))
{puts("Wrong choice: Try again");
getchar();
}
}while((selection<1)||(selection>4));
return selection;
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;
}
else
if(digit<tree->num)
tree->left=insert(tree->left,digit);
else if(digit>tree->num)
tree->right=insert(tree->right,digit);
else if(digit==tree->num)
{puts("Duplicate nodes: program exited");exit(0);
}
return(tree);
}
struct rec *copy(struct rec *tree)
{
second=(struct rec *)malloc(sizeof(struct rec));
head=second;
if(tree!=NULL)
{
second->num=tree->num;
if(tree->left!=NULL)
{
second->left->num=tree->left->num;
copy(tree->right);
}
if(tree->right!=NULL)
{
second->right->num=tree->num;
copy(tree->left);
}
}
return(head);
}
void inorder(struct rec *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}

/*output of the progam*/
Enter 1: Insert a node in the BST
Enter 2: Copy a tree to another BST
Enter 3: Display(inorder)the BST
Enter 4: END
Enter your choice
1
Enter integers:To quit enter 0
1
2
3
5
6
0
Enter 1: Insert a node in the BST
Enter 2: Copy a tree to another BST
Enter 3: Display(inorder)the BST
Enter 4: END
Enter your choice
2
Enter 1: Insert a node in the BST
Enter 2: Copy a tree to another BST
Enter 3: Display(inorder)the BST
Enter 4: END
Enter your choice
3
Inorder traversing TREE
           1
           2
           3
           5
           6
Enter 1: Insert a node in the BST
Enter 2: Copy a tree to another BST
Enter 3: Display(inorder)the BST
Enter 4: END
Enter your choice
4

TREE

#include<stdio.h>
#include<conio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void *exchange(struct rec *tree);
struct rec *temp;
void main()
{
struct rec *tree=NULL;
int choice;
long digit;
do
  {
    choice=select();
    switch(choice)
    {
      case 1: puts("Enter integer: To quit enter 0");
          scanf("%ld",&digit);
          while(digit!=0)
          {
        tree=insert(tree,digit);
        scanf("%ld",&digit);
        }continue;
      case 2: printf("%5d\n",tree->num);exchange(tree);continue;
      case 3: puts("END");exit(0);
      }
      }while(choice!=3);
      }
      int select()
      {
      int selection;
      do
      {
      puts("Enter 1: Insert a node");
      puts("Enter 2: Exchange subtrees");
      puts("Enter 3: End");
      puts("Enter your choice");
      scanf("%d",&selection);
      if((selection<1)||(selection>3))
      {puts("Wrong choice: Try again");
       getchar();
       }
       }while((selection<1)||(selection>3));
       return selection;
       }
       struct rec *insert(struct rec *tree,long digit)
       {
       if(tree==NULL)
       {
       tree=(struct rec *)malloc(sizeof(struct rec));
       tree->left=tree->right=NULL;
       tree->num=digit;
       }
       else
       if(digit<tree->num)
       tree->left=insert(tree->left,digit);
       else if(digit>tree->num)
       tree->right=insert(tree->right,digit);
       else if(digit==tree->num)
       {puts("Duplicates Nodes: Program Exited");exit(0);
       }
       return(tree);
       }
       void *exchange(struct rec *tree)
       {
       if((tree->left->num!=0)&&(tree->right->num!=0))
       {
       temp=tree->left;
       tree->left=tree->right;
       tree->right=temp;
       printf("%5ld\n",tree->left->num);
       printf("%5ld\n",tree->right->num);
       exchange(tree->left);
       exchange(tree->right);
       }
       }

/*output of the program*/
Enter 1: Insert a node
Enter 2: Exchange subtrees
Enter 3: End
Enter your choice
1
Enter integer: To quit enter 0
1
2
3
5
4
65
0
Enter 1: Insert a node
Enter 2: Exchange subtrees
Enter 3: End
Enter your choice
2
    1
Enter 1: Insert a node
Enter 2: Exchange subtrees
Enter 3: End
Enter your choice
3

TREE

GXTechno Tags: , , ,

#include<stdio.h>
#include<conio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void countnode(struct rec *tree);
void countleave(struct rec *tree);
struct rec *temp;
int node=1,total;
main()
{
    struct rec *tree=NULL;
    int choice;
    long digit;
    do
      {
        choice=select();
        switch(choice)
        {
          case 1: puts("Enter integers:To quit enter 0");
              scanf("%ld",&digit);
              while(digit!=0)
                   {
                 tree=insert(tree,digit);
                 scanf("%ld",&digit);
                }continue;
          case 2: countnode(tree);
              printf("Nuber of nodes=%d\n",node);
              node=1;continue;
          case 3: countleave(tree);
              printf("Nuber of leaves=%d\n",total);
              total=0;
              continue;
          case 4: puts("END");exit(0);
         }
      }while(choice!=4);
}
   int select()
      {
       int selection;
       do
     {
       puts("Enter 1: Insert a node");
       puts("Enter 2: Display number of nodes");
       puts("Enter 3: Display number of leave");
       puts("Enter 4: End");
       puts("Enter your choice");
       scanf("%d",&selection);
       if((selection<1)||(selection>4))
         {
           puts("Wrong choice: Try again");
           getchar();
         }
     }while((selection<1)||(selection>4));
        return selection;
    }
struct rec *insert(struct rec *tree,long digit)
{
  if(tree==NULL)
    {
       tree=(struct rec *)malloc(sizeof(struct rec ));
       tree->left=tree->right=NULL;
       tree->num=digit;
     }
else
   if(digit<tree->num)
      tree->left=insert(tree->left,digit);
else
   if(digit>tree->num)
     tree->right=insert(tree->right,digit);
else
    if(digit==tree->num)
      {
       puts("Duplicates Nodes:Program Exited");
       exit(0);
      }
return(tree);
}
void countnode(struct rec *tree)
{
    if(tree!=NULL)
      {
      if(tree->left!=NULL)
    {
      node++;
      countnode(tree->left);
     }
      if(tree->right!=NULL)
    {
      node++;
      countnode(tree->right);
    }
       }
}
void countleave(struct rec *tree)
{
   if(tree!=NULL)
     {
      if((tree->left==NULL)&&(tree->right==NULL))
     total++;
else
   countleave(tree->left);
   countleave(tree->right);
     }
}

/*output of the program*/
Enter 1: Insert a node
Enter 2: Display number of nodes
Enter 3: Display number of leave
Enter 4: End
Enter your choice
1
Enter integers:To quit enter 0
1
2
5
6
4
0
Enter 1: Insert a node
Enter 2: Display number of nodes
Enter 3: Display number of leave
Enter 4: End
Enter your choice
2
Nuber of nodes=5
Enter 1: Insert a node
Enter 2: Display number of nodes
Enter 3: Display number of leave
Enter 4: End
Enter your choice
3
Nuber of leaves=2
Enter 1: Insert a node
Enter 2: Display number of nodes
Enter 3: Display number of leave
Enter 4: End
Enter your choice
4

TREE

/*Program to reconstruct a binary search tree. */

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

#define MAX 101

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

void insert ( struct node **, int ) ;
void preorder ( struct node * ) ;
void postorder ( struct node * ) ;
void inorder ( struct node * ) ;
struct node * recons ( int *, int *, int ) ;
void deltree ( struct node * ) ;

int in[MAX], pre[MAX], x ;

void main( )
{
    struct node *t, *p, *q ;
    int req, i, num ;

    t = NULL ;  /* empty tree */

    clrscr( ) ;
    printf ( "Specify the number of items to be inserted: " ) ;
    while ( 1 )
    {
        scanf ( "%d", &req ) ;
        if ( req >= MAX || req <= 0 )
            printf ( "\nEnter number between 1 to 100.\n" ) ;
        else
            break ;
    }

    for ( i = 0 ; i < req ; i++ )
    {
        printf ( "Enter the data: " ) ;
        scanf ( "%d", &num ) ;
        insert ( &t, num ) ;
    }

    printf ( "\nIn-order   Traversal:\n" ) ;
    x = 0 ;
    inorder ( t ) ;

    printf ( "\nPre-order  Traversal:\n" ) ;
    x = 0 ;
    preorder ( t ) ;

    printf ( "\nPost-order Traversal:\n" ) ;
    x = 0 ;
    postorder ( t ) ;

    deltree ( t ) ;
    t = NULL ;
    t = recons ( in, pre, req ) ;

    printf ( "\n\nAfter reconstruction of the binary tree.\n" ) ;

    x = 0 ;
    printf ( "\nIn-order   Traversal:\n" ) ;
    inorder ( t ) ;

    x = 0 ;
    printf ( "\nPre-order  Traversal:\n" ) ;
    preorder ( t ) ;
    x = 0 ;
    printf ( "\nPost-order Traversal:\n" ) ;
    postorder ( t ) ;

    deltree ( t ) ;
    getch( ) ;
}

/* inserts a new node in a binary search tree */
void insert ( struct node **sr, int num )
{
    if ( *sr == NULL )
    {
        *sr = ( struct node * ) malloc ( sizeof ( struct node ) ) ;

        ( *sr ) -> left = NULL ;
        ( *sr ) -> data = num ;
        ( *sr ) -> right = NULL ;
        return ;
    }
    else  /* search the node to which new node will be attached */
    {
        /* if new data is less, traverse to left */
        if ( num < ( *sr ) -> data )
            insert ( &( ( *sr ) -> left ), num ) ;
        else
            /* else traverse to right */
            insert ( &( ( *sr ) -> right ), num ) ;
    }
}

void preorder ( struct node *t )
{
    if ( t != NULL )
    {
        printf ( "%d\t", pre[x++]= t -> data ) ;
        preorder ( t -> left ) ;
        preorder ( t -> right ) ;
    }
}

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

void inorder ( struct node *t )
{
    if ( t != NULL )
    {
        inorder ( t -> left ) ;
        printf ( "%d\t", in[x++]= t -> data ) ;
        inorder ( t -> right ) ;
    }
}

struct node * recons ( int *inorder, int *preorder, int noofnodes )
{
    struct node *temp, *left, *right ;
    int tempin[100], temppre[100], i, j ;

    if ( noofnodes == 0 )
        return NULL ;

    temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
    temp -> data = preorder[0] ;
    temp -> left = NULL ;
    temp -> right = NULL ;

    if ( noofnodes == 1 )
        return temp ;
    for ( i = 0 ; inorder[i] != preorder[0] ; )
        i++ ;

    if ( i > 0 )
    {
        for ( j = 0 ; j <= i  ; j++ )
            tempin[j] = inorder[j] ;

        for ( j = 0 ; j < i  ; j++ )
            temppre[j] = preorder[j + 1] ;
    }

    left = recons ( tempin, temppre, i ) ;
    temp -> left = left ;

    if ( i < noofnodes - 1 )
    {
        for ( j = i ; j < noofnodes - 1 ; j++ )
        {
            tempin[j - i] = inorder[j + 1] ;
            temppre[j - i] = preorder[j + 1] ;
        }
    }

    right = recons ( tempin, temppre, noofnodes - i - 1 ) ;
    temp -> right = right ;

    return temp ;
}

void deltree ( struct node *t )
{
    if ( t != NULL )
    {
        deltree ( t -> left ) ;
        deltree ( t -> right ) ;
    }
    free ( t ) ;
}

/*output of the program*/
Specify the number of items to be inserted: 6
Enter the data: 2
Enter the data: 3
Enter the data: 5
Enter the data: 6
Enter the data: 4
Enter the data: 8

In-order   Traversal:
2       3       4       5       6       8
Pre-order  Traversal:
2       3       5       4       6       8
Post-order Traversal:
4       8       6       5       3       2

After reconstruction of the binary tree.

In-order   Traversal:
2       3       4       5       6       8
Pre-order  Traversal:
2       3       5       4       6       8
Post-order Traversal:
4       8       6       5       3       2

TREE

GXTechno Tags: , , , ,

/*searching of tree*/
#include<stdio.h>
struct rec
{
    long num;
    struct rec *left;
    struct rec *right;
};
struct rec *tree=NULL;
struct rec *tree;
struct rec *delnum(long digit,struct rec *r);
struct rec *insert(struct rec *tree,long num);
struct rec *deletenode(long digit,struct rec *tree);
void search(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
void main()
{
int choice;
long digit;
int element;
    do
      {
        choice=select();
        switch(choice)
         {
           case 1: puts("Enter integer: To quit enter 0");
               scanf("%ld",&digit);
               while(digit!=0)
                {
                   tree=insert(tree,digit);
                   scanf("%ld",&digit);
                }continue;
           case 2: puts("Enter the number to be search");
               scanf("%ld",&digit);
               search(tree,digit);
               continue;
           case 3: puts("\npreorder traversing TREE");
               preorder(tree);continue;
           case 4: puts("\ninorder traversing TREEE");
               inorder(tree);continue;
           case 5: puts("\npostorder traversing TREE");
               postorder(tree);continue;
           case 6: puts("Enter element which do you wanbt delete from  the BST");
                scanf("%ld",&digit);
               deletenode(digit,tree);continue;
           case 7: puts("END");exit(0);
        }
    }while(choice!=7);
}
int select()
{
int selection;
    do
      {
        puts("Enter 1: Insert a node in the BST");
        puts("Enter 2: Search a node in BST");
        puts("Enter 3: Display(preorder)the BST");
        puts("Enter 4: Display(inorder)the BST");
        puts("Enter 5: Display(postorder) the BST");
        puts("Enter 6: Delete the element");
        puts("Enter 7: END");
        puts("Enter your choice");
        scanf("%d",&selection);
             if((selection<1)||(selection>7))
            {
              puts("wrong choice:Try again");
              getch(); }
       }while((selection<1)||(selection>7));
       return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
    if(tree==NULL)
      {
        tree=(struct rec *)malloc(sizeof(struct rec));
        tree->left=tree->right=NULL;
        tree->num=digit;
      }
     else
      if(digit<tree->num)
        tree->left=insert(tree->left,digit);
     else
      if(digit>tree->num)
        tree->right=insert(tree->right,digit);
     else if(digit==tree->num)
        {
           puts("Duplicate node:program exited");exit(0);
        }
        return(tree);
}
struct rec *delnum(long digit,struct rec *r)
{
struct rec *q;
if(r->right!=NULL)delnum(digit,r->right);
else
q->num=r->num;
q=r;
r=r->left;
}
struct rec *deletenode(long digit,struct rec *tree)
{
struct rec *r,*q;
if(tree==NULL)
{
puts("Tree is empty.");
exit(0);
}
if(digit<tree->num)
deletenode(digit,tree->left);
else
if(digit>tree->num)deletenode(digit,tree->right);
q=tree;
if((q->right==NULL)&&(q->left==NULL))
q=NULL;
else
if(q->right==NULL)tree=q->left;else
if(q->left==NULL)tree=tree=q->right;else
delnum(digit,q->left);
free(q);
}
void search(struct rec *tree,long digit)
{
    if(tree==NULL)
       puts("The number does not exits\n");
   else
    if(digit==tree->num)
    printf("Number=%ld\n" ,digit);
   else
    if(digit<tree->num)
       search(tree->left,digit);
   else
      search(tree->right,digit);
}
void preorder(struct rec *tree)
{
    if(tree!=NULL)
      {
        printf("%12ld\n",tree->num);
        preorder(tree->left);
        preorder(tree->right);
      }
}
void inorder(struct rec *tree)
{
    if(tree!=NULL)
        {
        inorder(tree->left);
        printf("%12ld\n",tree->num);
        inorder(tree->right);
        }
}
void postorder(struct rec *tree)
{
    if(tree!=NULL)
      {
        postorder(tree->left);
        postorder(tree->right);
        printf("%12ld\n",tree->num);
      }

}

/*output of the program*/
Enter 1: Insert a node in the BST
Enter 2: Search a node in BST
Enter 3: Display(preorder)the BST
Enter 4: Display(inorder)the BST
Enter 5: Display(postorder) the BST
Enter 6: Delete the element
Enter 7: END
Enter your choice
2
Enter the number to be search
6
Number=6
Enter 1: Insert a node in the BST
Enter 2: Search a node in BST
Enter 3: Display(preorder)the BST
Enter 4: Display(inorder)the BST
Enter 5: Display(postorder) the BST
Enter 6: Delete the element
Enter 7: END
Enter your choice
3

preorder traversing TREE
           2
           3
           5
           4
           6
           8
Enter 1: Insert a node in the BST
Enter 2: Search a node in BST
Enter 3: Display(preorder)the BST
Enter 4: Display(inorder)the BST
Enter 5: Display(postorder) the BST
Enter 6: Delete the element
Enter 7: END
Enter your choice
7

TREE

GXTechno Tags: , , , ,

/*traversing of tree*/
#include<stdio.h>
struct rec
{
    long num;
    struct rec *left;
    struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
int count=1;
main()
{
    int choice;
    long digit;
    do
     {
       choice=select();
       switch(choice)
          {
        case 1: puts("Enter integer: To quit enter 0");
            scanf("%ld",&digit);
            while(digit!=0)
                {
                tree=insert(tree,digit);
                scanf("%ld",&digit);
                }continue;
        case 2: puts("\npreorder traversing TREE");
            preorder(tree);continue;
        case 3: puts("\ninorder traversing TREEE");
            inorder(tree);continue;
        case 4: puts("\npostorder traversing TREE");
            postorder(tree);continue;
        case 5: puts("END");exit(0);
        }
    }while(choice!=5);
}
int select()
{
int selection;
    do
      {
        puts("Enter 1: Insert a node in the BT");
        puts("Enter 2: Display(preorder)the BT");
        puts("Enter 3: Display(inorder)the BT");
        puts("Enter 4: Display(postorder)the BT");
        puts("Enter 5: END");
        puts("Enter your choice");
        scanf("%d",&selection);
             if((selection<1)||(selection>5))
            {
              puts("wrong choice:Try again");
              getch(); }
       }while((selection<1)||(selection>5));
       return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
    if(tree==NULL)
      {
        tree=(struct rec *)malloc(sizeof(struct rec));
        tree->left=tree->right=NULL;
        tree->num=digit;count++;
      }
else
    if(count%2==0)
    tree->left=insert(tree->left,digit);
else
    tree->right=insert(tree->right,digit);
    return(tree);
}
void preorder(struct rec *tree)
{
    if(tree!=NULL)
      {
        printf("%12ld\n",tree->num);
        preorder(tree->left);
        preorder(tree->right);
      }
}
void inorder(struct rec *tree)
{
    if(tree!=NULL)
        {
        inorder(tree->left);
        printf("%12ld\n",tree->num);
        inorder(tree->right);
        }
}
void postorder(struct rec *tree)
{
    if(tree!=NULL)
      {
        postorder(tree->left);
        postorder(tree->right);
        printf("%12ld\n",tree->num);
      }

}

/*output of the program*/
Enter 1: Insert a node in the BT
Enter 2: Display(preorder)the BT
Enter 3: Display(inorder)the BT
Enter 4: Display(postorder)the BT
Enter 5: END
Enter your choice
1
Enter integer: To quit enter 0
2

0
Enter 1: Insert a node in the BT
Enter 2: Display(preorder)the BT
Enter 3: Display(inorder)the BT
Enter 4: Display(postorder)the BT
Enter 5: END
Enter your choice
1
Enter integer: To quit enter 0
2
3
4
5
6
0
Enter 1: Insert a node in the BT
Enter 2: Display(preorder)the BT
Enter 3: Display(inorder)the BT
Enter 4: Display(postorder)the BT
Enter 5: END
Enter your choice
2

preorder traversing TREE
           2
           2
           4
           6
           3
           5
Enter 1: Insert a node in the BT
Enter 2: Display(preorder)the BT
Enter 3: Display(inorder)the BT
Enter 4: Display(postorder)the BT
Enter 5: END
Enter your choice
5

Trigonometry Converter

GXTechno Tags: , ,

#include<stdio.h>
#include<conio.h>
#include<limits.h>
int select();
struct rec
{
float coef;
int exp;
struct rec *next;
};
struct rec *rear;
struct rec *create(struct rec *list);
void *add(struct rec *first,struct rec *second);
struct rec *insert(double coef,int exp,struct rec *rear);
void *display(struct rec *list);
int nodes;
void main()
{
struct rec *first=NULL,*second=NULL;
int choice;
do
{
choice=select();
switch(choice)
{
case 1: first=create(first);continue;
case 2: second=create(second);continue;
case 3: add(first,second);continue;
case 4: puts("END");exit(0);
}
}while(choice!=4);
}
int select()
{
int selection;
do
{
puts("Enter 1: create the first list");
puts("Enter 2: create the second list");
puts("Enter 3: add the two list");
puts("Enter 4: END");
puts("Entr your choice");
scanf("%d",&selection);
}while((selection<1)||(selection>4));
return (selection);
}
struct rec *create(struct rec *x)
{
float coef;
int exp;
int endexp=INT_MAX;
struct rec *element;
puts("Enter coefs &exp:exp in descending order:""to quit enter 0 for exp");
x=(struct rec *)malloc(sizeof(struct rec));
x->next=NULL;
rear=x;
for(;;)
{
puts("Enter coefficient");
element=(struct rec*)malloc(sizeof(struct rec));
scanf("%f",&coef);
element->coef=coef;
if(element->coef==0.0)break;
puts("Enter exponent");
scanf("%d",&exp);
element->exp=exp;
if((element->exp<=0)||(element->exp>=endexp))
{
puts("Invalid exponent");
break;
}
element->next=NULL;
rear->next=element;
rear=element;
}
x=x->next;
return(x);
}
void *add(struct rec *first,struct rec *second)
{
float total;
struct rec *end,*rear,*result;
result=(struct rec *)malloc(sizeof(struct rec));
rear=end;
while((first!=NULL)&&(second!=NULL))
{
if(first->exp==second->exp)
{
if((total=first->exp+second->exp)!=0.0)
rear=insert(total,first->exp,rear);
first=first->next;
second=second->next;
}
else
if(first->exp>second->exp)
{
rear=insert(first->coef,first->exp,rear);
first=first->next;
}else
{
rear=insert(second->coef,second->exp,rear);
second=second->next;
}
}
for(;first;first=first->next)
rear=insert(first->coef,first->exp,rear);
for(;second;second=second->next)
rear=insert(second->coef,second->exp,rear);
rear->next=NULL;
display(end->next);
free(end);
}
void *display(struct rec *head)
{
while(head!=NULL)
{
printf("%2lf",head->coef);
printf("%2d",head->exp);
head=head->next;
}
printf("\n");
}
struct rec *insert(double coef,int exp,struct rec *rear)
{
rear->next=(struct rec *)malloc(sizeof(struct rec));
rear=rear->next;
rear->coef=coef;
rear->exp=exp;
return(rear);
}

/*output of the program*/

Enter coefficient
2
Enter exponent
2
Enter coefficient
3
Enter exponent
1
Enter coefficient
2
Enter exponent
0
Invalid exponent
Enter 1: create the first list
Enter 2: create the second list
Enter 3: add the two list
Enter 4: END
Entr your choice
2
Enter coefs &exp:exp in descending order:to quit enter 0 for exp
Enter coefficient
1
Enter exponent
2
Enter coefficient
2
Enter exponent
3
Enter coefficient
2
Enter exponent
3
Enter coefficient
2
Enter exponent
0
Invalid exponent
Enter 1: create the first list
Enter 2: create the second list
Enter 3: add the two list
Enter 4: END
Entr your choice
3
4.000000 22.000000 32.000000 32.000000 23.000000 1
Enter 1: create the first list
Enter 2: create the second list
Enter 3: add the two list
Enter 4: END
Entr your choice

Linked List

GXTechno Tags: , , , ,

/*Write a program to attach two singly linked lists

#include<stdio.h>
#include<conio.h>

struct node//declaration of node structure
{
    int data;//DATA FIELD
    struct node *next;
};

struct list//declaration of list structure
{
    struct node *head;
};

/*initilisation of the list*/
void init(struct list *l)
{
    l->head=NULL;
}

/*create the new node*/
int create(struct node *p, int data)
{
    if(p==NULL)//IF THE LIST IS EMPTY
        return 1;

    else
    {
             /*ADJUST THE POINTERS*/
        p->data=data;
        p->next=NULL;
        return 0;
    }
}

/*for inserting the node*/
int insert(struct list *l,int data)
{
    struct node *p,*q;
    int x;

    p=(struct node *)malloc(sizeof(struct node));//allocate memory to the new node

    x=create(p,data);//call create function to create the new node

    if(x)//if newnode couldn't be created
    return x;

    else if(l->head==NULL)//if the list is already empty
        l->head=p;

    else
    {
        q=l->head;

        while(q->next!=NULL)//TILL THE END
        q=q->next;

        q->next=p;
    }
    return 0;
}

/*displays the list*/
int display(struct list *l)
{
    struct node *q=l->head;

    if(q==NULL)//if the list is empty
    return 1;

    else
    {
        printf("\n");

        while(q!=NULL)//till the end
        {
            printf(" %d-> ",q->data);
            q=q->next;
        }
        printf("NULL");
    return 0;
    }
}

/*attaches the second list to the first*/
int attach(struct list *l1,struct list *l2)
{
    struct node *p;

    p=l1->head;//p now points to first node

    if((l1->head==NULL)&&(l2->head==NULL))
    return 1;

    else if(l1->head==NULL)
    return 2;

    else if(l2->head==NULL)
    return 3;

    else
    {
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=l2->head;
        return 0;
    }
}

//main function
int main()
{
    int data,x,y,pos,ch;
    struct list l1,l2;

      /*declare the head pointers of two lists */
    l1.head=NULL;
    l2.head=NULL;

    clrscr();//clears the user screen

    do
    {
        printf("\n Enter the data in the first linked list(-1 to stop) : ");
        scanf("%d",&data);

        if(data==-1)
        break;

        x=insert(&l1,data);//function call

        if(x)
        printf("\n Node can not be inserted ");

    }while(data!=-1);

    printf("\n The first linked list is: ");

    x=display(&l1);//function call

    if(x)
    printf("\n List is empty ");

    do
    {
        printf("\n Enter the data in the second linked list(-1 to stop) : ");
        scanf("%d",&data);

        if(data==-1)
        break;

        x=insert(&l2,data);//function call

        if(x)
        printf("\n Node can not be inserted ");

    }while(data!=-1);

    printf("\n The second linked list is: ");

    x=display(&l2);//function call

    if(x)
    printf("\n List is empty ");

    y=attach(&l1,&l2);//function call[attaches list]

    if(y==1)
    printf("\n Lists are empty ");

    else if(y==2)
    {
        printf("\n First list is empty ");
        printf("\n The list after attachment is : ");
        display(&l2);
    }

    else if(y==3)
    {
        printf("\n Second list is empty ");
        printf("\n The list after attachment is : ");
        display(&l1);
    }

    else
    {
        printf("\n The attached linked list is : ");
        display(&l1);
    }

     /*free the memory occcupied by the linked lists*/
    free(l1);
    free(l2);

getch();
return 0;
}

/*
  OUTPUT

Enter the data in the first linked list(-1 to stop) : 1

Enter the data in the first linked list(-1 to stop) : 2

Enter the data in the first linked list(-1 to stop) : 3

Enter the data in the first linked list(-1 to stop) : -1

The first linked list is:
1->  2->  3-> NULL
Enter the data in the second linked list(-1 to stop) : 4

Enter the data in the second linked list(-1 to stop) : 5

Enter the data in the second linked list(-1 to stop) : -1

The second linked list is:
4->  5-> NULL
The attached linked list is :
1->  2->  3->  4->  5-> NULL

Malloc

GXTechno Tags: , , ,

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start;
void createmptylist(NODE **start)
{
*start=(NODE *)NULL;
}
void traversinorder(NODE *start)
{
while(start != (NODE *) NULL)
{
printf("%d\n",start->info);
start=start->next;
}
}
void insertatbegin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(start==(NODE *)NULL)
ptr->next=(NODE *)NULL;
else
ptr->next=start;
start=ptr;
}
void insert_at_end(int item)
{
NODE *ptr,*loc;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=(NODE *)NULL;
if(start==(NODE*)NULL)
start=ptr;
else
{
loc=start;
while(loc->next!=(NODE *)NULL)
loc=loc->next;
loc->next=ptr;
}
}

void dele_beg(void)
{
NODE *ptr;
if(start==(NODE *)NULL)
return;
else
{
ptr=start;
start=(start)->next;
free(ptr);
}
}
void dele_end(NODE *start)
{
NODE *ptr,*loc;
if(start==(NODE *)NULL)
return;
else if((start)->next==(NODE *)NULL)
{
ptr=start;
start=(NODE *)NULL;
free(ptr);
}
else
{
loc=start;
ptr=(start)->next;
while(ptr->next!=(NODE *)NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=(NODE *)NULL;
free(ptr);
}
}
void insert_spe(NODE *start,int item)
{
NODE *ptr,*loc;
int temp,k;
for(k=0,loc=start;k<temp;k++)
{
loc=loc->next;
if(loc==NULL)
{
printf("node in the list at less than one\n");
return;
}
}
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=loc->next;;
loc->next=ptr;
}
dele_spe(NODE *start)
{
NODE *ptr,*loc;
int temp;
printf("\nEnter the which element do you want to delete\n");
scanf("%d",&temp);
ptr=start;
if(start==NULL)
{
printf("Empty list\n");
return;
}
else
{
loc=ptr;
while(ptr!=NULL)
{
if(ptr->info==temp)
{
loc->next=ptr->next;
free(ptr);
return;
}
loc=ptr;
ptr=ptr->next;
}
}
}
void main()
{
int choice,item,after;
char ch;
clrscr();
createmptylist(start);
do
{
printf("1.Insert element at begin \n");
printf("2. insert element at end positon\n");
printf("3. insert specific the position\n");
printf("4.travers the list in order\n");
printf("5. delete from the begin\n");
printf("6. delete from the last\n");
printf("7. delete the element from the specific position\n");
printf("8. exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
    scanf("%d",&item);
    insertatbegin(item);
    break;
case 2:  printf("Enter the item\n");
    scanf("%d",&item);
    insert_at_end(item);
    break;
case 3: printf("Enter the item\n");
    scanf("%d",&item);
    insert_spe(start,item);
    break;
case 4: printf("\ntravers the list\n");
    traversinorder(start);
    break;
case 5: printf("Delete the item\n");
    dele_beg();
    break;
case 6: printf("Delete the element\n");
    dele_end(start);
    break;
case 7: printf("Delete the element");
    dele_spe(start);
    break;
case 8: return;
}
fflush(stdin);
printf("do your want continous\n");
scanf("%c",&ch);
}while((ch='y')||(ch='y'));
getch();
}

/*output of the program*/
2. insert element at end positon
3. insert specific the position
4.travers the list in order
5. delete from the begin
6. delete from the last
7. delete the element from the specific position
8. exit
enter your choice
1
Enter the item
2
do your want continous
y
1.Insert element at begin
2. insert element at end positon
3. insert specific the position
4.travers the list in order
5. delete from the begin
6. delete from the last
7. delete the element from the specific position
8. exit
enter your choice
2
Enter the item
3
do your want continous
y
1.Insert element at begin
2. insert element at end positon
3. insert specific the position
4.travers the list in order
5. delete from the begin
6. delete from the last
7. delete the element from the specific position
8. exit
enter your choice
5
Delete the item
do your want continous
y
1.Insert element at begin
2. insert element at end positon
3. insert specific the position
4.travers the list in order
5. delete from the begin
6. delete from the last
7. delete the element from the specific position
8. exit
enter your choice

Memory allocation function

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp;
int count=0;
int choice=1;
first=NULL;
while(choice)
{
    head=(NODE *)malloc(sizeof(NODE));
    printf("Enter the data item\n");
    scanf("%d",&head->num);
      if(first!=NULL)
        {
          temp->ptr=head;
          temp=head;
        }
else
        {
          first=temp=head;
        }
fflush(stdin);
printf("Do you want to continue(type 0 or 1)?\n");
scanf("%d",&choice);
}

temp->ptr=NULL;
temp=first;
printf("Status of the linked list is\n");
while(temp!=NULL)
    {
        printf("%d",temp->num);
        count++;
        temp=temp->ptr;
    }
printf("NULL");
printf("NO of nodes in the list =%d\n",count);
getch();
}

/*output of the program*/

Enter the data item
1
Do you want to continue(type 0 or 1)?
1

Enter the data item
2
Do you want to continue(type 0 or 1)?
1
Enter the data item
3
Do you want to continue(type 0 or 1)?
1
Enter the data item
4
Do you want to continue(type 0 or 1)?
0
Status of the linked list is
1234NULLNO of nodes in the list =4

Multiple Stacks Implementation

GXTechno Tags: , , , ,

#include<stdio.h>
#include<conio.h>

//Check whether stack is empty
int isempty(int *top,int *base,int stk)
{
    if((top[stk-1]==-1)||(top[stk-1]==base[stk-1]))//condition if the stack is empty
      return(1);
    else
      return(0);
}

//Check whether stack is full
int isfull(int *top,int *base,int stk,int total)
{
    if((top[stk-1]==total-1)||(top[stk-1]==base[stk]))//if the stack is full
      return(1);
    else
      return(0);
}

//Push operation(adds element) in stack
void push(int *array,int *top,int *base,int stk,int total,int info)
{
    if(isfull(top,base,stk,total))//check if stack is full
    {
      puts("\n\t*****Stack is full*****\n");
      return;
    }
    top[stk-1]+=1;//first increement the top pointer befor addition

    array[top[stk-1]]=info;//then push the value on to the stack
}

//Pop operation(deletes element) in stack
int pop(int *array,int *top,int *base,int stk)
{
    int temp;
    if(isempty(top,base,stk))//check if stack is empty
    {
      puts("\n\t*****Stack is empty*****\n");
      return(NULL);
    }
    temp=array[top[stk-1]];//first extract the value from the stack

    top[stk-1]-=1;//then decreement the pointer

    return(temp);//free the memory
}

//Display stack
void display(int *array,int *top,int *base,int stk)

Postfix expression calculation

GXTechno Tags: , , , ,

/*accepts a postfix expression and then calculate the expression for values*/
#include<stdio.h>
#include<conio.h>
float stack[10];
int top=-1;
void push(char);
float pop();
float eval(char [],float[]);
void main()
{
int i=0;
char suffix[20];
float value[20],result;
clrscr();
printf("Enter a valid postfix expression\t");
gets(suffix);
while (suffix[i]!='\0')
{
if(isalpha(suffix[i]))
{
fflush(stdin);
printf("\nEnter the value of %c",suffix[i]);
scanf("%f",&value[i]);
}
i++;
}
result=eval(suffix,value);
printf("The result of %s=%f",suffix,result);
getch();
}
float eval(char suffix[],float data[])
{
int i=0;
float op1,op2,res;
char ch;
while(suffix[i]!='\0')
{
ch=suffix[i];
if(isalpha(suffix[i]))
{
push(data[i]);
}
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+' : push(op1+op2);
break;
case '-' : push(op1-op2);
break;
case '*' : push(op1+op2);
break;
case '/' :push(op1/op2);
break;
case '^' : push(pow(op1,op2));
break;
}
}
i++;
}
res=pop();
return(res);
}
void push(char num)
{
top=top+1;
stack[top]=num;
}
float pop()
{
float num;
num=stack[top];
top=top-1;
return(num);
}

/*output of the program*/
enter a valid postfix expression
12+
the result of 12+ is 3

Infix to Postfix conversion

GXTechno Tags: , , , , ,

/*takes an infix expression and converts it into an equivalent postfix expression*/
#include<conio.h>
#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push (char);
char pop();
void main()
{
char infx[25];
printf("Enter the infix expression");
gets(infx);
in_to_post(infx);
getch();
}
void push (char symb)
{
if (top>=49)
{
printf("stack overflow");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("stack empty");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}
int preced(char ch)
{
if(ch==47)
{
return(5);
}
else
if(ch==42)
{
return(4);
}
else if(ch==43)
{
return(3);
}
else
return(2);
}
void in_to_post(char infix[])
{
int length;
static int index=0,pos=0;
char symbol,temp;
char postfix[40];
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case'(':push(symbol);
break;
case')' :temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
while (preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default : postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}

/*output of the program*/
eneter the infix expression1+2
12+

Link list

GXTechno Tags: , , , ,

/*linked list implementation of stacks*/

#include<stdio.h>
#include<conio.h>
struct stack
{
int no;
struct stack *next;
}
*start=NULL;
typedef struct stack st;
void push();
int pop();
void display();
void main()
{
char ch;
int choice,item;
do
{
clrscr();
printf("\n 1: push");
printf("\n 2: pop");
printf("\n 3: display");
printf("\n Enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1: push();
break;
case 2: item=pop();
printf("The delete element in %d",item);
break;
case 3: display();
break;
default : printf("\n Wrong choice");
};
printf("\n do you want to continue(Y/N)");
fflush(stdin);
scanf("%c",&ch);
}
while (ch=='Y'||ch=='y');
}
void push()
{
st *node;
node=(st *)malloc(sizeof(st));
printf("\n Enter the number to be insert");
scanf("%d",&node->no);
node->next=start;
start=node;
}
int pop()
{
st *temp;
temp=start;
if(start==NULL)
{
printf("stack is already empty");
getch();
exit();
}
else
{
start=start->next;
free(temp);
}
return(temp->no);
}
void display()
{
st *temp;
temp=start;
while(temp->next!=NULL)
{
printf("\nno=%d",temp->no);
temp=temp->next;
}
printf("\nno=%d",temp->no);
}

/*output of the program*/
1. PUSH
  2. POP
  3. DISPLPAY
  Enter your choice
1
eneter the element to be inserted
2
do you want to contniue(y/n)
y

1. PUSH
  2. POP
  3. DISPLPAY
  Enter your choice
2
deleted element is
2
do you want to contniue(y/n)
y

1. PUSH
  2. POP
  3. DISPLPAY
  Enter your choice
2
deleted element is
2
do you want to contniue(y/n)
n

Stack

GXTechno Tags: , , , , ,

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top=-1;
void main()
{
int choice;
char ch;
do
  {
    clrscr();
    printf("\n1. PUSH ");
    printf("\n2. POP ");
    printf("\n3. TRAVERSE ");
    printf("\nEnter your choice");
    scanf("%d",&choice);
    switch(choice)
     {
       case 1:  push();
        break;
       case 2:  printf("\nThe deleted element is %d",pop());
        break;
       case 3:  traverse();
        break;
       default: printf("\nYou Entered Wrong Choice");
       }
    printf("\nDo You Wish To Continue (Y/N)");
    fflush(stdin);
    scanf("%c",&ch);
    }
while(ch=='Y' || ch=='y');
}

void push()
{
int item;
if(Top == MAXSIZE - 1)
  {
    printf("\nThe Stack Is Full");
    getch();
    exit(0);
    }
else
  {
    printf("Enter the element to be inserted");
    scanf("%d",&item);
    Top= Top+1;
    stack[Top] = item;
    }
}

int pop()
{
int item;
if(Top == -1)
  {
    printf("The stack is Empty");
    getch();
    exit(0);
    }
else
   {
    item = stack[Top];
    Top = Top-1;
    }
return(item);
}

void traverse()
{
int i;
if(Top == -1)
  {
    printf("The Stack is Empty");
    getch();
    exit(0);
    }
else
  {
    for(i=Top;i>=0;i--)
     {
      printf("Traverse the element");
       printf("\n%d",stack[i]);
       }
    }
}

/*output of the program*/
1. PUSH
  2. POP
  3. TRAVERSE
  Enter your choice
1
eneter the to be inserted
2
do you want to contniue(y/n)
y

1. PUSH
  2. POP
  3. TRAVERSE
  Enter your choice
2
the deleted elment is
2

do you want to contniue(y/n)
n

Search This Blog