Thursday, November 8, 2012

Who Am I and Who Is My Parrent

An Unix Process got created, but it know its name (its ID) and who created it. How will is know its ID ?

In Unix Operating System, a process is a living entity.A process is a program in execution.

How does a Process knows its ID, also know as Process-ID. 

[incomplete]

Thursday, April 5, 2012

Reverse Linked List - Recursion - with code



#include <iostream>

using namespace std;

struct node {
int data;
struct node *next;
};

typedef struct node NODE;

class linklist {
private:
NODE *head;
public:
linklist():head(NULL) {}
void push(int);
void display();
void reverse();
NODE* reverse_driver(NODE*, NODE*);
};

void linklist::push(int data) {
NODE *tmp = new NODE;
tmp->data = data;
tmp->next = NULL;

if (head == NULL) {
head = tmp;
} else {
NODE *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = tmp;
}
}

void linklist::display() {
NODE *curr = head;
cout<<"HEAD: ";
while (curr != NULL) {
cout<<curr->data<<"->";
curr = curr->next;
}
cout<<"NULL\n";
}

void linklist::reverse() {
if (head != NULL) {
NODE *last = reverse_driver(head, head->next);
// make the last_node->next to NULL
head->next = NULL;

// change the head to the last
head = last;
}
}

NODE* linklist::reverse_driver(NODE *currnode, NODE *nextnode) {
if (nextnode != NULL) {
NODE *last = reverse_driver(nextnode, nextnode->next);
nextnode->next = currnode;
return last;
} else {
// Last Node - to be set as Head node
return currnode;
}
}

int main () {
linklist ll;

ll.push(10);
ll.push(20);
ll.push(30);
ll.push(40);

ll.display();
ll.reverse();
ll.display();
return 0;
}

Monday, April 2, 2012

The 2-Key Case

About 10 and a half years back, when I was an summer student in IMSC (Institute of Mathematical Science) my mentor(Dr. Venkatesh Raman) asked a question to ponder during my first weekend in the institute and let him know how to solve it.

Before I go into the problem: I will give a background for it.

Predominately, there are 2 problem in Computer Science: Searching and Sorting. "How to do it efficiently ?"

"implicit data structures" - these data structures uses very little or no extra spaces other than the "data elements".

For example, "linked list" which use 1 pointer per "data elements" will amount to the "Space Complexity" of SizeOf(data_elements) + ((Number of data_elements) * SizeOf(Pointer)).

ie.. if the input is N integers, the "Space Complexity" of the integer linked list = N + N (pointers for each integer)
= 2*N

The Space Complexity of a Binary Search Tree (BST) = N + 2 N (2 pointers for each data_element)
= 3*N

But, a Sorted Array is an "Implicit Data Structure", it does not store any extra space. Whereas if you take the "Time Complexity" of both BST and Sorted Array for searching an element in the Collection, it takes same time which is lg(N).

lg = log N to the base 2

Reverse Linked List - Recursion

2 weeks back I was asking someone to write a code for Reversing a Linked List. I just tried one with Recursion:

  0 #include <iostream>
1
2 using namespace std;
3
4 struct node {
5 int data;
6 struct node *next;
7 };
8
9 typedef struct node NODE;
10
11 class linklist {
12 private:
13 NODE *head;
14 public:
15 linklist():head(NULL) {}
16 void push(int);
17 void display();
18 void reverse();
19 NODE* reverse_driver(NODE*, NODE*);
20 };
21
22 void linklist::push(int data) {
23 NODE *tmp = new NODE;
24 tmp->data = data;
25 tmp->next = NULL;
26
27 if (head == NULL) {
28 head = tmp;
29 } else {
30 NODE *curr = head;
31 while (curr->next != NULL) {
32 curr = curr->next;
33 }
34 curr->next = tmp;
35 }
36 }
37
38 void linklist::display() {
39 NODE *curr = head;
40 cout<<"HEAD: ";
41 while (curr != NULL) {
42 cout<<curr->data<<"->";
43 curr = curr->next;
44 }
45 cout<<"NULL\n";
46 }
47
48 void linklist::reverse() {
49 if (head != NULL) {
50 NODE *last = reverse_driver(head, head->next);
51 // make the last_node->next to NULL
52 head->next = NULL;
53
54 // change the head to the last
55 head = last;
56 }
57 }
58
59 NODE* linklist::reverse_driver(NODE *currnode, NODE *nextnode) {
60 if (nextnode != NULL) {
61 NODE *last = reverse_driver(nextnode, nextnode->next);
62 nextnode->next = currnode;
63 return last;
64 } else {
65 // Last Node - to be set as Head node
66 return currnode;
67 }
68 }
69
70 int main () {
71 linklist ll;
72
73 ll.push(10);
74 ll.push(20);
75 ll.push(30);
76 ll.push(40);
77
78 ll.display();
79 ll.reverse();
80 ll.display();
81 return 0;
82 }
83

Wednesday, February 8, 2012

Convert Code to HTML - Perl

Convert the Code into HTML - Perl:
  0 #! /usr/bin/perl
1 use warnings;
2 use strict;
3
4 my $lineno=0;
5
6 print "<pre>";
7 foreach my $strline (<STDIN>) {
8 print "<a name=\"line$lineno\">";
9 printf "%3d", $lineno;
10 print "</a>";
11 print " <font color=\"#0000FF\">";
12 chomp($strline);
13 $strline =~ s/&/&amp/g;
14 $strline =~ s/</&lt/g;
15 $strline =~ s/>/&gt/g;
16 $strline =~ s/\t/ /g;
17 print $strline;
18 print "</font>";
19 print "<br>";
20 $lineno++;
21 }
22
23 print "</pre>\n";
24


If you swap the substitutions statements are shuffled, you will not get the desired output.

Monday, January 23, 2012

Bignum For C++

Sometime back I was solving problems from Project Euler(http://projecteuler.net/). I was using an array of tools like Sage, Mathematica, bc (arbitrary precision calculator - yet powerful)..

But, most of the problems needs a bit of computation(programming). So, I started searching for a BigNum library for my favorite programming language C++. Eventually, I found one GMP (GNU Multi Precision) library. Later on I found that GMP is one of the fastest BigNum library available and it is also the backbone for most of the tools I was using for solving Euler problems.

GMP library can be got from http://gmplib.org/. If you need C++ extension for the GMP library, you configure it with the following command:

./configure --prefix=/usr/local --enable-cxx

This command will create 2 library files (libgmp.so and libgmpxx.so) and copy it to the /usr/local/lib location.

Don't forget to add the path to your LD_LIBRARY_PATH. Enjoy BigNUm computing :-)