Posts

Showing posts from June, 2012

Insertion Sort in C

#include <string.h> #include <stdio.h> #include <stdlib.h> void insert(char *items, int count) {   register int i, b;   char t;   for(i=1; i < count; ++i) {     t = items[i];     for(b=i-1; (b >= 0) && (t < items[b]); b--)       items[b+1] = items[b];    // items[b+1] = t;     items[b] = t;   } } int main(void) {   char s[255] = "asdfasdfasdfadsfadsf";   insert(s, strlen(s));   printf("The sorted string is: %s.\n", s);   return 0; }

Binary Tree in C

#include <stdlib.h> #include <stdio.h> struct tree {   char info;   struct tree *left;   struct tree *right; }; struct tree *root; /* first node in tree */ struct tree *stree(struct tree *root,                    struct tree *r, char info); void print_tree(struct tree *root, int l); int main(void) {   char s[80];   root = NULL;  /* initialize the root */   do {     printf("Enter a letter: ");     gets(s);     root = stree(root, root, *s);   } while(*s);   print_tree(root, 0);   return 0; } struct tree *stree(   struct tree *root,   struct tree *r,   char info) {   if(!r) {     r = (struct tree *) malloc(sizeof(struct tree));     if(!r) {       printf("Out of Memory\n");       exit(0);     }     r->left = NULL;     r->right = NULL;     r->info = info;     if(!root) return r; /* first entry */     if(info < root->info) root->left = r;     els

Circular Buffer in C

/* A circular queue example using a keyboard buffer. */ #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAX 80 char buf[MAX+1]; int spos = 0; int rpos = 0; void qstore(char q); char qretrieve(void); int main(void) {   register char ch;   int t;   buf[80] = '\0';   /* Input characters until a carriage return is typed. */   for(ch=' ',t=0; t<32000 && ch!='\r'; ++t) {     if(_kbhit()) {       ch = _getch();       qstore(ch);     }     printf("%d ", t);     if(ch == '\r') {       /* Display and empty the key buffer. */       printf("\n");       while((ch=qretrieve()) != '\0') printf("%c", ch);       printf("\n");     }   }   return 0; } /* Store characters in the queue. */ void qstore(char q) {   if(spos+1==rpos || (spos+1==MAX && !rpos)) {     printf("List Full\n&quo

Binary Search in C

CODE:1-BINARY SEARCH #include <stdio.h> #include <stdlib.h> int values[] = { 1 , 2 , 3, 4 , 9 , 10 }; int compare (const void * a, const void * b) {   return ( *(int*)a - *(int*)b ); } int main () {   int *pos;   int key = 9;      pos = (int*) bsearch (&key, values, 6, sizeof (int), compare);      if ( pos != NULL )     printf ("%d is in the array", *pos);   else     printf ("%d is not in the array", key);      return 0; }

Get System Date and Time Sample in C

//////////////////////SYSTEM DATE && TIME////////////// CODE:1- #include <time.h> #include <stdio.h> void main( ) {  char dbuffer [9];    char tbuffer [9];    _strdate( dbuffer );    printf( "The current date is %s \n", dbuffer );    _strtime( tbuffer );    printf( "The current time is %s \n", tbuffer ); } CODE:2-Find Date Ans Time As Per Your Recquirement #include <stdio.h> #include <time.h> int main () {   time_t rawtime;   struct tm * timeinfo;   char buffer [80];   time ( &rawtime );   timeinfo = localtime ( &rawtime );   strftime (buffer,80,"TIME_IS:::: %y%m%d%H%M",timeinfo);   puts (buffer);     return 0; }   / * (SPECIFIER)                              (REPLACED BY)                            ( EXAMPLE)  %a                                             Abbreviated weekday name              Thu  %A                                            Full weekday name                           Thu