一,顺序表的基本概念:
顺序表一般表现为数组,使用一组地址 连续 的存储单元依次存储数据元素,如图所示。它具有如下特点:
- 长度固定,必须在分配内存之前确定数组的长度。
- 存储空间连续,即允许元素的随机访问。
- 存储密度大,内存中存储的全部是数据元素。
- 要访问特定元素,可以使用索引访问,时间复杂度为 O(1)。
- 要想在顺序表中插入或删除一个元素,都涉及到之后所有元素的移动,因此时间复杂度为 $O(n)$。
二,顺序表的c++模板实现
注模板的声明文件(.h)和定义文件(.cpp)建议不要分开!!!
1,seqList.h的代码:
- #include <iostream>
- /***************线性表数据结构***************/
- template <typename DataType>
- class SeqList
- {
- public:
- //构造函数,初始化数组以及数组长度
- SeqList(int size=defaultSize)
- {
- if(size >0) //检查赋予的顺序表大小,如果合法则分配相应大小的内存
- {
- maxSize = size;
- length = 0;
- elements = new DataType[maxSize]; //分配顺序表的内存大小
- for(int i = 0;i<maxSize;i++)
- elements[i] = NULL; //初始化
- }
- else
- {
- cout<<"Error Length of SeqList!"<<endl;
- }
- }
- //析构函数
- ~SeqList()
- {
- //delete[] elements; //回收顺序表的内存空间
- }
- //插入元素
- bool InsertElement(DataType data); //向表尾插入新元素
- //获取元素
- DataType GetElement(int location); //返回指定位置的元素
- //删除元素
- bool DelElement(int location); //删除指定元素
- //更改指定元素
- bool ChangeElement(int location,DataType data);
- //打印顺序表
- bool PrintList();
- //查找指定元素,并返回其在表中的位置
- int FindElement(DataType data); //返回匹配的第一个元素的位置(位置是一个整数值哈!!)
- //判断顺序表是否为空
- bool IsEmpty(SeqList L) ;
- //初始化书序表
- bool InitList(int nLen);
- //清空顺序表
- void ClearList(SeqList *L);
- //销毁顺序表
- void DestroyList();
- void ConverseList();
- //获取顺序表长度
- int getLength()
- {
- return length;
- }
- private:
- static const int defaultSize = 10;//顺序表的默认长度,只有静态变量才能在类中初始化
- DataType *elements;
- int maxSize; //顺序表最大大小
- int length; //顺序表的当前有效长度
- };
2,seqList.cpp的代码:
- <pre name="code" class="html">#include "stdafx.h"
- #include <iostream>
- #include "seqList.h"
- /***************顺序表插入操作********************/
- template <typename DataType>
- bool SeqList<DataType>::InsertElement(DataType data)
- {
- int curIndex = length; //记录新元素的插入位置
- if(length >= maxSize) //判断顺序表是否已满
- {
- return false; //顺序表已满,返回false,插入不成功
- }
- else
- {
- elements[curIndex] = data; //将新元素插入顺序表表尾
- length++; //顺序表有效长度加1
- return true;
- }
- }
- template <typename DataType>
- bool SeqList<DataType>:: InitList(int nLen)
- {
- DataType nchar='A';
- for(int i = 0;i<nLen;i++)
- {
- InsertElement(nchar++); //插入数据实现初始化
- }
- return true;
- }
- template <typename DataType>
- DataType SeqList<DataType>::GetElement(int location)
- {
- if(location<0 || location>length) //判断位置索引是否合法
- {
- std::cout<<"参数无效"<<std::endl;
- return 0;
- }
- else
- {
- return elements[location]; //返回指定位置的元素
- }
- }
- template <typename DataType>
- bool SeqList<DataType>:: DelElement(int location)
- {
- if(location<0 || location>length) //判断位置索引是否合法
- {
- std::cout<<"参数无效"<<std::endl;
- return false;
- }
- else
- {
- int j=0;
- for (int i=location;i<length;i++)
- {
- elements[location+j-1]=elements[location+j]; //铲除指定位置的元素
- j++;
- }
- length--;
- return true;
- }
- }
- template <typename DataType>
- bool SeqList<DataType>:: ChangeElement(int location,DataType data)
- {
- if(location<0 || location>length) //判断位置索引是否合法
- {
- std::cout<<"参数无效"<<std::endl;
- return false;
- }
- else
- {
- elements[location-1]=data;
- return true;
- }
- }
- template <typename DataType>
- bool SeqList<DataType>:: PrintList()
- {
- //输出初始化后的顺序表
- for(int i = 0;i<length;i++)
- cout<<GetElement(i)<<" ";
- cout<<endl;
- return true;
- }
- template <typename DataType>
- int SeqList<DataType>:: FindElement(DataType data)
- {
- //输出初始化后的顺序表
- for(int i = 0;i<length;i++)
- { if (elements[i]==data)
- {
- return i;
- }
- }
- cout<<"没有改元素"<<endl;
- return 0;
- }
- /**************判断顺序表是否为空,若是则返回真********************/
- template<typename DataType>
- bool SeqList<DataType>::IsEmpty(SeqList<DataType> L)
- { /* 初始条件:顺序线性表L已存在。操作结果:若L为空表,则返回TRUE,否则返回FALSE */
- if(L.length==0)
- return true;
- else
- return false;
- }
- template<typename DataType>
- void SeqList<DataType>::ClearList(SeqList *L)
- {
- for(int i = 0;i<length;i++)
- elements[i] = 0; //初始化
- L->length=0;
- L->maxSize=0;
- }
- /**************销毁顺序表********************/ //
- template<typename DataType>
- void SeqList<DataType>::DestroyList()
- {
- //初始条件:顺序线性表L已存在。操作结果:销毁顺序线性表L
- length=0;
- maxSize=0;
- delete[] elements; //这句话不要再出现在析构函数中了
- elements=NULL;
- }
- /**************逆序顺序表********************/
- template<typename DataType>
- void SeqList<DataType>::ConverseList()
- {
- for (int i=0;i<length/2;i++)
- {
- DataType temp;
- temp=elements[i];
- elements[i]=elements[length-1-i];
- elements[length-1-i]=temp;
- }
- }
3,主函数测试代码如下:
- <pre name="code" class="html">// ConsoleAppMySeqList.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "seqList.cpp"
- #include "windows.h"
- using namespace std;
- typedef char Mytype;
- int _tmain(int argc, _TCHAR* argv[])
- {
- int nLen=0;
- cout<<"请输入链表的长度: ";
- cin>>nLen;
- SeqList<Mytype> list(nLen); //建立1个顺序表,长度为nLen,链表元素类型为Mytype
- list.InitList(nLen);
- cout<<"初始化后的内容为;(亲,萌妹纸自动帮您完成的哦!!!)"<<endl;
- list.PrintList();
- int nPos=0;
- cout<<"你想删除那个位置的元素(位置从1开始算起) ";
- cin>>nPos;
- while (true)
- {
- if (nPos>list.getLength())
- {
- cout<<"输入过大,重新输入(位置从1开始算起)"<<endl;
- cin>>nPos;
- }else
- {
- break;
- }
- }
- list.DelElement(nPos);
- //输出初始化后的顺序表
- list.PrintList();
- cout<<"现在顺序表的长度为: "<<endl;
- cout<<list.getLength()<<endl;
- Mytype mchar='0';
- int nPos1=0;
- char ans='n';
- do
- {
- cout<<"请输入您想改变的指定位置和相应元素(示例:“k 5”) ";
- cin>>mchar>>nPos1;
- list.ChangeElement(nPos1,mchar);
- cout<<"继续更改?(y/n) "<<endl;
- cin>>ans;
- } while (ans=='y'||ans=='Y');
- cout<<"更改后的顺序表为:"<<endl;
- list.PrintList();
- cout<<"执行逆序:"<<endl;
- list.ConverseList();
- list.PrintList();
- Mytype bchar='0';
- cout<<"请输入您想查找的元素: ";
- cin>>bchar;
- int npos2=list.FindElement(bchar);
- cout<<"您查找的元素的位置为:"<<endl;
- cout<<npos2<<endl;
- list.ClearList(&list);
- if (list.IsEmpty(list)==true)
- {
- cout<<"顺序表已被清空"<<endl;
- }else
- {
- cout<<"顺序表还有元素"<<endl;
- }
- cout<<"5秒后执行销毁命令.........."<<endl;
- Sleep(1000);
- cout<<"4秒后执行销毁命令.........."<<endl;
- Sleep(1000);
- cout<<"3秒后执行销毁命令.........."<<endl;
- Sleep(1000);
- cout<<"2秒后执行销毁命令.........."<<endl;
- Sleep(1000);
- cout<<"1秒后执行销毁命令.........."<<endl;
- Sleep(1000);
- cout<<"再见,谢谢.........."<<endl;
- list.DestroyList();
- system("PAUSE");
- return 0;
- }
4,测试结果
参考资源:
【1】《百度百科》
【2】http://zh.wikipedia.org/wiki/顺序表