Re-usable Code: Linked Lists

October 28, 2006

One of the most useful ways to store data is the linked list.

Read on and explore a very basic framework that you can use to easily add them to your program.

We begin with the h file. Please note, this code is written with wxwidgets in mind but the only change required to use this code elsewhere will be the changing of the variable “wxString” to something else.

Because this is only a sample code snippet for training purposes, the two variables “Name and Value” will not carry over to your own program.

/////////////////////////////////////////////////////////////////////////////
// Name:        hmtkdataitem.h
// Purpose:     hmtk data item function
// Author:      Stephen De Chellis
// Modified by:
// Created:     04/09/05
// Copyright:   (c) Stephen De Chellis
// License:     Creative Commons Attribution-ShareAlike 2.5 License
/////////////////////////////////////////////////////////////////////////////

#ifndef __HMTKDATAITEM_H__
#define __HMTKDATAITEM_H__

class HMTKDataItem
{
    public:
        static HMTKDataItem* pHead;
               HMTKDataItem* pNext;
               wxString      Name;
               int           Value;

        HMTKDataItem();
        ~HMTKDataItem();

        void          AddHead(HMTKDataItem* DI);
        void          AddTail(HMTKDataItem* DI);
        int           Remove (HMTKDataItem* DI);
        void          Clear  (void);
        HMTKDataItem* Find   (wxString Search);

};

#endif

Now let us look at the cpp file

/////////////////////////////////////////////////////////////////////////////
// Name:        hmtkdataitem.cpp
// Purpose:     hmtk data item function
// Author:      Stephen De Chellis
// Modified by:
// Created:     04/09/05
// Copyright:   (c) Stephen De Chellis
// License:     Creative Commons Attribution-ShareAlike 2.5 License
/////////////////////////////////////////////////////////////////////////////

#include “precomp.h”
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “hmtkdataitem.h”

HMTKDataItem* HMTKDataItem::pHead = 0;
using namespace std;

HMTKDataItem::HMTKDataItem()
{
    Name.Clear();
    Value=0;
    pNext=0;
}
HMTKDataItem::~HMTKDataItem()
{
}

void HMTKDataItem::AddHead(HMTKDataItem* DI)
{
    DI->pNext = pHead;
    pHead = DI;
}

void HMTKDataItem::AddTail(HMTKDataItem* DI)
{
    cout << “Adding Tail” << endl;
    DI->pNext = (HMTKDataItem*)0;
    if (pHead == 0)
    {
        pHead = DI;
        return;
    }
    HMTKDataItem* pCurrent = pHead;
    cout << “Entering while loop” << endl;
    while(pCurrent->pNext)
    {
        pCurrent = pCurrent->pNext;
    }
    cout << “Leaving while loop” << endl;
    pCurrent->pNext = DI;

    cout << “Leaving Adding Tail” << endl;
    return;
}
int HMTKDataItem::Remove(HMTKDataItem* DI)
{
    HMTKDataItem* pCurrent = pHead;
    if(pCurrent == (HMTKDataItem*)0)
    {
        return 0;
    }
    if(DI == pHead && DI->pNext!=0)
    {
        pHead = DI->pNext;
        delete DI;
        return 0;
    }
    if(DI == pHead && DI->pNext==0)
    {
        pHead = 0;
        return 0;
    }
    while(pCurrent)
    {
        if(DI == pCurrent->pNext)
        {
            pCurrent->pNext = DI->pNext;
            DI->pNext = (HMTKDataItem*)0;
            delete DI;
            return 1;
        }
        pCurrent = pCurrent->pNext;
    }
    return 0;
}
void HMTKDataItem::Clear(void)
{
    HMTKDataItem* pPrevious;
    if (pHead == 0)
    {
        return;
    }
    HMTKDataItem* pCurrent = pHead;
    while(pCurrent->pNext)
    {
        pPrevious = pCurrent;
        pCurrent = pCurrent->pNext;
        delete pPrevious;
    }
    pHead=0;
    return;
}
HMTKDataItem* HMTKDataItem::Find(wxString Search)
{
    if (pHead == 0)
    {
        return 0;
    }
    HMTKDataItem* pCurrent = pHead;
    while(pCurrent->pNext)
    {
        if(pCurrent->Name.Contains(Search)==1)
        {
            return pCurrent;
        }
        pCurrent = pCurrent->pNext;
    }
    return 0;
}

Yep, that’s a lot of code!

Once again, please note that the variable “Name” is used in the Find function so if you change it, you also need to change the Find function

In the following example we will add an item to the linked list

HMTKDataItem* DI = new HMTKDataItem;  // create a pointer to a new item
DI->Name = Selection;                 // Give Name a value
DI->Value = 1;                        // give Value a new value
HMTKDataItem.AddTail(DI);             // Add the new item to the tail
delete DI;                            // delete the pointer

Now, before adding an item it might not be a bad idea to search the linked list to see if it already exists.

HMTKDataItem* DI = new HMTKDataItem;   // Create a pointer
DI = HMTKDataItem.Find(Selection);     // Search the existing linked list
if(DI == 0)
// Now you know it does not exist

