Linked list in C++ with pointer to pointer

I used to write linked list code in recursive form so that I don’t need to use dummy header node or something like that. This gives me a clear linked list with any extra nodes in it. Of cause, it is in recursive form thus there a memory and time overhead associated with it. The following is the example code.

#include <iostream>

using namespace std;
struct NODE {
    int value;
    NODE *next;
} *root;

void printList (NODE *root) {
    while (root != NULL) {
        cout << root->value << ' ';
        root = root->next;
    cout << endl;

void addNode (NODE **root, int value) {
    if ((*root)!=NULL && (*root)->value < value) {
        addNode (&((*root)->next), value);
    else {
        NODE *tmp;
        tmp = new NODE;
        tmp->value = value;
        tmp->next = *root;
        *root = tmp;

int main() {
    srand (time(NULL));
    for (int i=0; i<500; i++)
        addNode (&root, rand() % 1000);

    return 0;

Once you understand what the pointer to pointer **root is about then the whole program is not much special.

Last week, I had a chance to write code which required the use of linked list again. I suddenly about whether it can be converted to a non-recursive form which is something that did not come to my mind before. It turned out that the non-recursive form is actually very easy to write.

// Non recursive version
void addNode (NODE **root, int value) {
    while ((*root)!=NULL && (*root)->value < value) {
        root = &((*root)->next);
    NODE *tmp;
    tmp = new NODE;
    tmp->value = value;
    tmp->next = *root;
    *root = tmp;

Adding subtitle to mp4 movie with ffmpeg

I need to add a short copyright message at the beginning and ending of a short movie. So, I think it will be great to use ffmpeg for this simple task. Well, it turned out to be not as simple as I initially expected.

Running the initial simple command (show below) taken from somewhere in WWW result in error in reading the .srt file.

ffmpeg -i input-dv.mp4 -vf output-dv.mp4

Initially, I have my .srt setup as follows:

The movie should only be used in ...

Since the error message said it “cannot read” the file, I tried to change its name, adding fullpath, etc. Nothing worked. I also tried to change the contain. And finally, the following works. See the difference? I missed the spaces sounding “–>”!

00:00:00,000 --> 00:00:10,000
The movie should only be used in ...

Now the program run and produced a result mp4 file. However, when I viewed the movie, there was no subtitle! After reviewing the screen full of messages displayed during the conversion, I found there was a warning message about the missing fonts.conf file. Another round of information digging began.

This time, luckily, not before long, I came across a post ( where it provided most information about setting up the fonts.conf file.

Here are steps I took:

1. Download the fonts.conf from the above post and put it in the root directory of ffmpeg which, in my case, is D:\WinApps\ffmpeg

2. Setup the envoironment variables as follows:

set FC_CONFIG_DIR=d:\WinApp\ffmpeg
set FONTCONFIG_FILE=fonts.conf
set FONTCONFIG_PATH=d:\WinApp\ffmpeg

3. Run the following command

ffmpeg -i input-dv.mp4 -vf'Fontsize=18' output-dv.mp4

The first time I run the command, it took a bit longer to process.

The final problem is adding Chinese in the subtitle text. This turned out to be the most simple one to be solved. Just make sure the file encoding is in UTF-8. For this, I use the feature provided notepad directly.

“int RECV_PIN = TKD2;” error in Arduino IR library

Ok, it has been a long time since my last play-around with Arduino. This time, I tried to build a simple IR controller to open and close a window shader that I am planing to build. So I copy a sample program and test it out and get the error “TKD2 not defined” error in the following line of code in the file IRremoteTools.cpp which is part of the IRremote library.

int RECV_PIN = TKD2; // the pin the IR receiver is connected to

The simplest solution for this problem turned out to be just delete the two files IRremoteTools.cpp and IRremoteTools.h from the library directory. These two files just provide some additional function interface to the library which I am quite happy to live without them.

Allowing a local WiFi router to connect to the Internet through a wire LAN

I recently need to set up a WiFi router in my room where I have a computer that connects to a wired network with 802.1x authentication. The network is connected to the internet. Since the WiFi router cannot directly connect to a network with 802.1x authentication, the router is connected to the computer (running Ubuntu 16.04.1 LTS) instead. The following is the code, allowing the traffic from the WiFi router through my computer connect to the network with Internet capability.

# Perform NAT so that the WiFi route can connect to the Internet
# eth0 connect to a network which can connect to the Internet
# eth1 connect to LAN through WiFi router
# code taken from
echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

Error 1935 while try to upgrade blue tooth drive for lenovo X201

I recently tried to test my blue tooth stereo headset with my lenovo X201 and end up need to update the blue tooth driver in the computer. However, during the update process, the computer encountered an error 1935 and the update was not successful and rolled back. I tried a few times with the same result. I even tried to download and install the new driver manually, still without any luck. Reading the forums from web also gave me not much help. I finally do the following and magically it worked!

  1. In “Device Manager” (right-click Computer->property), right click the ThinkPad Bluetooth device and click “Uninstall”. The entire BT device will gone.
  2. In “Device Manager”, right click the top item (my notebook) and click “Scan for hardware changes”, wait a short while and it should find the BT device and start installing the most updated drivers!

Now, the device drivers are the most updated one!

Finding bit rate

I recently have a need to down-size of mp4 file in order to be able to upload it to a web page. The upload limit of the web page is 32M where the size of my mp4 file is about 200+M. I found out that I can use ffmpeg to re-compress the file with a smaller bit rate in order to down-size the file. The only problem is what bit rate to use. The command I used is as follows:

ffmpeg -i original.mp4 -b:v 126k -b:a 64k output.mp4

As you can see, I can control both the video bit rate and audio bit rate separately. After a few tests I eventually find our how to do the math. Given the length of my video is 22:49. The total bit rate is calculated as follows:

Total bit rate = (target file size / duration) * 8
               = (32 * 1024 * 1024) / (22 * 60 + 49) * 8
               = 196081
               = 191k

In other words, my maximum bit rate should be 191k, this is the combined total bit rate for both video and audio. Thus, I can select different combination of video and audio bit rate as long as they sum is within 191k. In the above example, I use 126k for video and 64k of audio, the result file is 33,414,347 bytes (of 31.9M) long. Good enough for me.

Web page template with Ajax

In modern web page design, we usually use some kind template system to make all pages to have the same layout and/or menu structure.  The most common way is to use PHP or some kind of server-side script to format your pages.  This is great if you have server-side scripting capability.  For site without server-side scripting capability, Ajax may be used.  The common way to use Ajax is to have template as the main page and use Ajax to load different page content in the web site.   This is good except that it posts problem for the search engine to index the content of your site.  Also, browser without Javascript enabled will not be able to view your site.  My solution for this is simple.

Continue reading “Web page template with Ajax”