/*******************************************
the headfile of static sequence list, which defines the class of static seqlist
filename: sta_seqlist.h
copyright: cozzw
author: zzw
created date: 2013-1-31
**********************************************/
1 #ifndef _STA_SEQLIST
2
3 #define _STA_SEQLIST
4
5 const int MAXSIZE = 100; //数组最大长度
6
7 class Sta_seqlist //定义静态顺序表类。
8 {
9 private: // 表的总大小,当前使用的大小,用数组模拟顺序表
10 int a[MAXSIZE];
11 int length;
12 int size;
13
14 public:
15 Sta_seqlist(); // 带参构造函数,参数初始化当前的表的长度
16 bool insert(int i, int elem);//在第i个位置插入数据elem,修改表的长度,返回插入状态
17 bool _delete(int i, int& elem);//删除第i个位置的元素,修改表长,删除元素放在elem中,返回删除状态
18 int getLength(); //返回表的当前长度
19 int get(int i); //返回第i个元素值
20 int find(int key);//查找关键字为key的元素在顺序表中的位置
21 void show(); //打印数组元素
22 ~Sta_seqlist(){}
23 };
24
25
26 #endif
27
/*******************************************
the file realizes the function in the class of seqlist
filename: sta_seqlist.cc
copyright: cozzw
author: zzw
**********************************************/
1 #include <iostream>
2 #include "sta_seqlist.h"
3 using namespace std;
4
5 Sta_seqlist::Sta_seqlist():n(0),length(MAXSIZE)
6 {
7 a[0] = 0; //哨兵;
8 }
9
10 bool Sta_seqlist:: insert(int i, int elem)
11 {
12 if(i<1 || i>this->length-1 ) //判断插入位置是否合理
13 {
14 cout<<"插入位置不合理,程序退出"<<endl;
15 return false;
16 }
17 else
18 if( n == this->length-1 ) //判断表是否满
19 {
20 cout<<"表已满,程序退出"<<endl;
21 return false;
22 }
23 else
24 {
25 for(int j=n; j>i-1; j--) //从第i个到第n个元素后移一位
26 this->a[j+1] = this->a[j];
27 this->a[i] = elem;
28 this->size++;
29 return true;
30 }
31 }
32
33 bool Sta_seqlist:: _delete(int i, int& elem)
34 {
35 if(i<1 || i>this->n+1 ) //判断删除位置是否合理
36 {
37 cout<<"删除位置不合理,程序退出"<<endl;
38 return false;
39 }
40
41 elem = this->a[i];
42 this->size = this->size-1;
43
44 for(int j=i; j<n+1; j++) //从第i个元素开始,用前一个元素覆盖后一个元素
45 {
46 this->a[j] = this->a[j+1];
47 }
48
49 return true;
50 }
51
52 int Sta_seqlist:: getLength()
53 {
54 return this->size;
55 }
56
57 int Sta_seqlist::get(int i)
58 {
59 if(i<1 || i>n)
60 {
61 cout<<"该位置元素不存在,返回0"<<endl;
62 return 0;
63 }
64 else
65 return a[i];
66 }
67
68 void Sta_seqlist::show()
69 {
70 for(int i=1; i<n+1; i++)
71 cout<<a[i]<<" ";
72 cout<<endl;
73 }
74
75 int Sta_seqlist::find(int key)
76 {
77 a[0] = key;
78 int i = size;
79
80 while(a[i] != key)
81 i--;
82 return i;//用i=0标记查找失败,避免了记录查找次数
83 }
/*******************************************
the file is used to test the seqlist
filename: main.cc
copyright: cozzw
author: zzw
created date: 2013-1-31
**********************************************/
#include "sta_seqlist.h"
#include<iostream>
using namespace std;
int main(void)
{
Sta_seqlist seqlist;
int elem;
int n;
for(int i=1; i<5; i++)
seqlist.insert(i,i);
n = seqlist.getLength();
cout<<"元素个数为"<<n<<endl;
seqlist.show();
seqlist.insert(2,10);
n = seqlist.getLength();
cout<<"元素个数为"<<n<<endl;
seqlist.show();
cout<<"记录4在表中的位置为:";
cout<<seqlist.find(4)<<endl;
seqlist._delete(2,elem);
cout<<"元素个数为"<<seqlist.getLength()<<endl;
seqlist.show();
cout<<"被删除元素为"<<elem<<endl;
cout<<seqlist.get(2)<<endl;
}