数据接口线性表C++实现
2015-03-07 00:00
447 查看
#include <iostream> #include <malloc.h> using namespace std; const int list_init_size = 100; const int listincrement = 10; template<class ElemType> class list_sq { public: bool InitList(); //构造一个空的线性表 void DestroyList(); //销毁线性表 void ClearList(); //将lst重置为空表 bool ListEmpty(); //若lst为空表,则返回true,否则返回false int ListLength(); //返回lst中元素的个数 bool GetElem(int pos, ElemType &e); //返回第pos个元素的值 bool PriorElem(ElemType cur_e, ElemType &pre_e);//若cur_e是lst的数据元素,且不是第一个, //则用pre_e返回它的前驱,否则操作失败 bool NextElem(ElemType cur_e, ElemType &next_e);//若cur_e是lst的数据元素,且不是最后一个,则用next_e返回它的后继 //否则操作失败,next_e无定义 bool ListInsert(int pos, ElemType e); //在lst中第pos个位置之前插入新的数据元素e,lst长度加一 bool ListDelete(int pos, ElemType &e); //删除lst的第pos个元素,并用e返回其值,lst的长度减一 private: class sq { public: sq() : elem(NULL), length(0), listsize(0){} ElemType *elem; int length; int listsize; }; sq lst; }; template<class ElemType> bool list_sq<ElemType>::InitList() { lst.elem = new ElemType[list_init_size]; if (!lst.elem) return false; lst.length = 0; lst.listsize = list_init_size; return true; } template<class ElemType> void list_sq<ElemType>::DestroyList() { delete[] lst.elem; lst.length = 0; lst.listsize = 0; lst.elem = NULL; } template<class ElemType> void list_sq<ElemType>::ClearList() { lst.length = 0; } template<class ElemType> bool list_sq<ElemType>::ListEmpty() { return lst.length == 0; } template<class ElemType> int list_sq<ElemType>::ListLength() { return lst.length; } template<class ElemType> bool list_sq<ElemType>::GetElem(int pos, ElemType &e) { if(pos < 1 || pos > lst.length) return false; e = *(lst.elem + pos - 1); return true; } template<class ElemType> bool list_sq<ElemType>::PriorElem(ElemType cur_e, ElemType &pre_e) { int i = 2; ElemType *p = lst.elem + 1; while(i <= lst.length && *p != cur_e) { ++p; ++i; } if(i > lst.length) return false; else pre_e = *--p; return true; } template<class ElemType> bool list_sq<ElemType>::NextElem(ElemType cur_e, ElemType &next_e) { int i = 1; ElemType *p = lst.elem; while(i <= lst.length && *p != cur_e) { ++p; ++i; } if(i == lst.length) return false; else next_e = *++p; return true; } template<class ElemType> bool list_sq<ElemType>::ListInsert(int pos, ElemType e) { ElemType *newbase, *p, *q; if(pos < 1 || pos > lst.length + 1) return false; if(lst.length >= lst.listsize) { if(!(newbase = (ElemType *)realloc(lst.elem,(lst.listsize+listincrement)*sizeof(ElemType)))) return false; lst.elem = newbase; lst.listsize += listincrement; } q = lst.elem +pos - 1; for(p = lst.elem + lst.length - 1; p >= q; --p) *(p + 1) = *p; *q = e; ++lst.length; return true; } template<class ElemType> bool list_sq<ElemType>::ListDelete(int pos, ElemType &e) { ElemType *p, *q; if(pos < 1 || pos > lst.length) return false; p = lst.elem + pos - 1; e = *p; q = lst.elem + lst.length - 1; for(++p; p <= q; ++p) *(p - 1) = *p; --lst.length; return true; } int main() { return 0; }
相关文章推荐
- 数据接口线性表C++实现
- 数据结构——线性表——顺序存储结构——C++实现线性表
- C++ 数据结构线性表-数组实现
- C++ 分文件写法(接口与实现分离)
- C++ 数据结构实现两个栈实现一个队列
- 读书笔记 effective c++ Item 34 区分接口继承和实现继承
- 【算法和数据结构】线性表(四)用两个栈来实现队列(C++实现)
- 【C语言 数据结构】 简单线性表实现
- c++ 线性表简单实现
- 数据结构顺序表链表C++实现
- C++ 中实现把EXCEL的数据导入数据库(ACCESS、MSSQL等)实例代码
- C++中实现把表的数据导出到EXCEL并打印实例代码
- Android 使用接口回掉实现线程间数据传递
- 数据结构——算法之(043)(c++各种排序算法实现)
- 数据挖掘—概念学习Candidate-Elimination算法的C++实现
- 数据挖掘-基于机器学习的SNS隐私策略推荐向导分类器的C++及WEKA实现与评估
- c++线性表实现(链式)
- [置顶] C++连接CTP接口实现简单量化交易(行情、交易、k线、策略)
- 实战caffe多标签分类——汽车品牌与车辆外观(C++接口)[详细实现+数据集]
- [置顶] 关于C++、C#实现EXCEL数据库批量导入数据库万行以上数据效率问题