A singly linked list with reverse…

In (not particularly beautiful) C++, both recursive and non-recursive reverse methods:

#include <iostream>
using namespace std;

class ListItem {

public:

  ListItem() : next(NULL)
  {}

  char data[100];
  ListItem *next;

  void reverse(ListItem *p) {
    if(next == NULL) {
      next = p;
    } else {
      next->reverse(this);
      next = p;
    }
  }
};

class LinkedList {

public:

  LinkedList() {}

  ListItem *start;
  ListItem *end;

  ListItem *push_back(char *data) {
  

    ListItem *i = new ListItem();

    if(end != NULL) { end->next = i; }
               else { start = i; }

    for(size_t n=0;n<100;n++) {i->data[n] = data[n];}

    end = i;
    return i;
  }

  void reverse() {
    start->reverse(NULL);
    ListItem *temp = end;
    end = start;
    start = temp;
  }

  void reverse_nonrec() {
  
    ListItem *last=NULL;
    for(ListItem *p=start;p!=NULL;) {
      ListItem *next = p->next;

      p->next = last;
      last = p;
      p = next;
    }

    ListItem *temp = end;
    end = start;
    start = temp;
  }

  void dump() {
    for(ListItem *p=start;p!=NULL;p=p->next) {
      cout << "data: " << p->data << endl;
    }
  }

  ~LinkedList() {
    ListItem *op = NULL;
    for(ListItem *p=start;p!=NULL;p=p->next) {
      if(op != NULL) delete op;
      op = p;
    }
    if(op != NULL) delete op;
  }

};

int main() {

  LinkedList mylist;

  char tempdata[100];
  for(size_t n=0;n<100;n++) { tempdata[n] = 'A'; }
  tempdata[99] = 0;
  for(size_t n=0;n<20;n++) {
    mylist.push_back(tempdata);
    for(size_t n=0;n<99;n++) { tempdata[n]++; }
  }

  mylist.dump();
  mylist.reverse();

  cout << "Reversed List" << endl;
  mylist.dump();

  cout << "Reverse again" << endl;
  mylist.reverse_nonrec();
  mylist.dump();

  
}

Generating Permutations in C++

#include <vector>
#include <iostream>

using namespace std;

void reverse(vector<int> &seq,int n1,int n2) {

  for(;(n2-n1)>=1;) {
    int temp = seq[n1]; seq[n1] = seq[n2]; seq[n2]=temp;
    n1++;
    n2--;
  }

}

int main() {

  int seq_len=4;

  vector<int> seq;
  for(int n=0;n<seq_len;n++) {seq.push_back(n);}
  for(int n=0;n<seq.size();n++) cout << seq[n] << " ";
  cout << endl;

  for(int n=0;n<seq_len;n++) {

    for(bool final=false;final==false;) {

      final=true;
      int k=0;

      for(int n=0;n<(seq.size()-1);n++) {
        if(seq[n] < seq[n+1]) {k=n; final=false;}
      }

      if(!final) {
        int l;
        for(int n=k+1;n<seq.size();n++) {
          if(seq[k] < seq[n]) {
            l=n;
          }
        }

        int temp = seq[k]; seq[k] = seq[l]; seq[l] = temp;
        reverse(seq,k+1,seq.size()-1);
        for(int n=0;n<seq.size();n++) cout << seq[n] << " ";
        cout << endl;
      }
    }
  }
}

Modifying shellinabox to use Gnu Unifont or Profont

shellinabox is a web based terminal client, it’s neat and kind of useful for remote access. However, I don’t much like the default fonts (which are limited by want is available to your browser). I therefore modified it to use the Unifont WOFF I previously generated.

It /kind of/ works. The spacing is still off, and the Unifont WOFF only seems to work in Safari at the moment (it is kind of big!). Anyway, moving to Unifont resolved some of the Kanji rendering artifacts I was having:

You can see that the Hanzi in the first example inserts an extra whitespace above the line. In the second example the Hanzi throws the (s out of alignment, due to the Hanzi width being different than the normal text.

This is the version of shellinthebox modified to use Unifont. As you can see it keeps everything nicely in alignment. There’s also no /extra/ white horizontal line. However, every line now gets a whitespace between it, I need to figure out the css padding issues that are causing this.

You can find my shellinthebox fork on github here.

Gnu Unifont WOFF

Today I discovered Gnu Unifont. It’s a really cool unicode bitmap font, with a high level of unicode coverage. There was no WOFF version available so I made one using fontforge. The version of fontforge in debian stable doesn’t have WOFF support so it was a build from source job, it’s worth noting that you need libpng-dev and libz-dev installed for WOFF support to be compiled in (otherwise it’s greyed out in the “generate fonts” menu).

You can download my WOFF here. And see it in action:


UNIFONT DEMO


However, it only seems to work in Safari… which is a shame. 🙁