c++模板库_顺序表
来源:互联网 发布:淘宝正品香水店知乎 编辑:程序博客网 时间:2024/06/18 17:14
SeqList.h:
const int DefaultSize = 100;
template <typename Type> class SeqList{
public:
SeqList(int sz = DefaultSize)
:m_nmaxsize(sz), m_ncurrentsize(-1){
if (sz > 0){
m_elements = new Type[m_nmaxsize];
}
}
~SeqList(){
delete[] m_elements;
}
int Length() const{
return m_ncurrentsize + 1;
}
int Find(Type x) const;
int IsElement(Type x) const;
int Insert(Type x, int i);
int Remove(Type x);
int IsEmpty(){
return m_ncurrentsize == -1;
}
int IsFull(){
return m_ncurrentsize == m_nmaxsize - 1;
}
Type Get(int i){
return i<0 || i>m_ncurrentsize ? (cout << "can't find the element" << endl, 0) : m_elements[i];
}
void Print();
private:
Type *m_elements;
const int m_nmaxsize;
int m_ncurrentsize;
};
template <typename Type> int SeqList<Type>::Find(Type x) const{
for (int i = 0; i < m_ncurrentsize; i++)
if (m_elements[i] == x)
return i;
cout << "can't find the element you want to find" << endl;
return -1;
}
template <typename Type> int SeqList<Type>::IsElement(Type x) const{
if (Find(x) == -1)
return 0;
return 1;
}
template <typename Type> int SeqList<Type>::Insert(Type x, int i){
if (i<0 || i>m_ncurrentsize + 1 || m_ncurrentsize == m_nmaxsize - 1){
cout << "the operate is illegal" << endl;
return 0;
}
m_ncurrentsize++;
for (int j = m_ncurrentsize; j > i; j--){
m_elements[j] = m_elements[j - 1];
}
m_elements[i] = x;
return 1;
}
template <typename Type> int SeqList<Type>::Remove(Type x){
int size = m_ncurrentsize;
for (int i = 0; i < m_ncurrentsize;){
if (m_elements[i] == x){
for (int j = i; j < m_ncurrentsize; j++){
m_elements[j] = m_elements[j + 1];
}
m_ncurrentsize--;
continue;
}
i++;
}
if (size == m_ncurrentsize){
cout << "can't find the element you want to remove" << endl;
return 0;
}
return 1;
}
template <typename Type> void SeqList<Type>::Print(){
for (int i = 0; i <= m_ncurrentsize; i++)
cout << i + 1 << ":\t" << m_elements[i] << endl;
cout << endl << endl;
}
test.h
#include <iostream>
#include "SeqList.h"
using namespace std;
int main()
{
SeqList<int> test(15);
int array[15] = { 2, 5, 8, 1, 9, 9, 7, 6, 4, 3, 2, 9, 7, 7, 9 };
for (int i = 0; i < 15; i++){
test.Insert(array[i], 0);
}
test.Insert(1, 0);
cout << (test.Find(0) ? "can't be found " : "Be found ") << 0 << endl << endl;
test.Remove(7);
test.Print();
test.Remove(9);
test.Print();
test.Remove(0);
test.Print();
cin.get();
return 0;
}
- c++模板库_顺序表
- c语言_数据结构_顺序表
- 【C++】模板类顺序表
- 数据结构的C实现_顺序表
- c++(模板类)实现顺序表
- 【C++】用模板实现顺序表
- 【C++】用模板实现顺序表Vector
- c++标准模板库_顺序结构_Vectors详解
- c++标准模板库_顺序结构_List详解
- C++_012_数据结构_栈_顺序表储存版
- C++_标准模板库(STL)
- C++_模板
- C++_函数模板基础知识
- C++_类模板基础知识
- C++_函数模板基础知识
- C++(1)_函数模板
- 数据结构_顺序表相关操作C语言源代码
- [数据结构] 顺序表的实现(c++/类模板)
- CSU 1870 中南大学网络赛D题 Legendary
- POJ 2063 Investment(完全背包+)
- 【社会/人文】概念的理解 —— 断舍离、饭(饭制版)
- TCP与UDP协议基本概述
- java--从txt文件中读取完全二叉树数据建立二叉树并打印出遍历结果
- c++模板库_顺序表
- c语言用栈实现10进制转换为8进制
- CSU 1871 中南大学网络赛E题 简单的数论
- U3D 游戏引擎之游戏架构脚本该如何来写
- Template Strings (JavaScript)
- 项目代码规范(一)
- CSU 1867 中南大学网络赛F题 John and Health rate
- Android.mk编译APK范例
- 表达式求值