integer queue in BCB
disclaimer
look at the HTML source to copy/paste, as some characters might be
interpreted depending on the browser you use.
A variable number of integers had to be buffered in a communication
application. A queue made for the datatype integer was choosen.
It can be adapted to hold other datatypes as well.
More frequent C++ users might consider going for a template.
I might do that sometimes in the future.
A queue consists of two elements : the queue structure and the nodes.
The h file
//---------------------------------------------------------------------------
#ifndef MyQueueH
#define MyQueueH
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------
class PACKAGE TMyIntNode : public TObject
{
private:
int fdata;
TMyIntNode *fnext;
TMyIntNode *fprev;
protected:
public:
__fastcall TMyIntNode(int u);
__fastcall TMyIntNode();
__fastcall ~TMyIntNode();
__published:
__property int data={read=fdata,write=fdata};
__property TMyIntNode *next={read=fnext,write=fnext};
__property TMyIntNode *prev={read=fprev,write=fprev};
};
//
// TMyIntQueue q;
//
// q= new TMyIntQueue();
// q->queue(5);
//
// if q->count>0 { i = q->unqueue(); }
//
class PACKAGE TMyIntQueue: public TObject
{
private:
int fcount;
TMyIntNode *fhead;
TMyIntNode *ftail;
protected:
public:
__fastcall TMyIntQueue();
__fastcall ~TMyIntQueue();
__fastcall queue(int z);
int __fastcall unqueue();
__published:
__property int count={read=fcount,write=fcount};
};
//---------------------------------------------------------------------------
#endif
The cpp file
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "MyQueue.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//static inline void ValidCtrCheck(TMyIntNode *)
//{
// new TMyIntNode(NULL);
//}
//---------------------------------------------------------------------------
__fastcall TMyIntNode::TMyIntNode(int u)
: TObject()
{
fdata = u;
fnext=NULL;
fprev=NULL;
}
__fastcall TMyIntNode::TMyIntNode()
{
fdata = 0;
fnext=NULL;
fprev=NULL;
}
__fastcall TMyIntNode::~TMyIntNode()
{
//
}
//.........................................................................
__fastcall TMyIntQueue::TMyIntQueue()
{
fhead = NULL;
ftail = NULL;
fcount =0;
}
__fastcall TMyIntQueue::~TMyIntQueue()
{
int h;
while (fhead != NULL)
{
h= unqueue();
}
}
__fastcall TMyIntQueue::queue(int z)
{
TMyIntNode *u;
u=new TMyIntNode(z);
if (fhead==NULL)
{
fhead=u;
ftail=u;
fcount++;
}
else {
u->next=fhead;
fhead->prev=u;
fhead=u;
fcount++;
}
}
int __fastcall TMyIntQueue::unqueue()
{
TMyIntNode *u;
int i;
if (ftail !=NULL) {
u=ftail;
ftail=u->prev;
if (ftail==NULL){fhead=NULL;}
fcount--;
i=u->data;
u->TMyIntNode::~TMyIntNode();
return i;
}
}
//---------------------------------------------------------------------------
//namespace Myqueue
//{
// void __fastcall PACKAGE Register()
// {
// TComponentClass classes[1] = {__classid(TMyIntNode)};
// RegisterComponents("Samples", classes, 0);
// }
//}
//---------------------------------------------------------------------------
a test environment
place 3 buttons : init_queue, queue_in, queue_out onto a form.
place a spinedit qiv
place 2 labels qcount, outnumber
the init_queue click :
void __fastcall TForm1::init_queueClick(TObject *Sender)
{
q = new TMyIntQueue();
}
the queue_in click :
void __fastcall TForm1::queue_inClick(TObject *Sender)
{
int t;
t = qiv->Value;
q->queue(t);
qcount->Caption=IntToStr(q->count);
}
the queue_out click :
void __fastcall TForm1::queue_outClick(TObject *Sender)
{
int t;
if (q->count == 0) exit;
t = q->unqueue();
outnumber->Caption=IntToStr(t);
qcount->Caption=IntToStr(q->count);
}
double linked list and queue in Delphi
my BCB
home
last updated: 23.oct.01
Copyright (99,2001) Ing.Büro R.Tschaggelar