说明:代码是可以运行的,但是发表在博客上后复制到编译器里面报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 ;
}