说明:代码是可以运行的,但是发表在博客上后复制到编译器里面报N多错误,找了半天原因是网页里面生成了一些空白字符,这些字符编译器无法识别。
因此使用了2种插入格式插入代码。
第二个带注释解释的代码不可复制,最上面折叠不带注释的代码复制后可以正常编译运行。
#include <iostream>
#include<cstdlib>
#include<string>
#include<iomanip> using namespace std; const int defaultsize = ; template <typename T>
class seqlist
{
public:
seqlist(int size = defaultsize);
seqlist(seqlist<T> &);
~seqlist(); //function
int lengthlist(void);
int listsize(void);
int searchlist(T i);
bool getdata(int i, T & t);
bool setdata(int i, T t);
bool insertdate(T t);
bool removedate(int i, T & t);
bool isempty(void);
bool isfull(void);
void sortlist(void); private:
T * lists;
int maxsize;
int last;
}; template <typename T>
seqlist<T>::seqlist(int size)
{
if (size > )
{
maxsize = size;
last = -;
lists = new T[size];
if (lists == NULL)
{
cout << "fatal error:can NOT allocate memory!" << endl;
exit(-);
}
}
else
cout << "incorrect size!" << endl;
} template <typename T>
seqlist<T>::seqlist(seqlist<T> &p)
{
maxsize = p.listsize();
last = p.lengthlist() - ;
lists = new T[maxsize];
if (lists == NULL)
{
cout << "memory error." << endl;
exit(-);
}
for (int i = ; i <= last; i++)
{
lists[i] = p.lists[i];
}
} template <typename T>
seqlist<T>::~seqlist()
{
cout << "free memory." << endl;
delete [] lists;
} template <typename T>
int seqlist<T>::lengthlist(void)
{
return last + ;
} template <typename T>
int seqlist<T>::listsize(void)
{
return maxsize;
} template <typename T>
int seqlist<T>::searchlist(T t)
{
if (!isempty())
{
cout << "search data=" << t << endl;
for (int i = ; i <= last; i++)
if (t - lists[i] < 0.00000001)
return i + ;
}
return -;
} template <typename T>
bool seqlist<T>::getdata(int i, T & t)
{
if (i > && i <= last + )
{
t = lists[i - ];
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::setdata(int i, T t)
{
if (i >= && i <= last + )
{
lists[i - ] = t;
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::insertdate(T t)
{
if (isfull())
{
cout << "The lists is full!" << endl;
return false;
}
else
{
lists[last + ] = t;
last++;
return true;
}
} template <typename T>
bool seqlist<T>::removedate(int i, T & t)
{
if (isempty())
{
cout << "the lists is empty!" << endl;
return false;
}
else if (i > && i <= last + )
{
t = lists[i - ];
for (int j = i; j <= last + ; j++)
lists[j - ] = lists[j];
last--;
return true;
}
else
{
cout << "incorrect number!" << endl;
return false;
}
} template <typename T>
bool seqlist<T>::isempty(void)
{
// cout << "last" << last << endl;
return (last == -) ? true : false;
} template <typename T>
bool seqlist<T>::isfull(void)
{
return (last == maxsize - ) ? true : false;
} template <typename T>
void seqlist<T>::sortlist(void)
{
if (isempty())
cout << "lists do not need to sort." << endl;
else
for (int i = ; i < last; i++)
{
int k = i;
for (int j = i + ; j < last + ; j++)
if (lists[j] < lists [k])
k = j;
if (k != i)
{
T temp = lists[i];
lists[i] = lists[k];
lists[k] = temp;
}
}
} int main(void)
{
int x1 = , x2 = , x3 = ;
double y1 = 97.2775, y2 = , y3 = ;
bool status; seqlist<int> seq1();
for (int i = ; i < ; i++)
seq1.insertdate(rand() % );
cout << "排序前:" << endl;
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
}
seq1.sortlist();
cout << endl;
cout << "排序后:" << endl;
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
}
cout << endl;
x1 = seq1.listsize();
cout << "lists seq1 size:" << x1 << endl;
status = seq1.isempty();
if (!status)
cout << "lists seq1 is not empty." << endl; x1 = seq1.lengthlist();
cout << "lists seq1 length:" << x1 << endl;
x1 = seq1.searchlist(x3);
cout << "lists seq1 search:" << x1 << endl; cout << endl << endl;
cout << "building seq2." << endl;
seqlist<double> seq2();
for (int i = ; i < ; i++)
seq2.insertdate(rand() / (double)(RAND_MAX / )); status = seq2.isfull();
if (status)
cout << "lists seq2 is full." << endl;
cout << endl; for (int i = ; i < ; i++)
{
seq2.getdata(i + , y2);
cout << "list[" << setw() << i + << "]=" << setw() << y2 << " ";
if ((i + ) % == )
cout << endl;
} cout << endl; x1 = seq2.lengthlist();
cout << "lists seq2 length:" << x1 << endl; x1 = seq2.searchlist(y1);
cout << "lists seq2 search:" << x1 << endl; seq2.setdata(, );
seq2.getdata(, y2);
cout << "seq2 setdata=" << y2 << endl; seq2.removedate(, y3);
cout << "remove data=" << y3 << endl;
cout << endl;
for (int i = ; i < ; i++)
{
seq2.getdata(i + , y2);
cout << "list[" << setw() << i + << "]=" << setw() << y2 << " ";
if ((i + ) % == )
cout << endl;
} cout << endl;
cout << endl; seqlist<int> seq3(seq1);
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
} return ;
}
代码如下:
#include <iostream>
#include<cstdlib> //rand()函数产生随机值
#include<string> //bool函数赋值
#include<iomanip> //输出格式控制头文件 using namespace std; const int defaultsize = 100; template <typename T>
class seqlist
{
public:
seqlist(int size = defaultsize); //默认形参的构造函数
seqlist(seqlist<T> &);
~seqlist(); //function
int lengthlist(void); //顺序表当前的元素个数
int listsize(void); //顺序表最大元素容纳个数
int searchlist(T i); //搜索顺序表
bool getdata(int i, T & t); //取顺序表第i个元素后赋值到引用t中
bool setdata(int i, T t); //设置第i个元素值为t
bool insertdate(T t); //插入到顺序表尾
bool removedate(int i, T & t); //将第i个元素保存到t后移出顺序表,
bool isempty(void); //判断表空
bool isfull(void); //判断表满
void sortlist(void); //表排序 private:
T * lists; //size of sequence list. //表存储的内存
int maxsize; //the maximum number of sequence list. //表的最大容纳元素个数
int last; //indicating sequence list last element. //当前表元素个数指示
}; template <typename T>
seqlist<T>::seqlist(int size)
{
if (size > 0)
{
maxsize = size;
last = -1;
lists = new T[size];
if (lists == NULL)
{
cout << "fatal error:can NOT allocate memory!" << endl;
exit(-1);
}
}
else
cout << "incorrect size!" << endl;
} template <typename T>
seqlist<T>::seqlist(seqlist<T> &p)
{
maxsize = p.listsize();
last = p.lengthlist() - 1;
lists = new T[maxsize];
if (lists == NULL)
{
cout << "memory error." << endl;
exit(-1);
}
for (int i = 0; i <= last; i++)
{
lists[i] = p.lists[i];
}
} template <typename T>
seqlist<T>::~seqlist()
{
cout << "free memory." << endl;
delete [] lists;
} template <typename T>
int seqlist<T>::lengthlist(void)
{
return last + 1;
} template <typename T>
int seqlist<T>::listsize(void)
{
return maxsize;
} template <typename T>
int seqlist<T>::searchlist(T t)
{
if (!isempty())
{
cout << "search data=" << t << endl;
for (int i = 0; i <= last; i++)
if (t - lists[i] < 0.00000001) //使用差值判断2个元素是否相等,防止精度导致的浮点类型不相等,例如1.23456与1.23457默认精度的情况
return i + 1; //成功则返回位置
}
return -1; //失败返回-1
} template <typename T>
bool seqlist<T>::getdata(int i, T & t)
{
if (i > 0 && i <= last + 1)
{
t = lists[i - 1];
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::setdata(int i, T t)
{
if (i >= 1 && i <= last + 1)
{
lists[i - 1] = t;
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::insertdate(T t)
{
if (isfull())
{
cout << "The lists is full!" << endl;
return false;
}
else
{
lists[last + 1] = t; //由于顺序表的元素排列未排序,处于无须状态,指定插入顺序无意义,因此默认插在顺序表尾
last++;
return true;
}
} template <typename T>
bool seqlist<T>::removedate(int i, T & t)
{
if (isempty())
{
cout << "the lists is empty!" << endl;
return false;
}
else if (i > 0 && i <= last + 1)
{
t = lists[i - 1]; //先保存即将要移出表元素的值
for (int j = i; j <= last + 1; j++) //移出后,将后面的表元素填充到前面的位置
lists[j - 1] = lists[j];
last--;
return true;
}
else
{
cout << "incorrect number!" << endl;
return false;
}
} template <typename T>
bool seqlist<T>::isempty(void)
{
// cout << "last" << last << endl;
return (last == -1) ? true : false;
} template <typename T>
bool seqlist<T>::isfull(void)
{
return (last == maxsize - 1) ? true : false;
} template <typename T>
void seqlist<T>::sortlist(void)
{
if (isempty())
cout << "lists do not need to sort." << endl;
else
for (int i = 0; i < last; i++) //无须顺序表的排序,从第一个元素开始与后面所有的元素比较
{
int k = i; //用k指示最小的元素
for (int j = i + 1; j < last + 1; j++) //用当前最小的与整个顺序表的所有元素比较
if (lists[j] < lists [k])
k = j;
if (k != i)
{
T temp = lists[i];
lists[i] = lists[k];
lists[k] = temp;
}
}
} int main(void)
{
int x1 = 0, x2 = 0 , x3 = 42 ;
double y1 = 97.2775, y2 = 0, y3 = 0 ;
bool status; seqlist<int> seq1(30);
for (int i = 0; i < 30; i++)
seq1.insertdate(rand() % 998);
cout << "排序前:" << endl;
for (int i = 0; i < 30; i++)
{
seq1.getdata(i + 1, x2);
cout.fill('0');
cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << " ";
if ((i + 1) % 5 == 0)
cout << endl;
}
seq1.sortlist();
cout << endl;
cout << "排序后:" << endl;
for (int i = 0; i < 30; i++)
{
seq1.getdata(i + 1, x2);
cout.fill('0');
cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << " ";
if ((i + 1) % 5 == 0)
cout << endl;
}
cout << endl;
x1 = seq1.listsize();
cout << "lists seq1 size:" << x1 << endl;
status = seq1.isempty();
if (!status)
cout << "lists seq1 is not empty." << endl; x1 = seq1.lengthlist();
cout << "lists seq1 length:" << x1 << endl;
x1 = seq1.searchlist(x3);
cout << "lists seq1 search:" << x1 << endl; cout << endl << endl;
cout << "building seq2." << endl;
seqlist<double> seq2(30);
for (int i = 0; i < 30; i++)
seq2.insertdate(rand() / (double)(RAND_MAX / 100)); status = seq2.isfull();
if (status)
cout << "lists seq2 is full." << endl;
cout << endl; for (int i = 0; i < 20; i++)
{
seq2.getdata(i + 1, y2);
cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << " ";
if ((i + 1) % 5 == 0)
cout << endl;
} cout << endl; x1 = seq2.lengthlist();
cout << "lists seq2 length:" << x1 << endl; x1 = seq2.searchlist(y1);
cout << "lists seq2 search:" << x1 << endl; seq2.setdata(7, 99);
seq2.getdata(7, y2);
cout << "seq2 setdata=" << y2 << endl; seq2.removedate(7, y3);
cout << "remove data=" << y3 << endl;
cout << endl;
for (int i = 0; i < 19; i++)
{
seq2.getdata(i + 1, y2);
cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << " ";
if ((i + 1) % 5 == 0)
cout << endl;
}
cout << endl;
cout << endl; seqlist<int> seq3(seq1);
for (int i = 0; i < 30; i++)
{
seq1.getdata(i + 1, x2);
cout.fill('0');
cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << " ";
if ((i + 1) % 5 == 0)
cout << endl;
} return 0;
}
可正常编译代码:
#include <iostream>
#include<cstdlib>
#include<string>
#include<iomanip> using namespace std; const int defaultsize = ; template <typename T>
class seqlist
{
public:
seqlist(int size = defaultsize);
seqlist(seqlist<T> &);
~seqlist(); //function
int lengthlist(void);
int listsize(void);
int searchlist(T i);
bool getdata(int i, T & t);
bool setdata(int i, T t);
bool insertdate(T t);
bool removedate(int i, T & t);
bool isempty(void);
bool isfull(void);
void sortlist(void); private:
T * lists;
int maxsize;
int last;
}; template <typename T>
seqlist<T>::seqlist(int size)
{
if (size > )
{
maxsize = size;
last = -;
lists = new T[size];
if (lists == NULL)
{
cout << "fatal error:can NOT allocate memory!" << endl;
exit(-);
}
}
else
cout << "incorrect size!" << endl;
} template <typename T>
seqlist<T>::seqlist(seqlist<T> &p)
{
maxsize = p.listsize();
last = p.lengthlist() - ;
lists = new T[maxsize];
if (lists == NULL)
{
cout << "memory error." << endl;
exit(-);
}
for (int i = ; i <= last; i++)
{
lists[i] = p.lists[i];
}
} template <typename T>
seqlist<T>::~seqlist()
{
cout << "free memory." << endl;
delete [] lists;
} template <typename T>
int seqlist<T>::lengthlist(void)
{
return last + ;
} template <typename T>
int seqlist<T>::listsize(void)
{
return maxsize;
} template <typename T>
int seqlist<T>::searchlist(T t)
{
if (!isempty())
{
cout << "search data=" << t << endl;
for (int i = ; i <= last; i++)
if (t - lists[i] < 0.00000001)
return i + ;
}
return -;
} template <typename T>
bool seqlist<T>::getdata(int i, T & t)
{
if (i > && i <= last + )
{
t = lists[i - ];
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::setdata(int i, T t)
{
if (i >= && i <= last + )
{
lists[i - ] = t;
return true;
}
else
return false;
} template <typename T>
bool seqlist<T>::insertdate(T t)
{
if (isfull())
{
cout << "The lists is full!" << endl;
return false;
}
else
{
lists[last + ] = t;
last++;
return true;
}
} template <typename T>
bool seqlist<T>::removedate(int i, T & t)
{
if (isempty())
{
cout << "the lists is empty!" << endl;
return false;
}
else if (i > && i <= last + )
{
t = lists[i - ];
for (int j = i; j <= last + ; j++)
lists[j - ] = lists[j];
last--;
return true;
}
else
{
cout << "incorrect number!" << endl;
return false;
}
} template <typename T>
bool seqlist<T>::isempty(void)
{
// cout << "last" << last << endl;
return (last == -) ? true : false;
} template <typename T>
bool seqlist<T>::isfull(void)
{
return (last == maxsize - ) ? true : false;
} template <typename T>
void seqlist<T>::sortlist(void)
{
if (isempty())
cout << "lists do not need to sort." << endl;
else
for (int i = ; i < last; i++)
{
int k = i;
for (int j = i + ; j < last + ; j++)
if (lists[j] < lists [k])
k = j;
if (k != i)
{
T temp = lists[i];
lists[i] = lists[k];
lists[k] = temp;
}
}
} int main(void)
{
int x1 = , x2 = , x3 = ;
double y1 = 97.2775, y2 = , y3 = ;
bool status; seqlist<int> seq1();
for (int i = ; i < ; i++)
seq1.insertdate(rand() % );
cout << "排序前:" << endl;
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
}
seq1.sortlist();
cout << endl;
cout << "排序后:" << endl;
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
}
cout << endl;
x1 = seq1.listsize();
cout << "lists seq1 size:" << x1 << endl;
status = seq1.isempty();
if (!status)
cout << "lists seq1 is not empty." << endl; x1 = seq1.lengthlist();
cout << "lists seq1 length:" << x1 << endl;
x1 = seq1.searchlist(x3);
cout << "lists seq1 search:" << x1 << endl; cout << endl << endl;
cout << "building seq2." << endl;
seqlist<double> seq2();
for (int i = ; i < ; i++)
seq2.insertdate(rand() / (double)(RAND_MAX / )); status = seq2.isfull();
if (status)
cout << "lists seq2 is full." << endl;
cout << endl; for (int i = ; i < ; i++)
{
seq2.getdata(i + , y2);
cout << "list[" << setw() << i + << "]=" << setw() << y2 << " ";
if ((i + ) % == )
cout << endl;
} cout << endl; x1 = seq2.lengthlist();
cout << "lists seq2 length:" << x1 << endl; x1 = seq2.searchlist(y1);
cout << "lists seq2 search:" << x1 << endl; seq2.setdata(, );
seq2.getdata(, y2);
cout << "seq2 setdata=" << y2 << endl; seq2.removedate(, y3);
cout << "remove data=" << y3 << endl;
cout << endl;
for (int i = ; i < ; i++)
{
seq2.getdata(i + , y2);
cout << "list[" << setw() << i + << "]=" << setw() << y2 << " ";
if ((i + ) % == )
cout << endl;
} cout << endl;
cout << endl; seqlist<int> seq3(seq1);
for (int i = ; i < ; i++)
{
seq1.getdata(i + , x2);
cout.fill('');
cout << "list[" << setw() << i + << "]=" << setw() << x2 << " ";
if ((i + ) % == )
cout << endl;
} return ;
}
C++基于模板顺序表的实现(带排序)的更多相关文章
-
C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
前面介绍的模板有关知识大部分都是用顺序表来举例的,现在我们就专门用模板来实现顺序表,其中的很多操作都和之前没有多大区别,只是有几个比较重要的知识点需要做专门的详解. #pragma once #inc ...
-
基于C++的顺序表的实现
顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...
-
";《算法导论》之‘线性表’";:基于动态分配的数组的顺序表
我们利用静态分配的数组来实现的顺序表的局限还是挺大的,主要在于它的容量是预先定好的,用户不能根据自己的需要来改变.如果为了后续用户能够自己调整顺序表的大小,动态地分配数组空间还是很有必要的.基于动态分 ...
-
";《算法导论》之‘线性表’";:基于静态分配的数组的顺序表
首先,我们来搞明白几个概念吧(参考自网站数据结构及百度百科). 线性表 线性表是最基本.最简单.也是最常用的一种数据结构.线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外, ...
-
C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...
-
C++顺序表(模板总结)
C++顺序表(模板总结) 总结: 1.模板类的实质是什么:让程序员写出和类型无关的代码 2.模板的对象时什么:方法或者类 3.是对类中的一系列操作,提供一个不固定数据类型的方法 用模板做的类的时候要指 ...
-
顺序表 C++模板实现
#include <iostream> using namespace std; template <typename T> class list{ private: int ...
-
老郭带你学数据结构(C语言系列)2-线性表之动态顺序表
一.基本概念: 线性表:由n个类型相同的数据元素组成的有限序列,记为(a1,a2,--an). 线性表的特征:其中的元素存在这序偶关系,元素之间存在着严格的次序关系. 顺序存储表:线性表中的元素依次存 ...
-
老郭带你学数据结构(C语言系列)1-线性表之静态顺序表
在学习之前,先说下我的软件情况,操作系统是manjaro linux 今天刚刚升级的,编辑器是vim8.0.55,编译器是gcc 6.2.1,调试器是gdb 7.12,版本再低点也可以,只要gcc版本 ...
随机推荐
-
CHAP认证原理
整个过程就是PPP协商过程,分三步:LCP.认证.NCP. 一 协议概述 PPP包含以下两个层次的协议: ·链路控制协议(LCP):负责建立.配置和测试数据链路连接 ·网络控制协议(NCP):负责建立 ...
-
Qt入门1---widget、mainwindow和Dialog区别
摘要: 看了一个月的Qt,居然没有理清Qt中 ------------------------------------ 1.QMainWindow A main window provides a f ...
-
list和数组之间相互的转化
list变成数组: String[] str=(String[]) list.toArray(new String[list.size()]); 数组变成list: List<String> ...
-
Codeforces 112B-Petya and Square(实现)
B. Petya and Square time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
-
Linux设备树语法详解【转】
转自:http://www.cnblogs.com/xiaojiang1025/p/6131381.html 概念 Linux内核从3.x开始引入设备树的概念,用于实现驱动代码与设备信息相分离.在设备 ...
-
angularjs系列之轻松使用$q进行异步编程
第一部分关于js中的异步编程 异步编程简单的说就是你写了一段代码,但他不会按照你书写代码的顺序立即执行,而是等到程序中发生了某个事件(如用户点击了某个按钮,某个ajax请求得到了响应)才去执行这段代码 ...
-
开发 chrome 扩展 GitHub-Remarks 的一些想法以及遗憾
前文 对于单页应用中如何监听 URL 变化的思考 说到我在开发 chrome 扩展 GitHub Remarks 中遇到的一个问题,本文来聊聊开发这个扩展的前后心路历程. 为什么开发这个扩展?前文说到 ...
-
Java8虚拟机内存模型
1. Java虚拟机运行时数据区 在JDK1.8之前,JVM运行时数据区分为堆.虚拟机栈.本地方法栈.方法区.程序计数器.如下图所示: 虚拟机栈:线程私有,随线程创建而创建.栈里面是一个一个“栈帧” ...
-
electron-vue项目搭建
参考:https://simulatedgreg.gitbooks.io/electron-vue/content/cn/getting_started.html 1. cnpm install -g ...
-
Java编程的逻辑 (80) - 定时任务的那些坑
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...