您的位置:首页 > 编程语言 > C语言/C++

C++学习之用数组实现顺序表

2013-06-07 14:23 423 查看
开始学习数据结构,路还长,加油!!

用C++的template实现顺序表SequenceList ,第一次上传完整代码,比较菜。

SequenceList.h:

#ifndef SEQ_LIST_H
#define SEQ_LIST_H
#include <iostream>
#include <exception>
//模板的声明和定义要在同一个头文件中

//创建一个顺序表类模板
template <typename Type> class SequenceList
{
public:
SequenceList(int length = 10);
~SequenceList();
//增 可以在某个位置插入某个元素 index 个元素之后插入数据
void Insert(const int index , const Type * value);
//删
void Delete(const int index , Type * value);
//查
int GetIndex(const Type *value);
//改
void Update(const int index , Type * value);
//清空数组
void OutPut(std::ostream & out) const;
//返回长度
int Length();
bool IsEmpty();
private:
Type * SList;
int MaxSize , Len;
};

template <class Type>
SequenceList<Type>::SequenceList(int length = 10)
{
//构造函数
SList = new Type[length];
MaxSize = length;
Len = 0;
}

template <class Type>
void SequenceList<Type>::Insert(const int index , const Type * value)
{
//增
if (index <0 || index > MaxSize)
{
throw std::out_of_range("out of bounds!!");
}
if(Len == MaxSize)
{
throw std::out_of_range("No Memory !!");
}

//向后移一个位置
for(int i = Len ; i >= index ; i--)
{
SList[i+1] = SList[i];
}
SList[index] = *value;
Len++;
}

template <class Type>
void SequenceList<Type>::Delete(const int index , Type * value)
{
//将顺序表中的第K个元素存入value中, 并将第K个元素删除
if(index < 0 || index > Len) throw std::out_of_range("out of bounds!!");
*value = SList[index];
for(int i = index ; i <= Len ;i++)
{
SList[i] = SList[i+1];
}
Len--;
}

template <class Type>
int SequenceList<Type>::GetIndex(const Type* value)
{
//查询value所在的位置 不是索引值 如果没有则返回0
for(int i = 0 ;i < Len ;i++)
{
if(*value == SList[i])
return (i+1);
}
return 0;
}

template <class Type>
void SequenceList<Type>::Update(const int index , Type * value)
{
//改
if(index > MaxSize || index < 0)
{
throw std::out_of_range("out of bounds!!");
}

if(index >Len) return;

SList[index] = *value;
}

template <class Type>
void SequenceList<Type>::OutPut(std::ostream & out) const
{
//测试用
for (int i = 0; i < Len; i++)
{
out << SList[i] << " ";
}
}

template <class Type>
SequenceList<Type>::~SequenceList()
{
//析构函数 不要忘了delete 还有[]
delete[] SList;
SList = NULL;
}

template <class Type>
int SequenceList<Type>::Length()
{
//求长度
return Len;
}

template <class Type>
bool SequenceList<Type>::IsEmpty()
{
//判断是否为空
return (0 == Len);
}
#endif

源文件.cpp
//模板是在真正实例化的时候才真正编译生成代码的
//为容易使用几乎总是在头文件中放置全部的模板声明和定义的
#include <iostream>

#include "SequenceList.h"

using namespace std;

int * array = new int[20];

void Init(int * (&arr) , int len)
{
for(int i = 0 ; i < len ; i ++)
{
arr[i] = 2*i;
}
}

int main()
{
SequenceList<int> sList(20);
cout << "Length = " << sList.Length() << endl;
cout << "is empty ? " << sList.IsEmpty() <<endl;

Init(array , 6);

sList.Insert(0 ,&array[0]);
sList.Insert(1 ,&array[1]);
sList.Insert(2 , &array[2]);
sList.Insert(3 , &array[3]);
sList.Insert(4 , &array[4]);
sList.OutPut(cout);

int value = 4;

cout << "index:" << sList.GetIndex(&value) << endl;

value = 4;
sList.Delete( 2 , &value);
sList.OutPut(cout);
cout << endl;
value = 100;
sList.Update(0 , &value);
sList.OutPut(cout);
cout << endl;
system("pause");
}见笑了!祝莘莘学子高考顺利.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 数据结构 template
相关文章推荐