开发环境:Eclipse
参考书籍:《数据结构》——清华大学出版社——殷人昆
SeqList.h
/*
* SeqList.h
*
* Created on: Jun 5, 2010
* Author: limx
*/
#include<iostream.h>
#include<stdlib.h>
const int defaultSize = 100;
template<class T>
class SeqList {
protected:
T * data;
//the size of the seqList
int maxSize;
//the array subscript of last element
int last;
void reSize(int newSize);
public:
//defaultSize is the default parameter;
SeqList(int sz = defaultSize);
virtual ~SeqList();
//const is mean that the parameter inside can not change
int size() const {
return maxSize;
}
//get the element at the array subscript i;
bool getData(int i, T& x);
T getData(int i);
//copy the seqlist constructor
SeqList(SeqList<T>& L);
int length() const {
return last;
}
//search the element by value x
int search(T x) const;
//insert element x into the array at subscript i ;
bool insert(int i, T & x);
//remove element of array subscript at i ;
bool remove(int i, T&x);
void input();
void output();
SeqList<T> operator =(SeqList<T> & L);
};
SeqList.cpp
/*
* SeqList.cpp
*
* Created on: Jun 5, 2010
* Author: limx
*/
#include"SeqList.h"
template<class T>
SeqList<T> SeqList<T>::operator =(SeqList<T> & L) {
maxSize = L.size();
last = L.length();
data = new T[maxSize];
if (data == NULL) {
{
cerr << "memory error !" << endl;
exit(1);
}
}
for (int i = 0; i <= last; i++) {
data[i] = L.getData(i);
}
}
template<class T>
T SeqList<T>::getData(int i) {
if (0 <= i && i <= last) {
return data[i];
} else {
return NULL;
}
}
template<class T>
bool SeqList<T>::getData(int i, T& x) {
if (0 <= i && i <= last) {
x = data[i];
return true;
} else {
return false;
}
}
template<class T>
SeqList<T>::SeqList(SeqList<T>& L) {
maxSize = L.size();
last = L.length();
data = new T[maxSize];
if (data == NULL) {
{
cerr << "memory error !" << endl;
exit(1);
}
}
for (int i = 0; i <= last; i++) {
data[i] = L.getData(i);
}
}
template<class T>
//delete the element at i ,i is the array subscript;
bool SeqList<T>::remove(int i, T&x) {
if (last < 0) {
return false;
}
if (i < 0 || i > last + 1) {
return false;
}
x = data[i];
for (int j = i; j <= last; j++) {
data[j] = data[j + 1];
}
last--;
}
template<class T>
SeqList<T>::SeqList(int sz) {
// TODO Auto-generated constructor stub
if (sz > 0) {
maxSize = sz;
last = -1;
data = new T[maxSize];
// null must be upper
if (data == NULL) {
cerr << "memory error!" << endl;
exit(1);
}
}
}
template<class T>
SeqList<T>::~SeqList() {
// TODO Auto-generated destructor stub
}
//insert the element at the location at ith,i is an array subscript;
template<class T>
bool SeqList<T>::insert(int i, T & x) {
if (last == maxSize - 1) {
return false;
} else if (i < 0 || i > last + 1) {
return false;
}
for (int j = last; j >= i; j--) {
data[j + 1] = data[j];
}
data[i] = x;
last++;
return true;
}
template<class T>
int SeqList<T>::search(T x) const {
for (int i = 0; i <= last; i++) {
if (data[i] == x) {
return i;
}
}
return -1;
}
template<class T>
void SeqList<T>::input() {
cout << "begin to create seqlist:" << endl;
while (1) {
cin >> last;
if (last <= maxSize - 1) {
break;
}
cout << "out of index" << maxSize - 1 << ":" << endl;
}
cout << "input the elements plz" << endl;
for (int i = 0; i <= last; i++) {
cout << i + 1 << ":";
cin >> data[i];
}
}
template<class T>
void SeqList<T>::output() {
cout << "the last element of location in the current list is :" << last
<< endl;
for (int i = 0; i <= last; i++) {
cout << "#" << i << ":" << data[i] << endl;
}
}
main.cpp
/*
* main.cpp
*
* Created on: Jun 5, 2010
* Author: limx
*/
#include<iostream.h>
#include"SeqList.cpp"
using namespace std;
int main() {
SeqList<char> sl(20);
int index;
char x;
/*I need to read about array*/
/*int* x = new int[10];*/
cout << "input the elements" << endl;
sl.input();
sl.output();
cout << "the size of sl" << sl.size();
cout << "enter the element to search:" << endl;
cin >> x;
cout << "the element to search is at " << sl.search(x) << endl;
cout << "enter the subscript of the element and the element to insert:"
<< endl;
cin >> index >> x;
sl.insert(index, x);
sl.output();
cout << "cope the array of sl" << endl;
SeqList<char> sl2(20);
sl2 = sl;
cout << "the element of sl2 is below:" << endl;
sl2.output();
return 0;
}
result :
input the elements
begin to create seqlist:
4
input the elements plz
1:a
2:b
3:c
4:d
5:e
the last element of location in the current list is :4
#0:a
#1:b
#2:c
#3:d
#4:e
the size of sl20enter the element to search:
a
the element to search is at 0
enter the subscript of the element and the element to insert:
5
f
the last element of location in the current list is :5
#0:a
#1:b
#2:c
#3:d
#4:e
#5:f
cope the array of sl
the element of sl2 is below:
the last element of location in the current list is :5
#0:a
#1:b
#2:c
#3:d
#4:e
#5:f
分享到:
相关推荐
数据结构中的 顺序线性表的基本操作,适合刚学数据结构的同学用
用C语言实现了顺序线性表的基本操作(创建、插入数据、获取数据、删除数据、获取存放的数据量、清空线性表、销毁线性表)
顺序线性表的JAVA实现代码,希望对学习数据结构的同学们有所帮助!
数据结构的线性表实现,顺序线性表的建立,输入,输出,排序,以及归并。可以参考一下
这是帮人做的一个期末考试题,从 txt 文件读取数据存储到 SqList 中。 实现了 SqList 的插入元素、删除元素操作。 代码在 VS2010 下编译通过。
华科计算机学院数据结构第一次实验报告,关于顺序线性表的编程,含代码
线性表操作,包括顺序线性表,链式线性表,双向循序线性表,还有实际应用线性表。通过Vc6.0测试
实训课时候上的顺序线性表,不错,大家可以下下来借鉴一下发。。。。
线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。...该资源是顺序线性表的C语言实现代码
顺序线性表.cpp
数据结构:顺序线性表的初始化、插入与输出(C/C++)
通讯录顺序线性表系统.cpp
顺序线性表(1).cpp
通讯录顺序线性表的删除.cpp
数据结构C语言版线性表算法2.4-数据结构c语言版严蔚敏顺序线性表12个基本操作及算法的实现... 定义线性表节点的结构.pdf数据结构C语言版线性表算法2.4-数据结构c语言版严蔚敏顺序线性表12个基本操作及算法的实现... ...
C与数据结构-第13次课(2)--顺序线性表的定义、创建和输出 定义线性表节点的结构.ppt
1. 可扩展性,线性表的初始尺寸为10,尺寸可扩展(设计一个线性表扩展函数,在保留原有数据的情况下,扩展线性表尺寸) 2. 插入数据,插入点后的数据后移; 3. 删除数据,删除点后的数据前移;
代码是我参考了国嵌嵌入式数据结构教学视频及相关资料整理而成,内含详细的注释,我相信对于产品开发应用帮助很大。