C and C++

Insertion Sort

void InsertionSort(float src[],float trgt[], int size){
    int i,j;
    for (trgt[0]=src[0],i=1; i < size-1; i++){
        for (j = i; i > 0; j--){
            if(trgt[j-1] > src[i])
               trgt[j]=trgt[j-1];
            else
               break;
        }
        trgt[j]=src[i];
    }


/** stack.c*//* ADT (Abstarct Data Type). a type with all bacic operations, with out   specifying its implementations.   ADTStack have the opeations ..
   push -- places a valeu on the stack
   pop  -- retrieves and delets a value from the top
   top  -- retrives the value at the top
   empty -- tests if the stack is empty
   full  -- tests if the stack is full
   reset -- clears the stack, or initializes the stack
*/
#define MAX_LEN 1000
#define EMPTY (-1)
#define FULL (MAX_LEN - 1)
typedef enum boolean{false,true} boolean;
typedef struct Stack{
    char s[MAX_LEN];
    int top;
}stack;
void reset(stack *stk){
    stk->top = EMPTY;
}
void push(char c,stack *stk){
    stk->top++;
    stk->s[stk->top ]=c;
}
Queue Using C Langage
/* queue.h  */
#include
#include
#include
#include
#define EMPTY 0
#define FULL 100
typedef unsigned int data;
typedef enum {false,true} boolean;
struct elem{  /* element of stack*/   
     data d;   
     struct elem *next;
};

typedef struct elem elem;
struct queue{ 
    int cnt; 
    elem *front, *rear;
};
typedef struct queue queue;
void initialize(queue *q);
void enqueue(data d, queue *q);
data dequeue(queue *q);
data front(const queue *q);
boolean empty(const queue *q);
boolean full(const queue *q);

/* Queue.c */
#include "queue.h"
void initialize(queue *q){
     q->cnt =0;
     q->front = NULL;
     q->rear = NULL;
}
data dequeue(queue *q){
    data d;
    elem *p;
    d=q->front->d;
    p=q->front ;
    q->front=q->front ->next ;
    q->cnt --;
    free(p);
    return d;
}
void enqueue(data d, queue *q){
    elem *p;
    p=malloc(sizeof(elem));
    p->d = d;
    p->next = NULL;
    if( !empty(q)){
        q->rear ->next =p;
        q->rear =p;
    }
    else
        q->front =q->rear =p;
    q->cnt ++;
}
data front(const queue *q){
    return q->front->d ;
}
boolean empty(const queue *q){
    return ((boolean)(q->cnt == EMPTY));
}
boolean full(const queue *q){
    return ((boolean)(q->cnt == FULL));
}
/*  Main.c  */
#include "queue.h"
int main(void){
    int c;
    int cnta=0;
    int cntb=0;
    data pid;
    queue a,b;
    initialize(&a);
    initialize(&b);
    /* Enqueues the queuesets */
    while( (c=getchar()) != EOF){
        switch(c){
        case 'A':
            assert(scanf("%u",&pid) == 1);
            if( ! full(&a))
                enqueue(pid,&a);
            break;
        case 'B':
            assert(scanf("%u",&pid) == 1);
            if( ! full(&b))
                enqueue(pid,&b);
            break;
        }
    }
    /* Dequeue the requests and print them */
    printf("\n A's Schedule");
    while( ! empty(&a)){
        pid=dequeue(&a);
        printf("  Job %u is %d\n",++cnta,pid);
    }
   
    printf("\n B's Schedule");
    while( ! empty(&b)){
        pid=dequeue(&b);
        printf("  Job %u is %d\n",++cntb,pid);
    }
   
    return 0;
}
/******--Using Environment Variables..---***/

#include <stdio.h>
#include <stdlib.h>
int main(int c, char *a[], char *env[]){
     int i;
     for(i=0; i < c; i++)            
          puts(a[i]);   
          for(i=0; env[i] != NULL; i++)           
                  puts(env[i]);       
          printf("%s%s\n%s%s\n%s%s\n%s%s",                 
                        "Name :",getenv("NAME"),                  
                        "User :",getenv("USER"),                  
                        "Shell :",getenv("SHELL"),                  
                        "Home Directory:",getenv("HOME"));   
    return 0;

}

Dynamic Matrix.


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

typedef double ** matrix;
typedef double * row;
typedef double elem;

/*
matrix get_matrix_space(int m, int n){
         int i;

         elem *p;
         matrix a;
         p=malloc(n * n * sizeof(elem));
         a=malloc(m * sizeof (row));
         --a;
         for(i=1; i<=m; i++)

              a[i]=p+((i-1) * n) -1;
         return a;
}

void release_matrix(matrix a){
      elem *p; p=(elem*) a[1]+1;
      free (p);
}

*/

double **get_matrix_space(int r, int c){
       int i; double **a; a=malloc(r,sizeof(double *));
       --a; /* offset the pointer */
       for(i=1 i<=r; i++){
            a[i] = calloc(r,sizeof(double)); --a[i];
       }
       return a;
}

void release_matrix(double **a,int m){
      int i;
      for(i=1; i<=m; i++)
             free(a[i]+1);
      free(a+1);
}

void print_matrix(double **a, int r,int c){
       int i,j;
       for(i=0; i < r; i++){
            putchar('\n');
            for(j=0; j < c;j++)
                 printf("%5.2lf",a[i][j]);
       }
}

double trace(double **a, int n){
    int i;
    double sum=0.0;
    for(i=0; i < r; i++)
         sum += a[i][i];
    return sum;
}



A Binary Tree Progam in ANSI C
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef char DATA;

struct node{
DATA d;
struct node *left,*right;
};

typedef struct node NODE;
typedef NODE *BTREE;

void inorder(BTREE root);
void preorder(BTREE root);
void postorder(BTREE root);
BTREE new_node();
BTREE init_node(DATA d1,BTREE p1, BTREE p2);
BTREE create_trea(DATA a[], int i, int size);



#include "protos.h"


/*
inorder : Left subtree Preorder: root Postorder : left subtree
root left subtree right subtree
right subtree right subtree root
*/

void inorder(BTREE root){
if(root != NULL){
inorder(root->left);
printf(" %c ",root->d);
inorder(root->right);
}
}

void preorder(BTREE root){
if(root != NULL){
printf(" %c ",root->d);
preorder(root->left);
preorder(root->right);
}
}

void postorder(BTREE root){
if(root != NULL){
postorder(root->left);
postorder(root->right);
printf(" %c " , root->d);
}
}


BTREE new_node(){ /* created a new node*/
return malloc(sizeof(NODE));
}

BTREE init_node(DATA d1,BTREE p1, BTREE p2){
BTREE t;
t=new_node();
t->d=d1;
t->left=p1;
t->right=p2;
return t;
}

/* creates a linked binary tree from an array */
BTREE create_trea(DATA a[], int i, int size){
if(i >= size)
return NULL;
else
return init_node(a[i],create_trea(a,2*i+1,size),create_trea(a,2*i+2,size));
}



#include "protos.c"
int main(void){
char arr[]="a+b-c*d/e";
BTREE b;
b=create_tree(arr,0,9);
printf("\n Preorder \n");
preorder(b);
printf("\n Postorder \n");
postorder(b);
printf("\n Inorder \n");
inorder(b);
return 0;
}