Skip to content

Instantly share code, notes, and snippets.

View rohit-nsit08's full-sized avatar

rohit jangid rohit-nsit08

View GitHub Profile
@rohit-nsit08
rohit-nsit08 / stringrev.c
Created August 10, 2011 13:08
string reverse c code
#include<stdio.h>
#include<string.h>
char s[] = "rohit";
void reverse(int);
int main()
{
printf("%s",s);
reverse(0);
printf("\n");
printf("%s",s);
@rohit-nsit08
rohit-nsit08 / treedepth.c
Created August 10, 2011 15:26
calculate the depth of a binary tree
void finddepth(Node *node,int depth){
if(node!=NULL){
depth=max(finddepth(node->left,depth+1), finddepth(node->right,depth+1) );
}
return depth;
}
On Mon
@rohit-nsit08
rohit-nsit08 / common_ancestor.c
Created August 11, 2011 19:00
given pointer to two nodes in a tree, find their common ancestor.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
struct node* left;
struct node* right;
int value;
}treenode;
int add_node(treenode**tree, int value);
@rohit-nsit08
rohit-nsit08 / paragraph_edit.c
Created August 12, 2011 16:58
deletes the extra space from the paragraph
#include <stdio.h>
#include <string.h>
char *s = "welcome to linux shell !";
char t[100];
//output = welcome to linux shell !
int main()
{
int i,j,flag=0;
@rohit-nsit08
rohit-nsit08 / permute.c
Created August 12, 2011 19:17
permutes the given string
#include <stdio.h>
#include <string.h>
void swap(char*a,char*b)
{
char t;
t = *a;
*a=*b;
*b=t;
}
void permute(char* s, int i, int n)
@rohit-nsit08
rohit-nsit08 / hash.c
Created August 13, 2011 03:32
hashing demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_TABLE_SIZE 100
#define HASH_TABLE_DELETE_ELEMENT_CHECK_FOR_ERRORS(t, n)
if (hash_table_delete_element(t, n) == 0)
hash_table_fatal_error(0, n)
@rohit-nsit08
rohit-nsit08 / dynamic_hop.c
Created August 13, 2011 14:37
hopping using dynamic programming
#include<stdio.h>
int main()
{
int input[] = {1, 3, 5 ,8 ,9 ,2 ,6, 7, 6, 8, 9};
int n = sizeof(input)/sizeof(int);
int dp[n];
int i,j,min,value,x;
dp[n-1] = 0;
for(i=n-2;i>=0;i--)
@rohit-nsit08
rohit-nsit08 / hopgreedy.c
Created August 13, 2011 15:58
hopping using greedy approach
#include<stdio.h>
int main()
{
int arr[] = {1, 3, 5, 8, 9, 1,1,2, 6, 7, 6, 8};
int n = sizeof(arr)/sizeof(int);
int i,j,step=0,jump=0,choice,max,val;
for(i=0;i<n;)
{
choice = arr[i];
@rohit-nsit08
rohit-nsit08 / inpretree.c
Created August 30, 2011 12:45
tree from inorder and preorder traversal
#include<stdio.h>
#include<stdlib.h>
char in[]= {'d','b','e','a','f','c','g'};
char pre[]={'a','b','d','e','c','f','g'};
int size = sizeof(in);
int pos = 0;
typedef struct node{
char value;
@rohit-nsit08
rohit-nsit08 / queens8.c
Created August 31, 2011 14:51
8 queens problem using backtracking
#include<stdio.h>
#include<stdlib.h>
int t[8] = {-1};
int sol = 1;
void printsol()
{
int i,j;
char crossboard[8][8];
for(i=0;i<8;i++)