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
Monday, April 2, 2012
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:
Wednesday, February 8, 2012
Convert Code to HTML - Perl
Convert the Code into HTML - Perl:
If you swap the substitutions statements are shuffled, you will not get the desired output.
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 :-)
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 :-)
Thursday, April 29, 2010
Trailing Zeros of a Factorial
0 /*
1 * Problem: To find the number of trailing zeros in a factorial (n)
2 * URL: http://www.codechef.com/problems/FCTRL/
3 * Input: n
4 * Output: No. of Trailing Zeroes
5 * Formula: E(sigma) limit (i=1..k) = [n/5^i]
6 * k is chosen such that 5^k+1 < n
7 * Execution Time: 1.49 (Alloted 8 Sec)
8 */
9
10 #include <iostream>
11
12 using namespace std;
13
14 int main() {
15 int n, trail_zeros;
16 long factor5, num;
17
18 cin>>n;
19 for (int i=0; i<n; i++) {
20 trail_zeros = 0;
21 factor5 = 5;
22 cin>>num;
23 while (factor5 <= num) {
24 trail_zeros += num / factor5;
25 factor5 *= 5;
26 }
27 cout<<trail_zeros<<endl;
28 trail_zeros = 0;
29 }
30 return 0;
31 }
32
The Program in bc (An arbitrary precision calculator language):
0 define f(x) {
1 if (x>1) {
2 return (x * f (x-1))
3 }
4 return (1)
5 }
6
7 define z(x) {
8 if (0 == x) return 0;
9 if (0 == x%10) {
10 return 1+z(x/10)
11 }
12 return 0
13 }
14
15 define trial_zero(x) {
16 return z(f(x))
17 }
18
Thursday, March 25, 2010
Programming Quotes
"Programming is art of expressing solutions to problems so that a computer can execute the solutions." - Bjarne Stroustroup
"Clearly, I reject the view that there is one way that is right for everyone and for every problem." - Bjarne Stroustrup
"Your Quote Here." - Bjarne Stroustrup (Templates Chapter - The C++ Programming Language Book).
"Clearly, I reject the view that there is one way that is right for everyone and for every problem." - Bjarne Stroustrup
"Your Quote Here." - Bjarne Stroustrup (Templates Chapter - The C++ Programming Language Book).
Wednesday, March 24, 2010
Makefile Explained - Part-1
Sorry for a long delay. I was held up tight in my previous project. Now, I have time to breathe, and enjoy the blue sky.
Let is say I have a file hello.cpp, now how we compile it:
c++ -o hello hello.cpp
if you have installed g++:
g++ -o hello hello.cpp
Generic format:
{compiler name} -o {output file} {source file}
-o - option to specify output filename. If we don't specify {output file}, by default the output will be written to "a.out" file.
"a.out" - assembler output
A Simple Makefile:
hello: hello.cpp
{tabspace}g++ -o hello hello.cpp
Output:
$make hello
g++ -o hello hello.cpp
$
Look at the output, when I give "make hello" command, the make command searches the "hello" target in the makefile and executes the command given for "hello" target.
This makefile make our life easier, we don't need to type "g++ -o hello hello.cpp" everytime, instead we can just give "make hello" which will take care of compiling it.
In my next post, we will see how Make utility can make our life more easier.
Let is say I have a file hello.cpp, now how we compile it:
c++ -o hello hello.cpp
if you have installed g++:
g++ -o hello hello.cpp
Generic format:
{compiler name} -o {output file} {source file}
-o - option to specify output filename. If we don't specify {output file}, by default the output will be written to "a.out" file.
"a.out" - assembler output
A Simple Makefile:
hello: hello.cpp
{tabspace}g++ -o hello hello.cpp
Output:
$make hello
g++ -o hello hello.cpp
$
Look at the output, when I give "make hello" command, the make command searches the "hello" target in the makefile and executes the command given for "hello" target.
This makefile make our life easier, we don't need to type "g++ -o hello hello.cpp" everytime, instead we can just give "make hello" which will take care of compiling it.
In my next post, we will see how Make utility can make our life more easier.
Tuesday, February 2, 2010
Pthread Part-1
A Simple Pthread Program:
Output:
0 #include <stdio.h>
1 #include <unistd.h>
2 #include <pthread.h>
3
4 void *fun(void *args) {
5 int i;
6
7 for (i=0; i<10; i++) {
8 printf("hello world\n");
9 sleep(1);
10 }
11 pthread_exit(NULL);
12 }
13
14 int main() {
15 pthread_t pt_id;
16
17 printf("Main: Before Create\n");
18 pthread_create(&pt_id, NULL, fun, NULL);
19 printf("Main: After Create\n");
20 pthread_join(pt_id, NULL);
21 printf("Main: After Join\n");
22 pthread_exit(NULL);
23 return 0;
24 }
25
Output:
$./pthread1
Main: Before Create
Main: After Create
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Main: After Join
$
Subscribe to:
Posts (Atom)