implementing malloc in C
12 Febimplement memcpy – C
20 Janoutput :
src : I am no superman!
dst : I am no superman!
implement 2D array dynamically in C
20 Jan}
for(i = 0; i < n; i++){
free(a[i]);
}
free(a);
}
remove duplicates from list of integers – C
17 Janfor(i = 0; i < len; i ++ ){
printHash(&hashMap[i]);
}
printf(“\n”);
}
void printHash(struct node** elem){
while((*elem) != NULL){
printf(” %d “,(*elem)->data);
(*elem) = (*elem)->next;
}
}
int doHash(int num){
int a = 1;
int b = 2;
int n = 5;
int ret = 0;
/* we will use hash function as h(x) = (ax + b)mod n */
ret = (a * num + b) % n;
return ret;
}
void fixInHash(int value, struct node** elem) {
struct node* current = NULL;
current = (struct node*) malloc(sizeof(struct node));
int flag = 0;
//printf(“value : %d\n”,value);
if( *elem == NULL){
binary search – c
17 Janprintf(“mid – found at index : %d\n”,mid);
exit(0);
}
if(num < a[mid]){
//printf(“less: %d – %d\n”, start, (mid-1));
doBinarySearch(a, start, (mid -1), num);
}
if(num > a[mid]){
//printf(“greater : %d – %d\n”, (mid+1),len);
doBinarySearch(a, (mid + 1), len, num);
}
}
sort array of 0s and 1s
17 JanThis is O(N) solution
permute the string – C program
15 Janint permuteString(char []);void doPermute(char [], char [], int [], int , int );
main() {
char string[] = “sun”; int stat = permuteString(string);
}
int permuteString(char inString[]) {
int length,i, *used; char *out;
length = strlen(inString);
out = (char *)malloc(length + 1); if(!out) { return 0; }
/* so that printf doesn’t run past the end of buffer */ out[length] = ”; used = (int *)malloc(sizeof(int) * length);
if(!used) { return 0; }
/* start with no letters used, so zero array */ for( i = 0; i < length; i ++) { used[i] = 0; }
doPermute(inString, out, used, length, 0);
free(out);
free(used);
return 1; //success
}
void doPermute(char in[], char out[], int used[], int length, int recurLevel ) {
int i ;
/* base case */
if(recurLevel == length ) {
printf(“%s\n”, out);
return;
}
/* recursive case */
for(i = 0; i < length ; i ++) {
if(used[i]) {
continue;
}
out[recurLevel] = in[i]; /* put current letter in output */
used[i] = 1; /* mark letter as used */
doPermute(in, out, used, length, recurLevel + 1);
used[i] = 0; /* unmark this letter */
}
}
Build linkedlist at tail/end – C
14 JanThere are two approaches given in the solution – using addNode() and addNodeWithDummy()
Appending nodes in linkedlist – C program
14 JanInteger/String conversions in C
14 Jana.) Given an string you have to convert the string into integer. The following program does that for you
