delphi7存储过程传入数组_数据结构线性表之顺序存储 类的封装
自己編程也挺久的了,然而數(shù)據(jù)結(jié)構(gòu)這塊是很弱的部分,然而這個東西對編程又異常重要,雖然這么久我一直沒感受到。所以最近集中學(xué)習(xí)一下。正好手里有一本大話數(shù)據(jù)結(jié)構(gòu),就按照該書的順序往下學(xué)習(xí)。
開始學(xué)習(xí)之前,要了解幾個概念,如時間復(fù)雜度和空間復(fù)雜度,具體概念就不羅列,大家可直接百度。
下面是線性表的定義:零個或多個數(shù)據(jù)元素的有限序列。
順序存儲:用一段地址連續(xù)的存儲單元依次存儲線性表的數(shù)據(jù)元素。
也就是這次說的順序存儲,大家自然就會想到數(shù)組。Ok,接下來我們就使用C++來封裝一個類,實現(xiàn)線性表的一些操作。
一般對數(shù)據(jù)操作都是增刪改查,我們以這幾個操作為核心,再擴充幾個其他操作,如初始化線性表、線性表是否為空、判斷某一元素的位置等操作。其實這里面這些操作主要就是插入與刪除,其他的都很好理解。
開發(fā)環(huán)境:Ubuntu Qt
類的組成:common.h 一些宏定義、數(shù)據(jù)結(jié)構(gòu)、
SeqList類的頭文件與源文件
namespace seqlist_namespace
{
#define OK 0
#define ERROR 1
#define MAXSIZE 100 /* 存儲空間初始分配量 */
typedef int ElemType; /* ElemType類型根據(jù)實際情況而定,這里假設(shè)為int */
typedef struct
{
ElemType data[MAXSIZE]; /* 數(shù)組,存儲數(shù)據(jù)元素 */
int length; /* 線性表當前長度 */
}SqList;
}
2.頭文件
class SeqList
{
public:
SeqList();
~SeqList();
public:
int initList(seqlist_namespace::SqList *L);
int isListEmpty(const seqlist_namespace::SqList& L);
int clearList(seqlist_namespace::SqList *L);
int listLength(const seqlist_namespace::SqList& L);
//list existed, return element by position i,
//first element means array cursor is 0
int getElem(const seqlist_namespace::SqList& L, const int i, seqlist_namespace::ElemType *e);
//list existed,search first is e element
int locateElem(const seqlist_namespace::SqList& L, const seqlist_namespace::ElemType e);
//
int listInsert(seqlist_namespace::SqList *L, const int i, const seqlist_namespace::ElemType e);
//
int listDelete(seqlist_namespace::SqList *L, const int i, seqlist_namespace::ElemType *e);
int listTraverse(seqlist_namespace::SqList& L);
};
3. 源文件
int SeqList::listInsert(seqlist_namespace::SqList *L, const int i,
const seqlist_namespace::ElemType e)
{
if(MAXSIZE == L->length)
return ERROR;
if(i < 1 || i > L->length+1)
return ERROR;
if(i <= L->length)
{
for(int j=L->length-1; j>=i-1; j--)
L->data[j+1] = L->data[j];
}
L->data[i-1] = e;
L->length++;
return OK;
}
int SeqList::listDelete(seqlist_namespace::SqList *L, const int i,
seqlist_namespace::ElemType *e)
{
if(0 == L->length)
return ERROR;
if(i < 1 || i > L->length)
return ERROR;
if(i < L->length)
{
for(int j=i-1; j!=L->length-1; j++)
{
L->data[j] = L->data[j+1];
}
}
*e = L->data[i-1];
L->length--;
return OK;
}
當需要修改元素時,傳入線性表指針;如果不需要修改則傳入線性表引用變量。
簡單說下插入和刪除。
插入數(shù)據(jù)時,涉及到數(shù)據(jù)元素向后移動,所以找到元素最大下標(L.length - 1)的數(shù)據(jù),之后將其后移一個位置,以此類推,直到移動完所有元素。之后進行插入操作。
刪除數(shù)據(jù)時,找到要刪除元素的下標(i -1),將它后繼元素(i-1+1)前移,以此類推。
4. 主函數(shù)中測試程序
seqlist_namespace::SqList sqlist;
seqlist_namespace::ElemType element;
SeqList L;
L.initList(&sqlist);
for(int i = 0; i < 5; i++)
L.listInsert(&sqlist, 1, i);
L.listTraverse(sqlist);
int ret = 0;
ret = L.locateElem(sqlist,4);
std::cout << "locate " << ret << std::endl;
ret = L.isListEmpty(sqlist);
std::cout << "isEmpty" << ret << std::endl;
L.getElem(sqlist, ret, &element);
std::cout << "getElement" << element << std::endl;
L.listDelete(&sqlist, 1, &element);
L.listTraverse(sqlist);
效果圖:
5. 小結(jié)
從實現(xiàn)上講,主要是插入、刪除部分的標準以及線性表的一些狀態(tài)的判斷,如表是否為空、表是否為滿、插入數(shù)據(jù)位置的合理性等。
從重要性講,它很重要,雖然現(xiàn)在還沒感覺出來。
學(xué)習(xí)貴在堅持和不斷總結(jié)。
程序中還有很多不足,希望大家不吝指正,需要整個Qt的工程的同志可在公眾號后臺留言。
歡迎關(guān)注公眾號: Pou光明
總結(jié)
以上是生活随笔為你收集整理的delphi7存储过程传入数组_数据结构线性表之顺序存储 类的封装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 涨幅 35%-40%,欧洲市场过去几年主
- 下一篇: python怎么显示汉字_mac在mat