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实现顺序表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++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- 算法学习记录十一(C++)--->调整数组顺序使奇数前偶数后
- 学习数据结构笔记--顺序表的数组实现
- 面试例题:输入n,求一个n x n矩阵,规定矩阵沿45度递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请问如何用C++实现? (中国台湾著名硬件公司2007年11月面试题)
- 【C++数据结构学习笔记---栈】用数组实现栈
- C++入门学习:虚析构、构造中不能实现多态、基类指针指向派生类数组的弊端
- c++ 模板学习笔记:函数模板实现数组通用排序和遍历打印(权哥)
- 对面试题“输入n,求一个nXn矩阵,规定矩阵沿45度递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请问如何用C++实现?”的理解
- 剑指offer刷题之c++实现的调整数组顺序使奇数位于偶数前面
- 剑指offer第十三题【调整数组顺序使奇数位于偶数前面】c++实现
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 习题2.8 随机数组的三种生成算法(补) 将bash的实现翻译成比较纯正的bash风格
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- C++[类设计]使用数组实现顺序栈
- 【C++数据结构学习笔记---线性表】用数组实现线性表
- 深度学习训练图片收集器——C++截图程序的实现2(键鼠钩子篇)
- C++学习总结——vector容器的实现
- 数据结构之顺序表——C++模板类实现
- C++实现一个数组类及其stack适配器
- 严蔚敏版数据结构学习笔记(1):线性表的顺序表示和实现
- 顺序栈的定义、初始化、出栈、入栈等操作 C++代码实现