Wednesday, December 9, 2009

C Dictionary

#include

struct node
{
char data;
struct node* next;
int numChildren;
char isTerminal;
};

int main()
{
struct node* aroot = malloc(sizeof(struct node));
struct node* tmp;
int i = 0;
aroot->data = 'a';
aroot->isTerminal = 1;
aroot->numChildren = 0;
aroot->next = malloc(sizeof(struct node));
++(aroot->numChildren);
aroot->next->data = 'n';
aroot->next->isTerminal = 1;
aroot->next->numChildren = 0;
tmp = (struct node*) realloc(aroot->next,aroot->numChildren + 1);
if(tmp)
aroot->next = tmp;
++(aroot->numChildren);
(aroot->next[1]).data = 'm';
(aroot->next[1]).isTerminal = 0;
//printf("%c\n",(aroot->next[0]).data);
//printf("%c\n",(aroot->next[1]).data);
for(i=0;inumChildren;i++)
{
//if(aroot->next[i].isTerminal)
printf("%c\n",(aroot->next[i]).data);
}
}

A whiteboard built using Flex/PHP/MySql

Here.

Maximum contiguous subarray problem

View and Edit here.
Copied verbatim from here.

Sunday, December 6, 2009

Grep for space and new line

I had to grep the following pattern followed by a new line :
"belongs to "
so it is
grep belongs[[:space:]]to[[:space:]]*$

I had to use '*' as I guess there are more characters than
space at the end.

Friday, December 4, 2009

Quick Sort in C by Kernighan Ritchie

void pArr(int v[],int left,int right)
{
int i;
for(i=left;i<=right;i++) printf("%d ",v[i]); printf("\n"); } void swap(int v[],int i,int j) { int tmp; tmp= v[i]; v[i] = v[j]; v[j] = tmp; } void qsort(int v[],int left,int right) { int i,last; pArr(v,left,right); if (left >= right)
return;
swap(v,left,(left+right)/2);
last = left;
for(i = left + 1;i<=right; i++)
if(v[i] < v[left])
swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last - 1);
qsort(v,last + 1,right);
}

int main()
{

int a[8] = {1,5,8,11,3,4,2,3};
pArr(a,0,7);
qsort(a,0,7);
pArr(a,0,7);
}

A simple hash implementation in C

#include
#include

struct node{
int key;
char *value;
struct node* next;
};

struct node* hash[5];

char* m_strdup(char* tmp)
{
int len = strlen(tmp);
char* newmem = malloc(sizeof(char)*len);
if(!newmem)
{
fprintf(stderr,"problem in memory allocation");
exit(0);
}
strcpy(newmem,tmp);
return newmem;
}

void traverse(struct node* root)
{
printf("traversing\n");
while(root)
{
printf("%s",root->value);
root = root->next;
}
printf("\n");
}
void addnew(int value)
{
int key = value%5;
char tmp[10];
int counter = 0;
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr;
if(!hash[key])
{
hash[key] = malloc(sizeof(struct node));
hash[key]->value = m_strdup(tmp);
hash[key]->next = NULL;
printf("added at %d - %d\n",key,counter);
}
else
{
curr = hash[key];
while(hash[key]->next)
{
hash[key] = hash[key]->next;
counter++;
}
hash[key]->next = malloc(sizeof(struct node));
hash[key]->next->value = m_strdup(tmp);
hash[key]->next->next = NULL;
hash[key] = curr;
printf("added at %d - %d\n",key,counter);
}
}

void lookup(int value)
{
int key = value%5;
int counter = 0;
char tmp[10];
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr = hash[key];
while(curr)
{
if(strcmp(curr->value,tmp)==0)
{
printf("found at %d - %d\n",key,counter);
break;
}
curr = curr->next;
counter++;
}
//hash[key] = curr;
}

int main()
{
int i;
for (i=0;i<5;i++)
hash[i] = NULL;
addnew(1);
addnew(2);
addnew(22);
addnew(12);
addnew(3);
addnew(13);
traverse(hash[3]);
lookup(1);
lookup(2);
lookup(3);
lookup(22);
lookup(12);
lookup(13);
}

Blog Archive