因工作需要,做了一个链表进行抛砖引玉
ledxaan.h
#ifndef ledxaanH
#define ledxaanH
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/************************************************
LINE STRING STRUCT
************************************************/
struct __line_string {
struct __line_string *pNext;
struct __line_string *pPrev;
bool used;
char cString[20];
};
typedef struct __line_string LINE_STRING_STRUCT;
typedef LINE_STRING_STRUCT * pLINE_STRING_STRUCT;
/*************************************************
class
**************************************************/
class TLedxa
{
public:
TLedxa();
int LedInsertLine (char *pcString); // 插入行
int GetLedLine(char *pString[]); // 取得对应行的值
private:
pLINE_STRING_STRUCT pHeader; // Header 指针
LINE_STRING_STRUCT item[6]; // 数据队列
};
extern TLedxa LedXa;
//---------------------------------------------------------------------------
#endif
ledxaan.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "ledxaan.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
TLedxa LedXa;
TLedxa::TLedxa()
{
for(int i=0; i<5; i++){ // 连接链表
item[i].used = false;
item[i].pNext = &item[i+1];
item[i+1].pPrev = &item[i];
}
// 首尾连接
item[5].pNext = &item[0];
item[0].pPrev = &item[5];
pHeader = &item[0];
}
//***********************************************
// Insert
//************************************************
int TLedxa::LedInsertLine(char *pcString)
{
if(!pHeader->used){
pHeader->used = true;
strcpy(pHeader->cString, pcString); // 赋值
}else{
pHeader = pHeader->pNext; // 链表指针移动
pHeader->used = true;
strcpy(pHeader->cString,pcString);
}
return (0);
}
//************************************************
// GetLine
//************************************************
int TLedxa::GetLedLine(char *pString[])
{
int i=0;
pLINE_STRING_STRUCT pLine;
pLine = pHeader;
for(; ((pLine->used)&&(i<7)); i++){
pString[i] = &pLine->cString[0];
pLine = pLine->pPrev;
}
return i;
}
调用函数如下:
void testline()
{
static int i = 1;
char buf[20];
char *String[8];
int StringNum;
int j;
sprintf(buf, "Line %d", i++);
LedXa.LedInsertLine(buf);
StringNum = LedXa.GetLedLine(&String[0]);
for (j = 0; j < StringNum; j++) {
cout<<String[j]<<endl;
}
}