To remove an item you would use:

HMTKDataItem* DI = new HMTKDataItem;  // create a pointer
DI = HMTKDataItem.Find(Selection);    // use find
if(DI != 0)                           // as long as DI gets something to point at
{
    HMTKDataItem.Remove(DI);          // we now delete the item in the linked list
}

That is about it. I’ve given you a very brief overview on how to use a linked list in your program. If you have any questions please leave a comment.

Why iTunes will never come to Linux

October 21, 2006

iTunes is one of the most requested applications for Linux. Everything else that you use a computer for can be done on Linux. There are alternatives to iTunes for Linux, but sometimes you want the real thing. I don’t think it will ever happen and here is why…

When it comes to buying a computer the longstanding stereotypes were:

Windows = Business, gamers, everyday people.
Mac = Creative, artistic, writers, photographers, people who do the “snap-clap” at poetry jams.
Linux = Geeks, counter-culture revolutionaries.
Unix = Servers.

These days these stereotypes are shifting. With the invention (and marketing) of the iPod, Apple has seen a large upswing in sales and revenue. In fact, since they switched over to Intel chips to power their computers they’ve gained an additional 1% of the PC market! The iPod “halo” is here.

Apple marketing has never been something to sneeze at. If you ask someone what is “cooler” a Mac or a PC they will invariably tell you the Mac is cooler. They may not be able to say why, but you know… it’s all about the marketing.

Apple marketing is so good that if they sold toilets, people would buy them.

“Hey, check this out over here.”
“What, your bathroom?”
“Not just any bathroom, check out the Apple iToilet I just got!”
“Oh man, that is sweet! Does it play mp3 files while you sit or something?”
“No, but it does have a way cool Apple logo on the seat!”

You might be wondering, “what does Apple marketing have to do with iTunes on Linux?” Well, I’m getting to that.

Linux has come a long way. With distributions such as Knoppix, which allow you to drop a CD in your drive and boot straight into Linux, and user-friendly distributions such as Ubuntu Linux is 99% ready for the common user.

Word processing? Use OpenOffice
Playing DVDs? Use Kaffeine
Play mp3s? Use XMMS
I’ve got this one application that only works in Windows… Try wine or crossover Office.
Web browsing? Please! Use FireFox and Thunderbird for your email!
File and printer sharing? Use CUPS and Samba.

Pretty much anything you want to use your computer for can be done with Linux… except iTunes.

The market share for Windows is huge. No one denies that. The true numbers may be questionable as we don’t know how many of those Windows machines are still in use nor how many have been scrubbed clean and had a new OS installed on them.

One big thing is changing, the Windows Vista EULA. Windows XP introduced us to the WGA (Windows Genuine Advantage) software. Advantage for who? Microsoft? I see no advantage. WGA is just an anti-piracy tool.

Have you ever had a hard drive failure and had to re-install Windows on your machine? Did it complain that the key was no longer good? Have you ever had a friend bring their computer to you to fix after a hard drive failure but they no longer have their Windows XP disks so you try one of yours only to be told their key will not work with your software because it came from a different OEM?

Well, Vista is worse. According to the new Vista EULA you are only ever allowed to sell the software once and move it to a new computer once. This is bad news for the power gamers who constantly upgrade their machines. It’s also bad news if your hard drive fails or if you replace your motherboard.

Apple sees this, and they are happy. They are not worried about pirated copies of OSX. OSX only works on Macs. Macs are only available from Apple. There is no vast see of Mac clones out there for people to use pirated copies of OSX on. Apple owns the Mac market.

So, what does all of this have to do with iTunes and Linux?

iTunes and the iPod are the gateway products to the Mac world. People buy an iPod and use iTunes. When it comes time to buy a new computer they look and see Windows Vista, Mac OSX and Linux. iTunes will work on Vista or the Mac, but not Linux. If the consumer has purchased a large number of iTunes DRMed files they will want to preserve their investment by buying a computer that will allow them to access those files. So the choice becomes Windows vs. Mac.

Apple has their “boot camp” package which will allow you to run windows on a Mac. OSX is also based on BSD (a version of Unix) and is very stable. Even though Linux can be configured to behave (and look) like a Windows box or an OSX box people will move to the Mac as their next computer. OSX has all the glamour of being from Apple and the stability of being built on Unix. Many Linux applications are either available for OSX or easily ported to run on OSX.

Now, why would Apple want to port iTunes to Linux? iTunes+iPod *is* their killer app. People want it! Given the choice between buying a new Windows, Mac or Linux PC I think many people will be brought in under the shiny halo of Apple marketing and buy a Mac.

This is not like the browser wars of the 90’s where you give away your browser to gain market dominance. Apple’s iTunes already has the market dominance, now it’s time to capitalize on that dominance by bringing people into the Mac world, one new PC at a time…

UPDATE
I have tried to use iTunes with wine and have been able to install both it and quicktime. When I run iTunes it complains that it needs Windows 2003 SP4 or newer. It does install though and that is a start!