As of writing this (March 31, 2014), the version of Boost included in Debian Wheezy is 1.49. This is a bit problematic for me, as some very useful things are included in more recent versions (e.g., better syslog event handlers in the logging library). One of the problems I sometimes face with Boost is in remembering how to compile/install it. The Boost “Getting Started” pages basically say that most of Boost is header-only and doesn’t need to be compiled, but that there are 16 libraries that need to be compiled. Unfortunately, it stops there. So, this is for future reference and for others that may find it useful. How does one compile/install all of Boost?

The first thing we need to do is make sure all the dependencies for Boost are available. Using your favorite package manager, make sure you have the following. (Depending on your distro, they may be named slightly differently. But, make sure you have the dev version when required.)

- build-essential
- g++
- python-dev
- autotools-dev
- libicu-dev
- libbz2-dev

Next, download and decompress the Boost version that you’d like. I’m using 1.55.

$ wget http://downloads.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.gz
$ gunzip boost_1_55_0.tar.gz
$ tar xvf boost_1_55_0.tar

Finally, as root, build and install. Since I’m placing the installation in /usr/local (as is the default), I’m not issuing any prefix declaration here. There are really just two commands:

(root) $ ./bootstrap.sh
(root) $ ./b2 --with=all -j 2 install

The -j n option will build the libraries using n threads, so this can be changed based on your system. After completion, you should be able to include and link against the required headers and libraries.h


The following code creates a linked list with 20 random elements, and then reverses the list.

/*
reverse-linked-list.cpp
Jason B. Hill (jason@jasonbhill.com)
reverse a linked list in C++
*/
 
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
// Node class
class Node {
 
    public:
    int         data;
    Node*       next;
 
    // create a new node
    Node(int x, Node* addr) {
        data = x;
        next = addr;
    }
};
 
// LinkedList class
class LinkedList {
    private:
    Node*       head;
 
    public:
 
    // initialize a linked list with a NULL head
    LinkedList() {
        head = NULL;
    }
 
    // determine if a list is empty
    bool is_empty() {
        if(head == NULL) return 1;
        else return 0;
    }
 
    // add an item to a linked list
    void node_add(int val) {
        if(head == NULL) head = new Node(val, NULL);
        else {
            Node* n = head;
            while(n->next != NULL) n = n->next;
            n->next = new Node(val, NULL);
        }
    }
 
    // return the length of a linked list
    unsigned int length() {
        if(head == NULL) return 0;
        if(head->next == NULL) return 1;
        unsigned int len = 1;
        Node* n = head;
        while(n->next != NULL) {
            n = n->next;
            len++;
        }
        return len;
    }
 
    // print a linked list
    void print() {
        cout<<"[";
        if(head != NULL) {
            if(head->next == NULL) cout<<head->data;
            else {
                Node* n = head;
                while(n->next != NULL) {
                    cout<<n->data<<",";
                    n = n->next;
                }
                cout<<n->data;
            }
        }
        cout<<"]\n";
    }
 
    //reverse a linked list
    void reverse() {
        if(head != NULL and head->next != NULL) {
            Node* m = head;
            Node* n = head->next;
            Node* t = NULL;
            if(n->next != NULL) t = n->next;
            head->next = NULL;
            while(t->next != NULL) {
                n->next = m;
                m = n;
                n = t;
                t = t->next;
            }
            n->next = m;
            head = t;
            t->next = n;
        }
    }
};
 
int main(void){
 
    LinkedList      L;
    unsigned int    i;
 
    srand(time(NULL)); 
    for(i=0;i<20;i++) L.node_add(rand() % 100);
 
    L.print();
 
    L.reverse();
 
    L.print();
 
 
    return(0);
}

This can be compiled (using gcc/g++) using:

g++ reverse-linked-list.cpp -o reverse-linked-list

An example execution is:

$ ./reverse-linked-list 
[99,31,17,41,7,38,59,7,67,75,75,13,62,70,0,20,46,3,14,24]
[24,14,3,46,20,0,70,62,13,75,75,67,7,59,38,7,41,17,31,99]
Posted in C++