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.